How i can Find Difference of arrays Having different Format - php

I am trying two Find Difference in two array Both of array are in different Format
First Array
Array
(
[0] => Array
(
[value] => /m/b/mb01-blue-0.jpg
)
[1] => Array
(
[value] => /m/b/mb04-black-0.jpg
)
[2] => Array
(
[value] => /m/b/mb04-black-0.jpg
)
}
Second Array
Array
(
[0] => /m/b/mb01-blue-0.jpg
[1] => /m/b/mb04-green-0.jpg
[2] => /m/b/mb04-blue-0.jpg
}
I want both of Arrays is a same format to use array_diff() function in php

Get the value column of your first array through array_column(), then check the difference between that result and the second array with array_diff().
$diff = array_diff(array_column($first, 'value'), $second);
Live demo at https://3v4l.org/19tgb

You may try this:
$array_1 = array(
"0"=> array("value" => "/m/b/mb01-blue-0.jpg"),
"1"=> array("value" => "/m/b/mb04-black-0.jpg"),
"2"=> array("value" => "/m/b/mb04-black-0.jpg"),
);
$result_1 = array_column($array_1,"value");
print_r($result_1);
To get the value of column specific from the array use array_column() php -function

Here is one-liner with array_shift,
$arr = array_map("array_shift", $arr);
array_shift - Shift an element off the beginning of array
Demo
Output:-
Array
(
[0] => /m/b/mb01-blue-0.jpg
[1] => /m/b/mb04-black-0.jpg
[2] => /m/b/mb04-black-0.jpg
)

Related

Unwrap array inside another array PHP

I am getting a string separated from commas and I am trying to split them into an array, which works. The only problem is that there is an outer array wrapping the array I want to use. So I don't want to use the $excludes[0] when passing the array to a function. Does anyone know a function I can use to unwrap the array inside $excludes[0]
$excludes = [];
Array ( [0] => Array (
[0] => company_logo,
[1] => social_links,
[2] => rss_link,
[3] => telephone ) )
My expected results would be the below.
Array (
[0] => company_logo,
[1] => social_links,
[2] => rss_link,
[3] => telephone
)
You can simple do :
1st Option:
print_r(array_shift($excludes));
2nd Option:
$new_array = $excludes[0];
print_r($new_array);
Hope this will work
check this
$excludes = Array ( 0 => Array (
0 => 'company_logo',
1 => 'social_links',
2 => 'rss_link',
3 => 'telephone' ) ) ;
$excludes=$excludes[0];
print_r($excludes);die();

PHP using one array as keys for subarray of multidimensional array

So i got one single dimensional array like:
Array
(
[0] => md5
[1] => name
[2] => description
[3] => url
)
and one multidimensional array:
Array
(
[0] => Array
(
[0] => md5#1
[1] => name1
[2] => desc1
[3] => url1
)
[1] => Array
(
[0] => md5#2
[1] => name2
[2] => desc2
[3] => url2
)
)
and I want to use values of first array as keys for subarrays of the multidimensional one, so the output should look like:
Array
(
[0] => Array
(
[md5] => md5#1
[name] => name1
[desription] => desc1
[url] => url1
)
[1] => Array
(
[md5] => md5#2
[name] => name2
[description] => desc2
[url] => url2
)
)
Alternatively(as a bit offtopic question), how can I sort the elements of multidimensional array by values of md5 if they keys of subarray are not [md5] but [0]?
Thanks in advance!
For combining values of two arrays where values of one are keys, and values of other are values, use array_combine function:
$keysArray = [];
$multiArray = [];
$result_array = [];
foreach ($multiArray as $value) {
$result_array[] = array_combine($keysArray, $value);
}
For sorting - use usort and define your custom function:
usort($result_array, function($a, $b) { return strcmp($a['md5'], $b['md5']); });
Here is the functional-style equivalent of #u_mulder's answer.
This technique merely applies the array of key names as the keys for every row in the array.
Using arrow function syntax (available since PHP7.4), allows the custom callback to access the globally scoped $keys array without needing to explicitly write use($keys) in the function declaration.
Code: (Demo)
$first = ['md5#1', 'name1', 'description1', 'url1'];
$second = ['md5#2', 'name2', 'description2', 'url2'];
$multidimArray = [$first, $second];
$keys = ['md5', 'name', 'description', 'url'];
var_export(
array_map(
fn($row) => array_combine($keys, $row),
$multidimArray
)
);

Can I split an array of this format to this?

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 >

Return the difference of multi-dimensional arrays

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.

Compare Two arrays array_diff_assoc() [duplicate]

This question already has answers here:
Compare 2-dimensional data sets based on a specified second level value
(9 answers)
Closed last year.
i need get value from 2 arrays...
First Array:
Array ( [0] => Array ( [id] => 1 [nombre_area] => biblioteca )
[1] => Array ( [id] => 2 [nombre_area] => enfermeria )
[2] => Array ( [id] => 3 [nombre_area] => talleres y laboratorios ) )
Second Array:
Array ( [0] => Array ( [0] => 1 [1] => biblioteca )
[1] => Array ( [0] => 3 [1] => talleres y laboratorios ) )
i need get the difference:
Array ( [0] => Array ( [id] => 2 [nombre_area] => enfermeria )
How can i do that ?
You are not operating on associative arrays at top level. You have two numeric arrays containing nested arrays. One of those contains associative arrays, the other one numeric arrays. First you could bring it in a normalized form, e.g. by $normalized = array_map( function($ar) { return array_values($ar); }, $array1 ); to the numeric form.
However, then you have two structures of the same form, but array_diff() won't perform a deep inspection. It will only compare a string representation of the elements at the first level. So you won't have another choice than recursively iterate the array, e.g. with help of the function array_walk_recursive().
You can try this one:
$array1 =Array (Array ( 'id' => 1, 'nombre_area' => 'biblioteca' ),Array ( 'id' => 2, 'nombre_area' => 'enfermeria' ),Array ( 'id' => 3 ,'nombre_area' => 'talleres y laboratorios' ) );
$array2 = Array (Array (1,'biblioteca' ), Array(3,'talleres y laboratorios' ));
$IDs = array_map(function($arr2){return $arr2[0];},$array2);
$result = array();
foreach($array1 as $arr1){
if(!in_array($arr1['id'],$IDs)) $result[] = $arr1; //compare id
}
print_r($result);

Categories