How to get array intersect with single array group - php

I have array values in single array and I need to intersect the arrays inside the main array.
Here is my code:
$a[1] = array ( 'value' => 'America','value1' => 'England1','value2' => 'Australia','value3' => 'England','value4' => 'Canada', );
$a[2] = array ( 'value' => 'America','value1' => 'Wales','value2' => 'Australia','value3' => 'England1','value4' => 'Canada', );
$a[3] = array ( 'value' => 'America','value1' => 'England','value2' => 'Australia','value3' => 'England1','value4' => 'Canada', );
I need to show the intersect values in the array. I need the result as follows:
Array
(
[value] => America
[value1] => England1
[value2] => Australia
[value4] => Canada
)
I can't check with this array with array_intersect() function. because array keys are coming in dynamically.
This is just a sample. It goes like:
$a[1],$a[2],$a[3].....$a[n]
So please suggest any solution for this.

You can do this with call_user_func_array:
$result = call_user_func_array("array_intersect", $a);

Its very easy use array_intersect() as follows
$a[1] = array ( 'value' => 'America','value1' => 'England1','value2' => 'Australia','value3' => 'England','value4' => 'Canada' );
$a[2] = array ( 'value' => 'America','value1' => 'Wales','value2' => 'Australia','value3' => 'England1','value4' => 'Canada' );
$a[3] = array ( 'value' => 'America','value1' => 'England','value2' => 'Australia','value3' => 'England1','value4' => 'Canada');
$c=count($a);
$new=a[0];
for($i=0;$i<$c;$i++)
{
$new=array_intersect($new, $a[$i+1]);
}
print_r($new);

Related

PHP : How to make parallel arrays from multidimentional array

I have this type of array
$arr = array(
0 => array(
0 => array(
'name' => 'test1',
'country' => 'abc'
)
1 => array(
'name' => 'test2',
'country' => 'xyz'
)
)
1 => array(
'name' => 'test3',
'country' => 'pqr'
)
);
How can I make all arrays as parallel arrays. So that all sub arrays are parallel to each other without using any loop.
Like this
$arr = array(
0 => array(
'name' => 'test1',
'country' => 'abc'
)
1 => array(
'name' => 'test2',
'country' => 'xyz'
)
2 => array(
'name' => 'test3',
'country' => 'pqr'
)
);
Any help is much appreciated. !
A dynamic version of Nigel's code would be to loop the array and merge each subarray.
$new = [];
foreach($arr as $subarr){
$new = array_merge($new, $subarr);
}
var_dump($new);
https://3v4l.org/np2ZD
You could simply merge the arrays...
$out = array_merge($arr[0], [$arr[1]]);
print_r($out);
Which gives...
Array
(
[0] => Array
(
[name] => test1
[country] => abc
)
[1] => Array
(
[name] => test2
[country] => xyz
)
[2] => Array
(
[name] => test3
[country] => pqr
)
)

How to flatten (compress) multi-dimensional array into one-dimensional array in PHP?

I got this array. I want all the other 3 array come into this [0] => Array. Don't want unique value just want to merge all array flat in to [0] => Array.
Array
(
[0] => Array
(
[0] => Array
(
[Campaign] => xxx
[Phone] => 111
[State] => cd
)
)
[1] => Array
(
[0] => Array
(
[Campaign] => zxxxzx
[Phone] => 111111
[State] => zxxx
)
)
[2] => Array
(
[0] => Array
(
[Campaign] => aaaa
[Phone] => 111
[State] => Csd
)
)
[3] => Array
(
[0] => Array
(
[Campaign] => sasa
[Phone] => 111
[State] => asas
)
)
)
This is another example of how important the naming is. What you are working with is basically:
$recordsGroups = array(
// first group:
array(
// record 1:
array(
'key1' => 'val1',
'key2' => 'val2',
),
// record 2:
array(
'key1' => 'aaa',
'key2' => 'bbb',
),
),
// 2nd group:
array(
// record 3:
array(
'key1' => 'ccc',
'key2' => 'ddd',
),
),
);
And what you are probably trying to do is:
$records = array();
foreach ($recordsGroups as $group)
foreach ($group as $record)
$records[] = $record;
Which will give you:
$records = array(
// record 1:
array(
'key1' => 'val1',
'key2' => 'val2',
),
// record 2:
array(
'key1' => 'aaa',
'key2' => 'bbb',
),
// record 3:
array(
'key1' => 'ccc',
'key2' => 'ddd',
),
);
This should do nicely:
$array = call_user_func_array('array_merge', $array);
Or Argument unpacking via ... (splat operator):
$array = array_merge(...$array);
Because arrays can't have duplicate keys, the best that this multi-dim array can be condensed is down to an indexed array of associative arrays. array_column() will make quick work of this task.
Code: (Demo)
$array=[
[
['Campaign'=>'xxx','Phone'=>'111','State'=>'cd']
],
[
['Campaign'=>'zxxxzx','Phone'=>'111111','State'=>'zxxx']
],
[
['Campaign'=>'aaaa','Phone'=>'111','State'=>'Csd']
],
[
['Campaign'=>'sasa','Phone'=>'111','State'=>'asas']
]
];
var_export(array_column($array,0));
Output:
array (
0 =>
array (
'Campaign' => 'xxx',
'Phone' => '111',
'State' => 'cd',
),
1 =>
array (
'Campaign' => 'zxxxzx',
'Phone' => '111111',
'State' => 'zxxx',
),
2 =>
array (
'Campaign' => 'aaaa',
'Phone' => '111',
'State' => 'Csd',
),
3 =>
array (
'Campaign' => 'sasa',
'Phone' => '111',
'State' => 'asas',
),
)

PHP Replace Array Values

I have 2 multidimensional arrays that I am working with:
$arr1 =
Array
([type] => characters
[version] => 5.6.7.8
[data] => Array
([Char1] => Array
([id] => 1
[name] =>Char1
[title] =>Example
[tags] => Array
([0] => DPS
[1] => Support))
[Char2] => Array
([id] => 2
[name] =>Char2
[title] =>Example
[tags] => Array
([0] => Tank
[1] => N/A)
)
)
etc...
$arr2=
Array
([games] => Array
([gameId] => 123
[gameType => Match
[char_id] => 1
[stats] => Array
([damage] => 55555
[kills] => 5)
)
([gameId] => 157
[gameType => Match
[char_id] => 2
[stats] => Array
([damage] => 12642
[kills] => 9)
)
etc...
Basically, I need almost all the data in $arr2... but only the Char name from $arr1. How could I merge or add the $arr1['name'] key=>value into $arr2 where $arr1['id'] is equal to $arr2['char_id'] as the "id" field of each array is the same number.
I've attempted using array_merge and array_replace, but I haven't come up with any working solutions. This is also all data that I am receiving from a 3rd party, so I have no control on initial array setup.
Thanks for any help or suggestions!
Actually, this is quite straighforward. (I don't think there a built-in function that does this.)
Loop $arr2 and under it loop also $arr1. While under loop, just add a condition that if both ID's match, add that particular name to $arr2. (And use some referencing & on $arr2)
Consider this example:
// your data
$arr1 = array(
'type' => 'characters',
'version' => '5.6.7.8',
'data' => array(
'Char1' => array(
'id' => 1,
'name' => 'Char1',
'title' => 'Example',
'tags' => array('DPS', 'Support'),
),
'Char2' => array(
'id' => 2,
'name' => 'Char2',
'title' => 'Example',
'tags' => array('Tank', 'N/A'),
),
),
);
$arr2 = array(
'games' => array(
array(
'gameId' => 123,
'gameType' => 'Match',
'char_id' => 1,
'stats' => array('damage' => 55555, 'kills' => 5),
),
array(
'gameId' => 157,
'gameType' => 'Match',
'char_id' => 2,
'stats' => array('damage' => 12642, 'kills' => 9),
),
),
);
foreach($arr2['games'] as &$value) {
$arr2_char_id = $value['char_id'];
// loop and check against the $arr1
foreach($arr1['data'] as $element) {
if($arr2_char_id == $element['id']) {
$value['name'] = $element['name'];
}
}
}
echo '<pre>';
print_r($arr2);
$arr2 should look now like this:
Array
(
[games] => Array
(
[0] => Array
(
[gameId] => 123
[gameType] => Match
[char_id] => 1
[stats] => Array
(
[damage] => 55555
[kills] => 5
)
[name] => Char1 // <-- name
)
[1] => Array
(
[gameId] => 157
[gameType] => Match
[char_id] => 2
[stats] => Array
(
[damage] => 12642
[kills] => 9
)
[name] => Char2 // <-- name
)
)
)
Iterate over $arr2 and add the data to it from the matching $arr1 array value:
$i = 0;
foreach($arr2['games'] as $arr2Game){
$id = $arr2Game['char_id'];
$arr2['games'][$i]['name'] = $arr1['data'][$id]['name'];
$i++;
}
Have not tested this code.
If I'm understanding you correctly, you want to add a name index to each of the arrays within the $arr2['games'] array.
foreach($arr2['games'] as $key => $innerArray)
{
$arr2['games'][$key]['name'] = $arr1['data']['Char'.$innerArray['char_id']]['name'];
}

How do I make a list of specific values in a PHP multidimensional array?

I have a multidimensional array like so:
$neighborhood => array(
'the_smiths' => array(
'dad' => 'Donald',
'mom' => 'Mary',
'daughter' => 'Donna',
'son' => 'Samuel'
)
'the_acostas' => array(
'dad' => 'Diego',
'mom' => 'Marcela',
'daughter' => 'Dominga',
'son' => 'Sergio'
)
);
I would like to create another array (let's call it $array_of_moms) of all the moms in the neighborhood. Pulling them all in separately is doable, but not practical (like so):
$array_of_moms = array(
$neighborhood['the_smiths']['mom'],
$neighborhood['the_acostas']['mom'],
)
How do I create something like this:
$array_of_moms = $neighborhood['mom'];
$moms = array();
foreach($neighborhood as $family)
{
$moms[] = $family['mom'];
}
This'll iterate through each family in the array and add the mom to the new $moms array.
Using foreach, you can iterate through an array with variable indicies.
$array_of_moms = array();
foreach ($neighborhood AS $family) {
$array_of_moms[] = $family['mom']; // append mom of each family to array
}
If you can manipulate your array, you could:
<?php
$neighborhood = array(
'families' => array(
'the_smiths' => array(
'dad' => 'Donald',
'mom' => 'Mary',
'daughter' => 'Donna',
'son' => 'Samuel'
),
'the_acostas' => array(
'dad' => 'Diego',
'mom' => 'Marcela',
'daughter' => 'Dominga',
'son' => 'Sergio'
)
)
);
foreach ($neighborhood['families'] as $family => $folks) {
$neighborhood['moms'][] = $folks['mom'];
}
print_r($neighborhood);
?>
Which outputs:
Array
(
[families] => Array
(
[the_smiths] => Array
(
[dad] => Donald
[mom] => Mary
[daughter] => Donna
[son] => Samuel
)
[the_acostas] => Array
(
[dad] => Diego
[mom] => Marcela
[daughter] => Dominga
[son] => Sergio
)
)
[moms] => Array
(
[0] => Mary
[1] => Marcela
)
)
http://codepad.org/xbnj5UmV

Which PHP Array function should I use?

I have two arrays:
Array
(
[0] => Array
(
[id] => 1
[type] => field
[remote_name] => Title
[my_name] => title
[default_value] => http%3A%2F%2Ftest.com
)
[1] => Array
(
[id] => 2
[type] => field
[remote_name] => BookType
[my_name] => book-type
[default_value] =>
)
[2] => Array
(
[id] => 3
[type] => value
[remote_name] => dvd-disc
[my_name] => dvd
[default_value] =>
)
)
Array
(
[title] => Test
[book-type] => dvd
)
I need to take each key in the second array, match it with the my_name value in the first array and replace it with the corresponding remote_name value of the first array while preserving the value of the second array.
There's got to be some carrayzy function to help!
EDIT: There will also be a few cases that the value of the second array will need to be replaced by the value of the first array's remote_name where the value of the second array matches the value of the first array's my_name. How can I achieve this?
EG: book-type => dvd should turn into BookType => dvd-disc
Like so?:
$first = array(
array(
'id' => 1,
'type' => 'field',
'remote_name' => 'Title',
'my_name' => 'title',
'default_value' => 'http%3A%2F%2Ftest.com',
),
array(
'id' => 2,
'type' => 'field',
'remote_name' => 'BookType',
'my_name' => 'book-type',
'default_value' => '',
),
array(
'id' => 3,
'type' => 'value',
'remote_name' => 'dvd-disc',
'my_name' => 'dvd',
'default_value' => '',
),
);
$second = array(
'title' => 'Test',
'book-type' => 'dvd',
);
$map = array('fields' => array(), 'values' => array());
foreach ($first as $entry) {
switch ($entry['type']) {
case 'field':
$map['fields'][$entry['my_name']] = $entry['remote_name'];
break;
case 'value':
$map['values'][$entry['my_name']] = $entry['remote_name'];
break;
}
}
$new = array();
foreach ($second as $key => $val) {
$new[isset($map['fields'][$key]) ? $map['fields'][$key] : $key] = isset($map['values'][$val]) ? $map['values'][$val] : $val;
}
print_r($new);
Output:
Array
(
[Title] => Test
[BookType] => dvd-disc
)
Explanation:
The first loop collects the my_name/remote_name pairs for fields and values and makes them more accessible.
Like so:
Array
(
[fields] => Array
(
[title] => Title
[book-type] => BookType
)
[values] => Array
(
[dvd] => dvd-disc
)
)
The second loop will traverse $second and use the key/value pairs therein to populate $new. But while doing so will check for key/value duplicates in $map.
Keys or values not found in the map will be used as is.
foreach($arr1 as &$el) {
$el['remote_name'] = $arr2[$el['my_name']];
}
unset($el);
I am not aware of such a carrayzy function, but I know how you could do it:
//$array1 is first array, $array2 is second array
foreach($array1 as $key => $value){
if (isset($value['remote_name'], $value['my_name']) && $value['remote_name'] && $value['my_name']){
$my_name = $value['my_name'];
if (isset($array2[$my_name])) {
$remote_name = $value['remote_name'];
$array2[$remote_name] = $array2[$my_name];
//cleanup
unset($array2[$my_name]);
}
}
}

Categories