Merge nested arrays - php

I have an array like this:
array(
0 => array(
'name' => 'group_one',
'options' => array('foo'=>'one', 'bar'=>'two')
),
1 => array(
'name' => 'group_two',
'options' => array('baz'=>'three', 'qux'=>'four')
)
);
I want to merge all of the nested options arrays into one, so it'll be like this:
array(
'foo' => 'one',
'bar' => 'two',
'baz' => 'three',
'qux' => 'four'
);
I have a feeling this is very simple, but anything I try seems too convoluted. Any help would be appreciated.

Maybe something slicker but this should work for any length of array:
$result = array();
foreach($array as $v) {
$result = array_merge($result, $v['options']);
}
Output:
Array
(
[foo] => one
[bar] => two
[baz] => three
[qux] => four
)

$my_array = $array[0]['options'] + $array[1]['options'];
You can also use array_merge()

or try to get a new array like this
$array = array(
0 => array(
'name' => 'group_one',
'options' => array('foo' => 'one', 'bar' => 'two')
),
1 => array(
'name' => 'group_two',
'options' => array('baz' => 'three', 'qux' => 'four')
)
);
$options = array();
foreach ($array as $arr) {
foreach ($arr['options'] as $key => $value) {
$options[$key] = $value;
}
}
echo '<pre>';
print_r($options);
echo '<pre>';

Related

How to extract an array with multiple array keys and values to another array?

Please note that my php version is not 7.
I have an array of arrays like:
array(
'0' => array('id'=>1,'name'=>'abc',"class"=>'xyz'),
'1' => array('id'=>2,'name'=>'abc1',"class"=>'xyz1'),
'2' => array('id'=>3,'name'=>'abc',"class"=>'xyz2'),
);
I want to extract it into two arrays. like
array(
'0' => array('id'=>1,'name'=>'abc'),
'1' => array('id'=>2,'name'=>'abc1'),
'2' => array('id'=>3,'name'=>'abc'),
);
array(
'0' => array('id'=>1,"class"=>'xyz'),
'1' => array('id'=>2,"class"=>'xyz1'),
'2' => array('id'=>3,"class"=>'xyz2'),
);
How can i achieve this, I am in search of some built-in function etc, I studied its supported with array_column but with versions higher than 7.
Edit:
I also tried array_intersect_key and array_slice but its working with single dimensional array.
Then you might want to keep it simple and just use a straight forward foreach loop like this
$old = array(
array('id'=>1,'name'=>'abc',"class"=>'xyz'),
array('id'=>2,'name'=>'abc1',"class"=>'xyz1'),
array('id'=>3,'name'=>'abc',"class"=>'xyz2')
);
foreach ( $old as $temp ) {
$new1 = array('id' => $temp['id'], 'name' => $temp['name']);
$new2 = array('id' => $temp['id'], 'class' => $temp['class']);
}
Use a foreach and add the values to a new array for example:
$idsNames = [];
$idsClasses = [];
$items = [
array('id' => 1, 'name' => 'abc', "class" => 'xyz'),
array('id' => 2, 'name' => 'abc1', "class" => 'xyz1'),
array('id' => 3, 'name' => 'abc', "class" => 'xyz2'),
];
foreach ($items as $item) {
$idsNames[] = ["id" => $item["id"], "name" => $item["name"]];
$idsClasses[] = ["id" => $item["id"], "class" => $item["class"]];
}

Removing selected elements from array of associative arrays

I have the following array of associative arrays.
$result = array(
(int) 0 => array(
'name' => 'Luke',
'id_number' => '1111',
'address' => '1544addr',
'time_here' => '2014-04-12 13:07:08'
),
(int) 1 => array(
'name' => 'Sam',
'id_number' => '2222',
'address' => '1584addr',
'time_here' => '2014-04-12 14:15:26'
I want to remove selected elements from this array such that it will look like this;
array(
(int) 0 => array(
'name' => 'Luke',
'id_number' => '1111'
),
(int) 1 => array(
'name' => 'Sam',
'id_number' => '2222',
This is the code I wrote;
foreach($result as $value)
{
unset($value('address') );
unset($value('time_here') );
}
When I run the code, Apache web server crashed.
Can the smarter members point out what did I do wrong? Thank you very much.
Array notation is wrong, use this;
$finalResult = array();
foreach($result as $value)
{
unset($value['address'] );
unset($value['time_here'] );
$finalResult[] = $value;
}
Here is a working demo: Demo
That is because you are not accessing array correctly. Use square bracket insted of round brackets :
foreach($result as $value)
{
unset($value['address'] );
unset($value['time_here'] );
}

PHP find dependencies of multi-dimensional array

I am able to traverse a multi-dimensional array, but I also need information about dependencies. Here is what I am trying to do. I have an array like this:
array(
'top1' => 'sth',
'top2' => array(
'sub1' => 'sth',
'sub2' => array(
'line1' => 'sth',
'line2' => 'sth'
)
)
'top3' => 'sth'
)
I am able to traverse the array to get all the keys, result is this:
array([0] => 'top1', [1] => 'top2', [2] => 'sub1', [3] => 'sub2', ...)
However, I need to know the parent of the current element. So I hope I could get something like this:
array(
[top1] => array('parent' => 0, 'id' => 1),
[top2] => array('parent' => 0, 'id' => 2),
[sub1] => array('parent' => 2, 'id' => 2.1),
[sub2] => array('parent' => 2, 'id' => 2.2),
[line1] => array('parent' => 2.2, 'id' => 2.2.1),
...
[top3] => array('parent' => 0, 'id' => 3)
)
I have been trying many ways, but couldn't always get the correct result. Can anyone solve this out? Thanks!
here is a working example for you
function traverse(array $input, $parent = null) {
$result = array();
$current = 1;
foreach ($input as $key => $value) {
$id = null !== $parent ? $parent . '.' . $current : $current;
$result[$key] = array('parent' => null !== $parent ? $parent : 0, 'id' => $id);
if (is_array($value)) {
$result = array_merge($result, traverse($value, $id));
}
$current++;
}
return $result;
}
$input = array(
'top1' => 'sth',
'top2' => array(
'sub1' => 'sth',
'sub2' => array(
'line1' => 'sth',
'line2' => 'sth'
)
),
'top3' => 'sth'
);
echo '<pre>';
print_r($input);
echo '<hr>';
print_r(traverse($input));
echo '</pre>';

php compare two multidimensional array and check for duplicate

I have two arrays
$array1 = array(
0 => array(
'user' => 'user0',
'id' => 'id0'
),
1 => array(
'user' => 'user1',
'id' => 'id1'
),
2 => array(
'user' => 'user2',
'id' => 'id2'
)
);
$array2 = array(
0 => array(
'emp' => 'emp0',
'id' => 'id3'
),
1 => array(
'emp' => 'emp1',
'id' => 'id1'
),
2 => array(
'emp' => 'emp2',
'id' => 'id2'
)
);
i need to loop array 2 first an d give input of id from array1 to the array 1 and search whether the value of id1 from arr1 exists in array2
Maybe this could work? (If I understood your question correctly)
$id_arr = array();
$final_arr = array();
checkArray($array1, $id_arr, $final_arr);
checkArray($array2, $id_arr, $final_arr);
function checkArray($arr, &$id_arr, &$final_arr) {
foreach ($arr as $key => $value) {
if (!in_array($value['id'], $id_arr)) {
$id_arr[] = $value['id'];
$final_arr[] = $value;
}
}
}
var_dump($final_arr);

Process list of PHP arrays to find arrays which contain the same keys/values

I have a long list of arrays which contain values. I'm trying to write a script that will locate duplicate key/value pairs among the arrays and report back to me so I know which arrays have the same data.
$array = array(
'one' => array('foo' => 'foo', 'baz' => 'baz', 'bar' => 'bar'),
'two' => array('foo' => 'foo', 'baz' => 'baz', 'nil' => 'nil'),
'three' => array('foo' => 'foo', 'var' => 'var'),
'four' => array('var' => 'var', 'nil' => 'nil'),
'five' => array('bar' => 'bar')
);
// First failed attempt
//call_user_func_array('array_intersect_assoc', $array));
The result should be able to identify 2 or more arrays share the same keys and values.
array(
array(
'keys' => array('one', 'two', 'three'),
'values' => array('foo' => 'foo')
),
array(
'keys' => array('one', 'five'),
'values' => array('bar' => 'bar')
),
array(
'keys' => array('one', 'two'),
'values' => array('foo' => 'foo', 'baz' => 'baz')
),
array(
'keys' => array('three', 'four'),
'values' => array('var' => 'var')
),
array(
'keys' => array('two', 'four'),
'values' => array('nil' => 'nil')
),
);
What is the proper way to process these arrays? How can I achieve something like this?
Actually, it was pretty easy assuming that the values are always strings.
$master = array();
foreach($array as $name => $row)
{
foreach($row as $key => $value)
{
if(empty($master[$key.$value]))
{
$master[$key.$value] = array();
}
$master[$key.$value][] = $name;
}
}
print_r($master);

Categories