This is an interesting situation which I created a working function for, but wondering if I just anyone had any simpler methods for this.
I have the following multidimensional array:
$foo = array(
[0] => array(
'keys' => array(
'key1' => 1,
'key2' => a,
'key3' => 123
),
'values' => array(
//goodies in here
)
)
[1] => array(
'keys' => array(
'key1' => 1,
'key2' => b,
'key3' => 456
),
'values' => array(
//goodies in here
)
)
)
What I wanted, was to transform this into a multidimensional array nested based on the values from the keys
array, the output I was looking for is:
$bar = array(
[1] => array(
[a] => array(
[123] => array( //values array from above )
),
[b] => array(
[456] => array( //values array from above )
)
)
)
The keys can always be nested based on their position in the keys
array, but the keys themselve are not always the same, keys
handles a user defined grouping, so the order and values can change. I also didn't want duplicate keys.
array_merge
failed me because in a lot of cases, the array keys are actually numeric ids. So, this function works - but I'm wondering if I made myself a new pair of gloves.
protected function convertAssociativeToMulti( &$output, $keys, $value )
{
$temp = array();
$v = array_values( $keys );
$s = sizeof( $v );
for( $x = 0; $x < $s; $x++ )
{
$k = $v[ $x ];
if ( $x == 0 )
{
if ( !array_key_exists( $k, $output ) )
$output[ $k ] = array();
$temp =& $output[ $k ];
}
if ( $x && ( $x + 1 ) !== $s )
{
if( !array_key_exists( $k, $temp ) )
$temp[ $k ] = array();
$temp =& $temp[$k];
}
if ( ( $x + 1 ) == $s )
$temp[$k] = $value;
}
}
$baz = array();
foreach( $foo as $bar )
{
$this->convertAssociativeToMulti( $baz, $bar['keys'], $bar['values'] );
}
So, how do you do this more simply / refactor what I have?
Associative array — An array where each key has its own specific value. Multidimensional array — An array containing one or more arrays within itself.
PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.
The index data type for a simple array must be an integer value. The index type for an associative array can be one of a set of supported data types. The index values in a simple array must be a contiguous set of integer values. In an associative array the index values can be sparse.
You can use the PHP array_values() function to get all the values of an associative array.
This is a bit more concise (See it in action here):
$bar=array();
foreach($foo as $item)
{
$out=&$bar;
foreach($item['keys'] as $key)
{
if(!isset($out[$key])) $out[$key]=array();
$out=&$out[$key];
}
foreach($item['values'] as $k=>$v) $out[$k]=$v;
// $out=array_merge($out,$item['values']); -- edit - fixed array_merge
}
var_dump($bar);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With