php - Converting certain sub arrays to another array -
php - Converting certain sub arrays to another array -
i'm collecting form fields on page via jquery , passing them on php page in ajax post, array passed below (actual info on 60 fields / arrays @ present)
array ( [0] => array ( [0] => main [1] => text [2] => product-name [3] => fieldvalue ) [1] => array ( [0] => main [1] => select [2] => product-range [3] => fieldvalue ) [2] => array ( [0] => main [1] => select [2] => product-year [3] => fieldvalue ) [3] => array ( [0] => main [1] => text [2] => product-type [3] => fieldvalue ) [4] => array ( [0] => main [1] => text [2] => product-sku [3] => fieldvalue ) [5] => array ( [0] => main [1] => text [2] => component-name [3] => fieldvalue ) [6] => array ( [0] => main [1] => text [2] => component-stid [3] => fieldvalue )
on form user can dynamically add together multiple sets of component fields, looking grouping 7 component fields array , add together multidimensional array of components can sort through later. way looping through array info follows
foreach($formdata $value) { if($value[0] == 'main') { if($value[2] == 'product-name') { $productname = $value[3]; } if($value[2] == 'product-range') { $productrange = $value[3]; } if($value[2] == 'product-year') { $productyear = $value[3]; } } }
i struggling find clean way of pulling off, advise on best practice?
thanks
you can utilize associative array this:
$arr = array( 'product_range' => array("main" => "text", 'product_range' => 'val'), 'product_type' => array("main" => "text", 'product_type' => 'val') ); foreach ($arr $key => $val) { echo $key." ".$val[$key]."<br>"; }
php arrays multidimensional-array
Comments
Post a Comment