php - Merge two arrays into one array (identic keys) -
i think simple question, can't figure out how this. i've these 2 arrays:
( [0] => array ( [accordion_title] => [accordion-title]title 1[/accordion-title] ) [1] => array ( [accordion_title] => [accordion-title]title 2[/accordion-title] ) ) ( [0] => array ( [accordion_content] => [accordion-content]content 1[/accordion-content] ) [1] => array ( [accordion_content] => [accordion-content]content 2[/accordion-content] ) )
how can combine/merge them this?
( [0] => array ( [accordion_title] => [accordion-title]title 1[/accordion-title] [accordion_content] => [accordion-content]content 1[/accordion-content] ) [1] => array ( [accordion_title] => [accordion-title]title 2[/accordion-title] [accordion_content] => [accordion-content]content 2[/accordion-content] ) )
thanks help.
you can try in way:
$array1 = array( array('accordion_title'=>'title 1'),array('accordion_title'=>'title 2') ); $array2 = array( array('accordion_content'=>'content 1'),array('accordion_content'=>'content 2') ); $array3 = array(); foreach( $array1 $key => $array ) { $array3[] = array( key($array) => current($array), key($array2[$key]) => current($array2[$key]) ); } print_r( $array3 );
this output:
array ( [0] => array ( [accordion_title] => title 1 [accordion_content] => content 1 ) [1] => array ( [accordion_title] => title 2 [accordion_content] => content 2 ) )
updated:
with function can combine infinite arrays (not two), if have different sizes:
/* groups passed arrays in array of associative arrays same keys , values * * @example $array1 = array( array('a'=>'val1'),array('a'=>'val2') ); * $array2 = array( array('b'=>'val3'),array('b'=>'val4') ); * $array3 = array( array('c'=>'val5'),array(),array('c'=>'val6') ); * multiarraycombine( $array1, $array2, $array3 ); * return: array * ( * 0 => array('a'=>'val1','b'=>'val3','c'=>'val5'), * 1 => array('a'=>'val2','b'=>'val4'), * 2 => array('c'=>'val6') * ) * * @param array $array1[, $array2[, $array3...]] * * @option const t_object_cast cast returned assoc arrays stdobject * * @return array */ function multiarraycombine() { /* passed parameters , t_object_cast option: */ $args = func_get_args(); $asobject = ( t_object_cast == end($args) ); if( $asobject ) array_pop( $args ); $retval = array(); # init array returned /* retrieve highest passed arrays key: */ $max = 0; foreach( $args $array ) $max = max( $max, max( array_keys($array) ) ); /* loop each arrays key: */ for( $i=0; $i<=$max; $i++ ) { /* init associative array add: */ $add = array(); /* process actual key ($i) of each passed array: */ foreach( $args $array ) { /* if key ($i) exists, add each passed array: */ if( isset($array[$i]) , is_array($array[$i]) ) { foreach( $array[$i] $key => $val ) { $add[$key] = $val; } } } /* add obtained associative array return array */ if( $asobject ) $retval[] = (object) $add; else $retval[] = $add; } return $retval; }
so, following code (three arrays):
$array1 = array( array('accordion_title'=>'title 1'),array('accordion_title'=>'title 2') ); $array2 = array( array('accordion_content'=>'content 1'),array('accordion_content'=>'content 2') ); $array3 = array( array('accordion_date'=>'date 1'),array(),array('accordion_date'=>'date 3') ); print_r( multiarraycombine( $array1, $array2, $array3 ) );
the output is:
array ( [0] => array ( [accordion_title] => title 1 [accordion_content] => content 1 [accordion_date] => date 1 ) [1] => array ( [accordion_title] => title 2 [accordion_content] => content 2 ) [2] => array ( [accordion_date] => date 3 ) )
updated 2:
- now function return all passed values of each rows, not first;
- added option
t_object_cast
: passing constantt_object_cast
after list of arrays, rows of returned array formatted stdobjects instead of arrays.
explanation:
to allow not predetermined arguments, don't format function multiarraycombine( $arg1, $arg2, ... )
, use instead func_get_args()
function, "allow user-defined functions accept variable-length argument lists".
first of (in latest update), check if last argument predefined constant t_object_cast
: if is, set $asobject
true
, pop-it off end of arguments array; in $args
variable have array each passed arrays.
next step: retrieve max key value of passed arrays; choose way instead of more comfortable foreach( $array1 $row )
avoid omit values if 1 of other arrays have more rows first. eventually not numeric keys omitted.
then, main loop: process each row of originals arrays , add keys , values row added returned array. if there duplicated keys, last returned.
after processing each array, add obtained row (converted object if option passed) returning array.
that's all!
globish here, i'm sorry
Comments
Post a Comment