html - HTTP post array merging with PHP -


i have following question:

i have following inputs:

<input type="text" name="name[]" ... <input type="text" name="qty[]" ... <input type="text" name="value[]" ... 

what receive after posting is:

'name' =>    array (size=3)     0 => string 'book one' (length=8)     1 => string 'book two' (length=8)     2 => string 'book three' (length=10) 'qty' =>    array (size=3)     0 => string '1' (length=1)     1 => string '3' (length=1)     2 => string '1' (length=1) 'value' =>    array (size=3)     0 => string '10' (length=5)     0 => string '30' (length=5)     0 => string '25' (length=5) 

because of cannot foreach, , doing , index not nice way. :(

i merge into:

'items' =>   array (size=3)     0 => array('name' => 'book one', 'qty' => '1', 'value' => '10')     1 => array('name' => 'book two', 'qty' => '3', 'value' => '30')     2 => array('name' => 'book three', 'qty' => '1', 'value' => '25') 

any nice way it?

i still run foreach on array

$r = array(     'name' => array (         0 => 'book one',         1 => 'book two',         2 => 'book three'     ),     'qty' => array(         0 => '1',         1 => '3',         2 => '1'     ),     'value' =>  array(         0 => '10',         1 => '30',         2 => '25'     ) );  $i=0; $e=array(); $ehh = array_keys($r); foreach ($r $k) {     $e[] = array_combine($ehh, array_column($r,$i));     $i++; }   echo "<pre>";  print_r($e);  echo "</pre>"; 

$r yr array, $ehh stores it's keys while. each loop takes corresponding column , combines key values. product:

array (     [0] => array         (             [name] => book 1             [qty] => 1             [value] => 10         )      [1] => array         (             [name] => book 2             [qty] => 3             [value] => 30         )      [2] => array         (             [name] => book 3             [qty] => 1             [value] => 25         )  ) 

if yr < php 5 , array column function not there yet, use this:

function array_column($a=null,$b=null,$c=null){$argc=func_num_args();$d=func_get_args();if($argc<2){trigger_error("array_column() expects @ least 2 parameters, {$argc} given",e_user_warning);return null;}if(!is_array($d[0])){trigger_error('array_column() expects parameter 1 array, '.gettype($d[0]).' given',e_user_warning);return null;}if(!is_int($d[1])&&!is_float($d[1])&&!is_string($d[1])&&$d[1]!==null&&!(is_object($d[1])&&method_exists($d[1],'__tostring'))){trigger_error('array_column(): column key should either string or integer',e_user_warning);return false;}if(isset($d[2])&&!is_int($d[2])&&!is_float($d[2])&&!is_string($d[2])&&!(is_object($d[2])&&method_exists($d[2],'__tostring'))){trigger_error('array_column(): index key should either string or integer',e_user_warning);return false;}$e=$d[0];$f=($d[1]!==null)?(string) $d[1]:null;$g=null;if(isset($d[2])){if(is_float($d[2])||is_int($d[2])){$g=(int) $d[2];}else{$g=(string) $d[2];}}$h=array();foreach($e $i){$j=$k=null;$l=$m=false;if($g!==null&&array_key_exists($g,$i)){$l=true;$j=(string) $i[$g];}if($f===null){$m=true;$k=$i;}elseif(is_array($i)&&array_key_exists($f,$i)){$m=true;$k=$i[$f];}if($m){if($l){$h[$j]=$k;}else{$h[]=$k;}}}return $h;} 

or: comment here robbieaverill[at]gmail.com //shorter function

array_walk($array, function(&$value, $key, $return) {   $value = $value[$return]; }, 'foo'); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -