I have 2 arrays -
Array
(
[0] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => test#test.com
)
[1] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => test3#test.com
)
[2] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => testagain#gmail.com
)
)
Array
(
[0] => Array
(
[to_email] => test#test.com
)
[1] => Array
(
[to_email] => test3#test.com
)
)
I want to get the values from Array 1 which are different from the second array.
I have tried using -
$result = array_diff_assoc($array1, $array2);
AND
$result = array_diff($array1, $array2);
But both gave error like -
Notice: Array to string conversion in
Outcome that I am expecting is
Array
(
[0] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => testagain#gmail.com
)
)
You can generate a list of the email addresses to exclude using array_column. We use the 3 parameter form to index that array by the email addresses as it makes it easier to filter with:
$exclude_ids = array_column($array2, 'to_email', 'to_email');
We can then use array_filter to filter $array1:
$output = array_filter($array1, function ($v) use ($exclude_ids) {
return !isset($exclude_ids[$v['to_email']]);
});
print_r($output);
Output:
Array
(
[2] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => testagain#gmail.com
)
)
Demo on 3v4l.org
Note if you want the output array re-indexed to 0, just use
$output = array_values($output);
Related
I have an array like mentioned below, which I want to rearrange without using loop:
Array
(
[0] => Array
(
[Books] => Array
(
[id] => 4
)
)
[1] => Array
(
[Books] => Array
(
[id] => 3
)
)
[2] => Array
(
[Books] => Array
(
[id] => 2
)
)
[3] => Array
(
[Books] => Array
(
[id] => 1
)
)
)
I want an output like this:
Array(4,3,2,1)
I'm assuming you do not want to use for or foreach loops, but anything else that internally is or uses a loop is fine.
in this case, you can use array_map:
$result = array_map(function($item){
return $item['books']['id'];
}, $currentArray);
OR
if you do not even want that:
$v1 = array_column($input, 'books');
$result = array_column($v1, 'id');
I have array 1 like this
Array
(
[0] => 1
[1] => 2
)
Second array would be
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
)
[1] => Array
(
[FullName] => Dvs Patel
)
)
I want to merge it the way values would be added to second array with same keys. Desired Output will look like this or some way around so that I can use the Array 1's value with Second Array Only:
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
[0] => 1
)
[1] => Array
(
[FullName] => Dvs Patel
[1] => 2
)
)
You can apply simple foreach() to do that
$final = [];
foreach($array2 as $key =>$arr2 ){
$final[$key]['FullName'] = $arr2['FullName'];
$final[$key][$key] = $array1[$key];
}
print_r($final);
Output:- https://eval.in/1010437
If both arrays are of the same length, you might use array_map passing the array_keys as the second parameter:
$array1 = ["1", "2"];
$array2 = [
["FullName" => "Bhupat Chippa"],
["FullName" => "Dvs Patel"]
];
$result = array_map(function($x, $y) use ($array1){
$x[$y] = $array1[$y];
return $x;
}, $array2, array_keys($array1));
print_r($result);
Demo
That will give you:
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
[0] => 1
)
[1] => Array
(
[FullName] => Dvs Patel
[1] => 2
)
)
I am new to postgresql with php. I get below array using pg_fetch_array() function.
Array
(
[0] =>
[name] =>
[1] => 1
[status] => 1
[2] => C2005
[code] => C2005
)
after removing index 1 and value of key status, i have to reindex this array so that expected output should become like:
Array
(
[0] =>
[name] =>
[1] => C2005
[code] => C2005
)
I tried
unset($row[1]);
unset($row['status'];
$foo = array_values($row);
echo "<pre>";
print_r($foo)
echo "</pre>";
and got output
Array
(
[0] =>
[name] =>
[2] => C2005
[code] => C2005
)
How the numeric index can be re-indexed after removing particular keys from the array?
You can filter the array by their key. Live demo.
unset($array['status']);
$number_keys = array_values(array_filter($array, function($k){return is_int($k) && $k != 1;}, ARRAY_FILTER_USE_KEY));
$nonnumber_keys = array_filter($array, function($k){return is_string($k);}, ARRAY_FILTER_USE_KEY);
$result = array_merge($number_keys, $nonnumber_keys);
I have a number of arrays:
Array ( [0] => A-I-only )
Array ( [0] => B-III-only )
Array ( [0] => C-I-and-II-only )
Array ( [0] => D-II-and-III-only )
Array ( [0] => E-I,-II,-III )
I want to put each array's first row in one array, like this:
Array( [0] => A-I-only [1] =>B-III-only [2] => C-I-and-II-only [3] => D-II-and-III-only [4] => E-I,-II,-III )
Is there a way to do it?
Use can use array merge function. Like:
$array = array_merge($array1,$array2,...);
Please note though that this would not work properly if your common indices were of string type (there would be a value overriding). Take a look here for more.
$array0 = array('A-I-only');
$array1 = array('B-III-only');
$array2 = array('C-I-and-II-only');
$array3 = array('D-II-and-III-only');
$array4 = array('E-I,-II,-III');
$result = array_merge($array0, $array1, $array2, $array3,
$array4);
print_r($result);
The Above code will give result as below
Array
(
[0] => A-I-only
[1] => B-III-only
[2] => C-I-and-II-only
[3] => D-II-and-III-only
[4] => E-I,-II,-III
)
I have an array like this:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
)
)
[1] => Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
[id] => 5678
[name] => Sally
)
[2] => Array
(
[id] => 1234
[name] => Duke
)
)
My resulting array should be the following (basically merging and getting rid of duplicates and removing null values):
Array
(
[0] => Array
(
[id] => 1234
[name] => John
)
[1] => Array
(
[id] => 5678
[name] => Sally
)
[2] => Array
(
[id] => 1234
[name] => Duke
)
)
Is there an easy way to do this using PHP's array functions?
EDIT: So far I have this:
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $value);
}
}
print_r($result);
Use array_merge to merge the arrays. Then use array_unique to remove duplicates. Finally, to remove null keys, use a loop.
foreach($array as $key=>$value)
{
if(is_null($value) || $value == '')
unset($array[$key]);
}
Documentation on these methods can be found here.
Flatten your unnecessarily depth array structure by unpacking the subarrays into a merge call.
Filter out the empty subarrays with array_filter().
Remove the duplicate subarrays with array_unique(). (SORT_REGULAR prevents "Warning: Array to string conversion")
Code: (Demo)
var_export(
array_unique(
array_filter(
array_merge(...$array)
),
SORT_REGULAR
)
);
Optionally, call array_values() on the resultant array to re-index it (remove the old keys).