Remove elements from array1 that are in array2 - php

I have arrays that looks like this:
$array1 = array(
'peter#example.com' => array(
'peter' => 'Smith',
),
'john#example.com' => array(
'john' => 'Smith',
),
'louis#example.com' => array(
'louis' => 'Smith',
),
'jane#example.com' => array(
'jane' => 'Smith',
),
);
$array2 = array(
'0' => 'peter#example.com',
'1' => 'john#example.com',
);
How do I remove the array elements in array1 that match array2?

As simple as:
$diff = array_diff_key($array1, array_flip($array2));

Quick and easy (but not as quick and easy as deceze's method, lol)
foreach ($array1 as $key => $value) {
for ($i = 0; $i < count($array2); $i++) {
if ($key == $array2[$i]) {
unset($array1[$key]);
}
}
}

Related

Convert all values into a single string in a multi-dimension PHP array?

$array = array(
array(
'id' => 1,
'first_name' => 'testOne',
),
array(
'id' => 333,
'first_name' => 'test333',
)
);
Required Output:
array("1_testOne","333_test333");
This should help -
array_map(function($a) {
return implode('_', $a); // implode all with '_'
}, $array);
array_map()
$array = array(
array(
'id' => 1,
'first_name' => 'testOne',
),
array(
'id' => 333,
'first_name' => 'test333',
)
);
$new_array = [];
foreach($array as $key => $value) {
$new_array[] = $value['id'] . "_" . $value['first_name'];
}
echo "<pre>";
print_r($new_array);
It will return output as
Array('1_testOne', '333_test333')

Create multidimensional array recursively

The original array looks like this:
$array = array(
array(
'key' => 'key1',
'val' => 'val1'
),
array(
'key' => 'key2:subkey1',
'val' => 'val2'
),
array(
'key' => 'key3:subkey2',
'val' => 'val3'
),
array(
'key' => 'key3:subkey3:subsubkey1',
'val' => 'val4'
),
array(
'key' => 'key3:subkey3:subsubkey2',
'val' => 'val5'
),
array(
'key' => 'key3:subkey3:subsubkey3',
'val' => 'val6'
)
);
And I want to generate a new array based on the original one that looks like this:
$result = array(
'key1' => 'val1',
'key2' => array(
'subkey1' => 'val2'
),
'key3' => array(
'subkey2' => 'val3',
'subkey3' => array(
'subsubkey1' => 'val4',
'subsubkey2' => 'val5',
'subsubkey3' => 'val6'
)
)
);
The algorithm should be able to handle a reference array of any depth.
What I have tried so far works, but I am not happy with using eval for various reasons:
function convert($array) {
$out = array();
foreach ($array as $data) {
$key = $data['key'];
$pos = strpos($key, ':');
if ($pos === false) {
$out[$key] = $data['val'];
} else {
$split = explode(":", $key);
eval("\$out['" . implode("']['", $split) . "'] = '" . $data['val'] . "';");
}
}
return $out;
}
Is there a way to solve this without resorting to eval, i.e. setting the $out directly? The val comes from user input, so it is obviously very unsafe to use eval in this case.
Thank you for your advice.
function convert($array) {
$out = array();
foreach ($array as $data) {
$key = $data['key'];
$value = $data['val'];
$helper = &$out;
foreach (explode(':', $key) as $i) {
$helper = &$helper[$i];
}
$helper = $value;
}
return $out;
}
Took me a while to figure out how to do it. References are perhaps the best way to achieve it.
The $helper-variable is a temporary reference to the $out-array we want to return after we're done. It splits each key by the colon as delimiter and iterates through each individual key. In every iteration we change the helper reference to the next key.
An example:
$out = array();
explode(':', $key) => array('key3', 'subkey3', 'subsubkey1');
$helper = &$out;
// foreach loop starts
$helper = $helper['key3']; // Iteration 1
$helper = $helper['key3']['subkey3']; // Iteration 2
$helper = $helper['key3']['subkey3']['subsubkey1']; // Iteration 3
I hope you understand how it works.
Try this neat code:
$array = array(
array(
'key' => 'key1',
'val' => 'val1'
),
array(
'key' => 'key2:subkey1',
'val' => 'val2'
),
array(
'key' => 'key3:subkey2',
'val' => 'val3'
),
array(
'key' => 'key3:subkey3:subsubkey1',
'val' => 'val4'
),
array(
'key' => 'key3:subkey3:subsubkey2',
'val' => 'val5'
),
array(
'key' => 'key3:subkey3:subsubkey3',
'val' => 'val6'
)
);
$new_array = array();
foreach($array as $element) {
$temp = &$new_array;
foreach(explode(':', $element['key']) as $key) {
$temp = &$temp[$key];
}
$temp = $element['val'];
}
unset($temp);
var_dump($new_array);

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'] );
}

Merge nested arrays

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>';

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);

Categories