PHP array diff different types - php

I have 2 arrays of data from different data sources in different formats, but they represent the same resources. So id in one is the same as guid in the other for example.
Currently I am converting one of the arrays to match the other, and then running them via array_udiff to get the difference.
However, I need to compare 3 properties to check if they are a match, so I can't return -1,0,1 as the 3 fields either match, or do not match.
If I simply return -1 or 0, it works comparing $a to $b, but fails comparing $b to $a
$arr_a = [['id'=>1, 'a'=>1, 'b'=>0],['id'=>2, 'a'=>2, 'b'=>3],['id'=>3, 'a'=>1, 'b'=>0]];
$arr_b = [['id'=>3, 'a'=>1, 'b'=>0],['id'=>4, 'a'=>2, 'b'=>3],['id'=>5, 'a'=>1, 'b'=>0]];
function diff($a, $b) {
if( ($a['id'] == $b['id'])
&& ($a['a'] == $b['a'])
&& ($a['b'] == $b['b'])
) {
return 0;
} else {
return -1;
}
$not_in_b = array_udiff($arr_a, $arr_b,'diff');
$not_in_a = array_udiff($arr_b, $arr_a,'diff');
print_r($not_in_b);
print_r($not_in_a);
The above returns...
Array
(
[0] => Array
(
[id] => 1
[a] => 1
[b] => 0
)
[1] => Array
(
[id] => 2
[a] => 2
[b] => 3
)
)
Array
(
[0] => Array
(
[id] => 3
[a] => 1
[b] => 0
)
[1] => Array
(
[id] => 4
[a] => 2
[b] => 3
)
[2] => Array
(
[id] => 5
[a] => 1
[b] => 0
)
)
As you can see the diff of $a to $b works, but $b to $a does not...
How can I compare multiple vaules like this for equality...
UPDATE
This works, but making two arrays with the three identifying properties values as the keys...
$arr_a = [['id'=>1, 'a'=>1, 'b'=>0],['id'=>2, 'a'=>2, 'b'=>3],['id'=>3, 'a'=>1, 'b'=>0]];
$arr_b = [['id'=>3, 'a'=>1, 'b'=>0],['id'=>4, 'a'=>2, 'b'=>3],['id'=>5, 'a'=>1, 'b'=>0]];
$arra_a_keys=[];
foreach($arr_a as $item) {
$arra_a_keys[$item['id'].'_'.$item['a'].'_'.$item['b']] = $item;
}
$arra_b_keys=[];
foreach($arr_b as $item) {
$arra_b_keys[$item['id'].'_'.$item['a'].'_'.$item['b']] = $item;
}
$not_in_b = array_diff_key($arra_a_keys, $arra_b_keys);
$not_in_a = array_diff_key($arra_b_keys, $arra_a_keys);
print_r($not_in_b);
print_r($not_in_a);

To compare by ids only you can do the following:
$ids = array_column($a, 'id');
$guids = array_column($b, 'guid');
$not_in_b = array_filter($a, function ($item) use ($guids) {
return !in_array($item['id'], $guids);
});
$not_in_a = array_filter($b, function ($item) use ($ids) {
return !in_array($item['guid'], $ids);
});
Here is working demo.
Addition:
Also, you can do it with array_udiff:
$compareFunction = function ($a, $b) {
$id1 = isset($a['id']) ? $a['id'] : $a['guid'];
$id2 = isset($b['id']) ? $b['id'] : $b['guid'];
return strcmp($id1, $id2);
};
$not_in_b = array_udiff($a, $b, $compareFunction);
$not_in_a = array_udiff($b, $a, $compareFunction);
Here is working demo.
But be aware array_udiff is really not the most straight forward function. There is nothing about this in the documentation, but it not only compares but also sorts the arrays you provided with a callback function. That's why
The comparison function must return an integer less than, equal to, or
greater than zero if the first argument is considered to be
respectively less than, equal to, or greater than the second.
But this sorting also tricks the programmer, because he expected to that in function int callback ( mixed $a, mixed $b ) $a comes from $array1 and $b comes from $array2. That is not the case. You can read this article to find out more details. So I think that array_filter solution is more understandable

You could use a foreach for make array comparable
$ids1 = [];
foreach($a as $v1){
$i1[] = $v1['id'];
}
$ids2 = [];
foreach($b as $v2){
$i2[] = $v2['guid'];
}
$one_notin_two = array_diff($i1,$i2);
$two_notin_one = array_diff($i2,$i1);

You can solve this using array_column() and array_diff().
Here is sample code: See Live Demo
$a = [['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]];
$b = [['guid' => 3], ['guid' => 4], ['guid' => 6], ['guid' => 7]];
$a = array_column($a, 'id');
$b = array_column($b, 'guid');
$not_in_b = array_diff($a, $b);
$not_in_a = array_diff($b, $a);
Hope it should be solved your problem. Thank you.

Related

PHP: How to move array item to the first place without changing its key if value is numeric?

Here is my array:
$arrayA = array(0 => "someString",
1 => "otherString",
2 => "2017",
3 => "anotherString",
4 => "2016");
My goal is to to find the first item that has a numeric value (which would be "2017") and place it first in the array, without changing its original key and keeping the others in the same order.
So I want:
$arrayA = array(2 => "2017",
0 => "someString",
1 => "otherString",
3 => "anotherString",
4 => "2016");
I tried uasort() php function and it seems the way to do it, but I could not figure out how to build the comparison function to go with it.
PHP documentation shows an example:
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
But, WHO is $a and WHO is $b?
Well, I tried
function my_sort($a,$b) {
if ($a == $b ) {
return 0;
}
if (is_numeric($a) && !is_numeric($b)) {
return -1;
break;
}
}
But, of course, I am very far from my goal. Any help would be much appreciated.
You don't need to sort per se. Once you find the element in question, you can simply push it onto the front of the array with the + operator:
foreach ($arrayA as $k => $v) {
if (is_numeric($v)) {
$arrayA = [$k => $v] + $arrayA;
break;
}
}
print_r($arrayA);
Yields:
Array
(
[2] => 2017
[0] => someString
[1] => otherString
[3] => anotherString
[4] => 2016
)

Sorting of multidimensional array with with numbers and letters

How to sort multidimensional array. This is what my array looks like
[0] => Array
(
[id] => 1
[title] => 3A
[active] => 1
)
[1] => Array
(
[id] => 1
[title] => A
[active] => 1
)
[2] => Array
(
[id] => 1
[title] => 2A
[active] => 1
)
[3] => Array
(
[id] => 1
[title] => B
[active] => 1
)
I have tried several usort methods, but cannot seem to get this to work. I am needing the array sorted so that it will sort by numeric then by alpha numeric like so: A,B,2A,3A.
I am not sure if this would be possible without adding a position field to dictate what order the titles are suppose to be in, or am I missing something here?
You can build a "key" for each item where the digit part is padded on the left with 0s, this way, the sort function can perform a simple string comparison:
$temp = [];
foreach ($arr as $v) {
$key = sscanf($v['title'], '%d%s');
if (empty($key[0])) $key = [ 0, $v['title'] ];
$key = vsprintf("%06d%s", $key);
$temp[$key] = $v;
}
ksort($temp);
$result = array_values($temp);
demo
This technique is called a "Schwartzian Transform".
As #Kargfen said, you can use usort with your custom function. Like this one :
usort($array, function(array $itemA, array $itemB) {
return myCustomCmp($itemA['title'], $itemB['title']);
});
function myCustomCmp($titleA, $titleB) {
$firstLetterA = substr($titleA, 0, 1);
$firstLetterB = substr($titleB, 0, 1);
//Compare letter with letter or number with number -> use classic sort
if((is_numeric($firstLetterA) && is_numeric($firstLetterB)) ||
(!is_numeric($firstLetterA) && !is_numeric($firstLetterB)) ||
($firstLetterA === $firstLetterB)
) {
return strcmp($firstLetterA, $firstLetterB);
}
//Letters first, numbers after
if(! is_numeric($firstLetterA)) {
return -1;
}
return 1;
}
This compare-function is just based on the first letter of your titles, but it can do the job ;-)
You can resolve that problem with help of usort and custom callback:
function customSort($a, $b)
{
if ($a['id'] == $b['id']) {
//if there is no number at the beginning of the title, I add '1' to temporary variable
$aTitle = is_numeric($a['title'][0]) ? $a['title'] : ('1' . $a['title']);
$bTitle = is_numeric($b['title'][0]) ? $b['title'] : ('1' . $b['title']);
if ($aTitle != $bTitle) {
return ($aTitle < $bTitle) ? -1 : 1;
}
return 0;
}
return ($a['id'] < $b['id']) ? -1 : 1;
}
usort($array, "customSort");
At first the function compares 'id' values and then if both items are equal it checks 'title' values.

PHP sort array substring

I want to sort this array by year:
Array
(
[0] => data/pictures/alice/1980
[1] => data/pictures/alice/1985
[2] => data/pictures/bob/1981
[3] => data/pictures/bob/1985
[4] => data/pictures/bob/1987
[5] => data/pictures/bob/1989
)
Expected result:
Array
(
[0] => data/pictures/alice/1980
[1] => data/pictures/bob/1981
[2] => data/pictures/alice/1985
[3] => data/pictures/bob/1985
[4] => data/pictures/bob/1987
[5] => data/pictures/bob/1989
)
I've already tried different sort functions without success.
Example:
asort($paths, SORT_STRING | SORT_FLAG_CASE);
sort($path, SORT_NUMERIC);
Since it's a path just map the array through basename() and then sort based on that:
array_multisort(array_map('basename', $paths), SORT_ASC, $paths);
Try this
function cmp($a, $b) {
// if equal, don't do much
if ($a == $b) {
return 0;
}
$explodedA = explode('/', $a);
$explodedB = explode('/', $b);
$yearPartA = $explodedA[count($explodedA) - 1];
$yearPartB = $explodedB[count($explodedB) - 1];
if ($explodedPartA == $explodedPartB) { // compare full string
return ($a < $b) ? -1 : 1;
}
return ($yearPartA < $yearPartB) ? -1 : 1;
}
// actual sort of the array $path (e.g. the whole point)
usort($path, "cmp");
Consider, however that you'd probably be doing 'explode' several times for each array element and that it might be cheaper to work a bit on the array first. Not sure how big your array is... Do some testing.
$array = ['data/pictures/alice/1980','data/pictures/alice/1985','data/pictures/bob/1981','data/pictures/bob/1985','data/pictures/bob/1987','data/pictures/bob/1989'];
uasort($array, function($a,$b) {
$y1 = array_pop(explode('/', $a));
$y2 = array_pop(explode('/', $b));
if($y1===$y2) {
// if year the same use other criteria
if($a===$b) {
return 0;
}
return $a>$b?-1:1;
};
return $y1>$y2?-1:1;
});
Use usort and in the custom function explode the strings by "/" and compare the last parts of the arrays.

Can I determine PHP array value deltas?

I have two multidimensional arrays that I need to determine the delta for each value. I know the array_diff function only returns the difference in keys. Is there a functon that will determine the delta for each set of values assuming the two arrays contain the same set of keys?
Example:
array_1(test1 => Array([key1] => 100, [key2] => 200 ) )
array_2(test1 => Array([key1] => 105, [key2] => 195 ) )
I would expect something like:
array_3(test1 => Array([key1] => 5, [key2] => -5 ) )
Are there any PHP methods to do this or am I on my own?
Answers here suggested using foreach loop but I think creating anonymous function will be easier:
<?php
$count_delta = create_function('$a,$b', 'return $a - $b;');
$arr1 = array(100, 200);
$arr2 = array(20, 180);
$delta = array_map($count_delta, $arr1, $arr2);
var_dump($delta);
Output will be:
array
0 => int 80
1 => int 20
$delta = array();
foreach( $array1 as $k=>$v )
{
if( array_key_exists( $k, $array2 )
{
// preserve the key
$delta[$k] = $array1[$k] - $array2[$k];
// or don't
$delta[] = $array1[$k] - $array2[$k];
}
}
print_r($delta);
There is no built-in function for that, but you can use this.
function delta_array($a, $b) {
if (sizeof($a) != sizeof($b))
return false;
$arr = array();
for ($i=0; $i < $c = sizeof($a); $i++)
$arr[] = $b[$i] - $a[$i];
return $arr;
}
$arr1 = array(100,200);
$arr2 = array(105,195);
$delta = delta_array($arr1, $arr2);
print_r($delta);
The above will return
Array
(
[0] => -5
[1] => 5
)

Combine arrays to form multidimensional array in php

I know there's a ton of answers but I can't seem to get it right. I have the following arrays and what I've tried:
$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );
$e = array_combine($a, array($b,$c,$d) );
But with this I get the error array_combine() [function.array-combine]: Both parameters should have an equal number of elements.
I know it's because the $a's array values aren't keys. That's why I'm coming here to see if I could get some help with an answer that can help me make it look something like this:
array(2) {
[1421]=>array( [0] => teststring1
[1] => teststring3
[2] => teststring5
)
[2241]=>array( [0] => teststring2
[1] => teststring4
[2] => teststring6
)
}
If you have control over creating the arrays, you should create them like:
$a = array ('1421' ,'2241');
$b = array ('teststring1', 'teststring3', 'teststring5');
$c = array ('teststring2', 'teststring4', 'teststring6');
$e = array_combine($a, array($b,$c) );
If not, you have to loop over them:
$result = array();
$values = array($b, $c, $d);
foreach($a as $index => $key) {
$t = array();
foreach($values as $value) {
$t[] = $value[$index];
}
$result[$key] = $t;
}
DEMO
Here is a one-liner in a functional coding style. Calling array_map() with a null function parameter followed by the "values" arrays will generate the desired subarray structures. array_combine() does the key=>value associations.
Code (Demo)
var_export(array_combine($a, array_map(null, $b, $c, $d)));
Output:
array (
1421 =>
array (
0 => 'teststring1',
1 => 'teststring3',
2 => 'teststring5',
),
2241 =>
array (
0 => 'teststring2',
1 => 'teststring4',
2 => 'teststring6',
),
)
Super clean, right? I know. It's a useful little trick when you don't have control of the initial array generation step.
Here's a new version of array_merge_recursive which will handle integer keys. Let know how it goes.
$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );
$e = array_combine($a, array_merge_recursive2($b, $c, $d));
echo "<pre>";
print_r($e);
function array_merge_recursive2() {
$args = func_get_args();
$ret = array();
foreach ($args as $arr) {
if(is_array($arr)) {
foreach ($arr as $key => $val) {
$ret[$key][] = $val;
}
}
}
return $ret;
}

Categories