I recently create a function to combine some arrays of associative arrays in a single array in response to a stackoverflow question, where you can found more details about this.
The function works as expected, but I think their description is very obscure. In addition, i choose constant T_OBJECT_CAST
as option to convert each rows as object: someone can suggest a better predefined constants?
Goal:
As requested from another user (edited):
I've these two arrays:
1)
[0=>['title1'=>'Title 1'], 1=>['title2'=>'Title 2']];
2)
[0=>['contn1'=>'Content 1'],1=>['contn2'=>'Content 2']];
How can I combine/merge them that they look like this?
[
0=>['title1'=>'Title 1','contn1'=>'Content 1'],
1=>['title2'=>'Title 2','contn2'=>'Content 2']
]
Explanation:
(Globish here, i'm sorry)
To allow not predetermined arguments, I don't format function as multiArrayCombine( $arg1, $arg2, ... )
, I use instead the func_get_args()
function, that "allow user-defined functions to accept variable-length argument lists".
First of all, I check if the last argument is the predefined constant T_OBJECT_CAST
: if it is, I set $asObject
to True
, then I pop-it off the end of arguments array; now in the $args
variable I have an array with each passed arrays.
Next step: I retrieve the max key value of all passed arrays; i choose this way instead of more comfortable foreach( $array1 as $row )
to avoid to omit values if one of the other arrays have more rows than the first. Eventually not numeric keys are omitted.
Then, the main loop: I process each row of originals arrays and I add their keys and values to row that will added to returned array. If there are duplicated keys, only the last is returned.
After processing each array, i add the obtained row (converted to object if this option is passed) to returning array.
The function:
/* Groups passed arrays in an array of associative arrays with same keys and 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 as stdObject
*
* @return array
*/
function multiArrayCombine()
{
/* Get all passed parameters and T_OBJECT_CAST option: */
$args = func_get_args();
$asObject = ( T_OBJECT_CAST == $args[count($args)-1] );
if( $asObject ) array_pop( $args );
$retval = array(); # Init array to be returned
/* Retrieve highest passed arrays key: */
$max = 0;
foreach( $args as $array ) $max = max( $max, max( array_keys($array) ) );
/* Loop for each arrays key: */
for( $i=0; $i<=$max; $i++ )
{
/* Init associative array to add: */
$add = array();
/* Process actual key ($i) of each passed array: */
foreach( $args as $array )
{
/* If the key ($i) exists, add each passed array: */
if( is_array($array[$i]) )
{
foreach( $array[$i] as $key => $val )
{ $add[$key] = $val; }
}
}
/* Add the obtained associative array to return array */
if( $asObject ) $retval[] = (object) $add;
else $retval[] = $add;
}
return $retval;
}