PHP array merge from unknown number of parameters
Asked Answered
J

6

2

I have an PHP array looking like this:

$array['my_data']['value'] = 'some value';
$array['my_own_data']['value'] = 'another value';
$array['different_data']['value'] = 'another value';

I need to use array_merge in PHP or such. The problem is that the number of first level keys are unknown. It could be 150 items, I don't know.

This would not work for me because of the unknown number of keys:

array_merge($array['my_data'], $array['my_own_data'], $array['different_data']);

Do I need a loop or are there something fancy?

Jahdiel answered 19/7, 2011 at 13:4 Comment(4)
What do you want the final array to look like?Servo
did you know that array_merge($array['my_data'], $array['my_own_data'], $array['different_data']); will result into an array containing $array['different_data']['value'] as array_merge will overwrite the array elements with equal key?Tuition
What do you want to do? You've just told you've got some problem, but not what you need to retrieve from the original $array? The keys? The second keys? The values?Fullfaced
Possible in PHP 5.6 using the ... operator.Ungracious
S
6

This may do, if that's what you want:

$array = array_map('current', $array);

or possibly

$array = array_map(function ($a) { return $a['value']; }, $array);

This should give you

array('some value', 'another value', 'another value')
Servo answered 19/7, 2011 at 13:8 Comment(0)
F
2

The problem is that the number of first level keys are unknown. It could be 150 items, I don't know.

That's a problem easy to solve.

If you want to know the number of keys of the first level:

$numberOfKeysOfTheFirstLevel = count($array);

There you know the number now. If you need actually their names, you can do so as well:

$keyNamesOfTheFirstLevel = array_keys($array);

So then you continued in your question:

This would not work for me because of the unknown number of keys: array_merge($array['my_data'], $array['my_own_data'], $array['different_data']);

The good news is, you don't need to know the number of keys to perform your array_merge operation:

call_user_func_array('array_merge', $array);

So regarding to what you wrote about in your question, this should answer it. However the result might not be what you expected. See the other answers as well please and please ask.

Fullfaced answered 19/7, 2011 at 14:10 Comment(0)
D
1

Please try this:

$new_array = array();
foreach($array as $item) {
  $new_array[] = $item['value'];
};
Daves answered 19/7, 2011 at 13:9 Comment(0)
D
1
// 1. your array should have different keys, otherwise it will have no effect
$array['my_data']['value1'] = 'some value';
$array['my_own_data']['value2'] = 'another value';
$array['different_data']['value3'] = 'another value';

$result = call_user_func_array('array_merge', $array);

will return

array(
  'value1' => 'some value', 
  'value2' => 'another value', 
  'value3' => 'another value', 
)

Otherwise, did you wanted something like this ?

array(
  'value' => array('some value', 'another value', 'another value')
)

Then you could do

$array['my_data']['value'] = 'some value';
$array['my_own_data']['value'] = 'another value';
$array['different_data']['value'] = 'another value';

$result = array();
// PHP <= 5.2
array_walk($array, create_function('&$item, $key, &$dest', '
    foreach ((array) $item as $subKey => $value) {
        if (!isset($dest[$subKey])) { $dest[$subKey] = array(); }
        $dest[$subKey][] = $value;
    }
'), & $result);
var_export($result);
Depressed answered 19/7, 2011 at 14:26 Comment(0)
A
0
array_map(function($a){
    return $a['value'];
},$array);

for php5.2 and older:

function stuff($a){
    return $a['value'];
}
array_map('stuff',$array);
Assyriology answered 19/7, 2011 at 13:10 Comment(0)
S
0

If I understand what you want, you could use array_map.

To change:

$a = array();
$a['x']['value'] = 1;
$a['y']['value'] = 3;
$a['z']['value'] = 3;

into:

array(3) {
 ["x"]=>
 int(1)
 ["y"]=>
 int(2)
 ["z"]=>
 int(3)
}

You could define a function:

function f($x) { return $x['value'];}

and use array_map:

$b = array_map(f, $a);
Screwball answered 19/7, 2011 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.