how can I compare these two arrays?
I want the result be elements that are not available in one array. For example:
Array #1
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 3
)
)
Array #2
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
)
Result
Array
(
[0] => Array
(
[0] => c
[1] => 3
)
)
In set theory this is call symmetrical difference.
First you need to remove any possible duplicates, then get the individual differences for both arrays and the merge those two individual differences.
$a = array('John', 'Bob', 'Mary', 'Serena');
$b = array('Jim', 'Mary', 'John', 'Bob');
// Remove any duplicates
$a = array_unique($a);
$b = array_unique($b);
// Get the individual differences, using array_diff()
$a_minus_b = array_diff($a, $b);
$b_minus_a = array_diff($b, $a);
// Simply merge them together to get the symmetric difference
$symmetric_difference = array_merge($a_minus_b, $b_minus_a); // produces ['Serena', 'Jim']
You may need to make the necessary changes for a 2D array if you need to.
I hope this help.
Related
I have 2 arrays as below. I want to remove data from array2 if array1 has the stu_id. final array should be like result_array.
$array1 = Array
(
[0] => Array
(
[stu_id] => 1
[name] => mr.a
)
[1] => Array
(
[stu_id] => 3
[name] => mr.b
)
)
$array2 = Array
(
[0] => Array
(
[id] => 1
[stu_id] => 1
[data] => abc
)
[1] => Array
(
[id] => 2
[stu_id] => 2
[data] => xyz
)
[3] => Array
(
[id] => 3
[stu_id] => 3
[data] => aaa
)
)
$result_array = Array
(
[0] => Array
(
[id] => 2
[stu_id] => 2
[data] => xyz
)
)
I tried array_diff, $result_array = array_diff($array2, $array1); but it's not working.
Please help me to do this.
Temporarily assign keys using array_column() with stud_id (NULL retains the full subarray data), then use array_diff_key() to filter, and array_values() to reset the keys:
Code: (Demo)
$array1=[
['stu_id'=>1,'name'=>'mr.a'],
['stu_id'=>3,'name'=>'mr.b']
];
$array2=[
['id'=>1,'stu_id'=>1,'data'=>'abc'],
['id'=>2,'stu_id'=>2,'data'=>'xyz'],
['id'=>3,'stu_id'=>3,'data'=>'aaa']
];
//var_export(array_column($array1,NULL,'stu_id'));
//var_export(array_column($array2,NULL,'stu_id'));
var_export(array_values(array_diff_key(array_column($array2,NULL,'stu_id'),array_column($array1,NULL,'stu_id'))));
Output:
array (
0 =>
array (
'id' => 2,
'stu_id' => 2,
'data' => 'xyz',
),
)
If you'd like to use a foreach loop structure, generate a filtering array of stu_id's from $array1 and write a conditional check on each iteration of $array2. This method doesn't not modify the original arrays, so they can be reused "down script".
Code:
$stu_ids=array_column($array1,'stu_id');
foreach($array2 as $row){
if(!in_array($row['stu_id'],$stu_ids)){
$result[]=$row; // auto-index the qualifying subarrays
}
}
var_export($result);
// same result as above method
foreach($array1 as $data1){
foreach($array2 as $k => $data2){
if($data2["stu_id"] == $data1["stu_id"]){
unset($array2[$k]);
break;
}
}
}
$result_array = $array2;
How can I do the below change in PHP?
Input:
[hiddenAllPrefered] => Array
(
[0] => 14477,14478,14479,14485,14486,14487
)
Output should be like this:
[hiddenAllPrefered] => Array
(
[0] => 14477,14478,14479
[1] => 14485,14486,14487
)
A possible solution:
$input = array('14477,14478,14479,14485,14486,14487');
$output = array_map(
function (array $a){
return implode(',', $a);
},
array_chunk(
explode(',', $input[0]),
3
)
);
Read it from inside out:
explode() splits the string $input[0] using comma (,) as delimiter and returns an array;
array_chunk() splits the array into chunks of size 3; it returns an array of arrays, each inner array contains 3 elements (apart from the last one that can contain less);
array_map() applies the function it receives as its first argument to each value of the array it gets as its second argument (the array of arrays returned by array_chunk()); it returns an array whose values are the values returned by the function;
the anonymous function passed to array_map() gets an array (of size 3 or less) and uses implode() to join its elements into a string, using comma (,) to separate the values and returns the string;
array_map() puts together all the values returned by the anonymous function (one for each chunk of 3 elements of the array) into a new array it returns.
The output (print_r($output)) looks like this:
Array
(
[0] => 14477,14478,14479
[1] => 14485,14486,14487
)
try this as a boilerplate
function chunker($arr, $l) {
return array_chunk($arr, $l);
}
print_r(chunker($hap, 3));
/*
Array
(
[0] => Array
(
[0] => 14477
[1] => 14478
[2] => 14479
)
[1] => Array
(
[0] => 14485
[1] => 14486
[2] => 14487
)
)
*/
UPDATE
php > $h = [ "14477,14478,14479,14485,14486,14487" ];
php > $hap = explode(",", $h[0]);
php > print_r($hap);
Array
(
[0] => 14477
[1] => 14478
[2] => 14479
[3] => 14485
[4] => 14486
[5] => 14487
)
php > print_r(chunker($hap, 3));
Array
(
[0] => Array
(
[0] => 14477
[1] => 14478
[2] => 14479
)
[1] => Array
(
[0] => 14485
[1] => 14486
[2] => 14487
)
)
php >
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
)
Hi I have below multidimensional arrays -
Array
(
[user_attempts] => 0
[2] => Array
(
[0] => 1
[1] => 4
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
)
[1] => Array
(
[0] => Earth
)
)
and my second array is like below
Array
(
[1] => Array
(
[0] => Earth
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
[1] => asdas
)
)
I need to intersect both multidimensional arrays - so the result would be
Array
(
[1] => Array
(
[0] => Earth
)
[3] => Array
(
[0] => 32
[1] => 23
)
)
Can anyone help me to sort this out.
What I have tried is using array_intersect() but it limits to single array not multidimensional i guess.
PHP comes with a ton of functions already built in, but sometimes you still have to implement things yourself. What you want to do can be easily done by using the existing functions.
The goal is to do the following steps:
Find the keys that exist in both arrays
Loop through the array using these keys
Take the items of both input arrays with each of these keys
Calculate the intersection of those two arrays
Put it into a result array
Here is one way to do this:
function array_intersect_2dim (array $a1, array $a2) {
$keys = array_intersect(array_keys($a1), array_keys($a2));
$return = array();
foreach ($keys as $key) {
$return[$key] = array_intersect($a1[$key], $a2[$key]);
if (sizeof($return[$key]) == 0) {
unset($return[$key]);
}
}
return $return;
}
It works only for two dimensions. If you need more, you have to build a recursive approach, which follows the exact same principle.
To make the easier to compare you can use serialize/unserialize on this one. And then use array_intersect(). Try this example: Sample Output
$array1 = array( 'user_attemps' => 0, 2 => array(1, 4), 3 => array(32, 23), 4 => array('asdsa'), 1 => array('Earth'),);
$array2 = array( 1 => array('Earth'), 2 => array(2, 3), 3 => array(32, 23), 4 => array('asdsa', 'asdas'),);
$result = array_map('unserialize',array_intersect(array_map('serialize', $array1), array_map('serialize', $array2)));
print_r($result);
i am having an array
Array
(
[1] => Array
(
[0] => 1
[1] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 1
[1] => 3
)
)
and i need to find the common subarrays
In the above example array 1 and 3 have the common sub array
(
[0] => 1
[1] => 3
)
So the final array must be
Array
(
[1] => Array
(
[0] => 1
[1] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
)
)
But i need to count the common values some how.
Any suggestion.
I wouldn't use this for production code, but here's a quick & somewhat clever way to do it:
$arrays = array(array(1,3), array(1,2), array(1,3)); // Your example data
$serialized = array_map('serialize', $arrays);
$counts = array_count_values($serialized);
foreach ($counts as $data => $count) {
echo "$count: " . print_r(unserialize($data), true);
}
Just compare each element of array with other assuming them as a linear array but use array_diff to compare each element. If they are different copy the element or array index into another array
To isolate unique rows, use the SORT_REGULAR flag with array_unique().
To acquire the number of duplicated rows, subtract the unique count from the initial count.
Code: (Demo)
$array = [[1, 3], [1, 2], [1, 3]];
$count = count($array);
$unique = array_unique($array, SORT_REGULAR);
echo "Number of duplicated rows: " . ($count - count($unique));