Check if array contain other array (for multidimensional array) php [duplicate] - php

This question already has answers here:
PHP: Check if an array contains all array values from another array
(5 answers)
Closed 4 years ago.
I am looking forward to compare two arrays in PHP.
For example, I have array A:
Array
(
[0] => Array
(
[option_id] => 19
[sub_option_id] => 57
)
[1] => Array
(
[option_id] => 1093
[sub_option_id] => 3582
)
[2] => Array
(
[option_id] => 1093
[sub_option_id] => 57
)
)
And array B:
Array
(
[0] => Array
(
[order_option_detail] => Array
(
[0] => Array
(
[option_id] => 19
[sub_option_id] => 57
)
[1] => Array
(
[option_id] => 1093
[sub_option_id] => 57
)
[2] => Array
(
[option_id] => 1093
[sub_option_id] => 3582
)
)
)
[1] => Array
(
[order_option_detail] => Array
(
[0] => Array
(
[option_id] => 1
[sub_option_id] => 2
)
)
)
)
By looking at the data structure, I can see that array B contains array A. How can I achieve the same analysis using PHP, ie how to check array B contain array A?
Please help me if you know!
Thank you so much!

You can use the following function for array compare:
function array_equal($a, $b) {
if (!is_array($a) || !is_array($b) || count($a) != count($b))
return false;
$a = array_map("json_encode", $a);
$b = array_map("json_encode", $b);
return array_diff($a, $b) === array_diff($b, $a); // mean both the same values
}
And then use it as:
$details = array_column($arrayB, 'order_option_detail');
foreach($details as $detail){ // loop the two items.
if (array_equal($detail, $arrayA)) {
// Do what ever
}
}

From arrayB you only need 'order_option_detail'.
So if we use array_column we can get those isolated.
$details = array_column($arrayB, 'order_option_detail');
foreach($details as $detail){ // loop the two items.
if($detail === $arrayA){
// Do something
}
}
https://3v4l.org/TW670

Related

How to remove duplicate values from a multi dimensional array for specific key in php [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 2 years ago.
I searched for solutions on here but didn't find one for my use case.
I have a big array which is built like this example:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[2] => Array
(
[Template] => page.html5
)
[3] => Array
(
[Template] => page2.html5
)
[4] => Array
(
[Template] => page.html5
)
[5] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
I would like to remove all duplicate values for the array key "Template", but besides that I want the array to stay the way it is.
So afterwards my Array should look like:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
Is there a way to achieve this without using lots of memory?
Thanks for your answers :)
You could use the following logic, which uses:
array_map() to flatten the array with index keys-values, and serialize() (stringify) the last array element so we can use
array_unique() on the result.
Then, to restore the stringified array, i.e. turn it back into an array, we use unserialize().
<?php
$newArr = array_unique(array_map(function ($el) {
return $el['Template'] ?? serialize($el);
}, $arr));
// restore the last element to array
$last = array_key_last($newArr); // (PHP 7 >= 7.3.0)*
$newArr[$last] = unserialize($newArr[$last]);
*if PHP version <7.3.0 use: end($newArr); $last = key($newArr);
Output:
Array
(
[0] => page.html5
[1] => page2.html5
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
working demo
The code below loops the array, marks indexes for removal and then another loop does the removals:
$templates = array(); //This will store the remove plan
for ($index = 0; $index < count($input); $index++) {
if (isset($input[$index]["Template"])) { //Ignore items where there is no template
if (isset($templates[$input[$index]["Template"]])) { //Let's check whether we have already seen this template
$templates[$input[$index]["Template"]] = array(); //From now on we will find duplicates for this dude
} else { //Mark for removal
$templates[$input[$index]["Template"]][]=$index;
}
}
}
//Actual removals
foreach($templates => $index) {
//Removing the actual element:
unset($input[$index]["Template"]);
//Remove the parent as well if it becomes empty
if (!count($input[$index])) unset($input[$index]);
}
The memory need for this algorithm is:
average(element_size) * number_of_elements

How to insert data on array multidimensional in PHP 7 [duplicate]

Following is the output of my multidimensional array $csmap_data
Array
(
[0] => Array
(
[cs_map_id] => 84
[cs_subject_id] => 1
)
[1] => Array
(
[cs_map_id] => 85
[cs_subject_id] => 5
)
[flag] => 1
)
Initially there was no [flag] => 1 key-value in the array, I added it to the array $csmap_data.
But I want to add the [flag] => 1 in the above two array elements, not as a separate array element. In short I wanted following output :
Array
(
[0] => Array
(
[cs_map_id] => 84
[cs_subject_id] => 1
[flag] => 1
)
[1] => Array
(
[cs_map_id] => 85
[cs_subject_id] => 5
[flag] => 1
)
)
The code I was trying to achieve this is as follows, but couldn't get the desired output:
if (!empty($csmap_data)) {
foreach($csmap_data as $csm) {
$chapter_csmap_details = $objClassSubjects->IsClassSubjectHasChapters($csm['cs_map_id']);
$csmap_data ['flag'] = 1;
}
}
Can anyone help me out in obtaining the desired output as I depicted? Thanks in advance.
<?
foreach($csmap_data as $key => $csm)
{
$csmap_data[$key]['flag'] = 1;
}
That should do the trick.
You can also do it using php array functions
$csmap_data = array_map(function($arr){
return $arr + ['flag' => 1];
}, $csmap_data);
UPDATE:
to use multiple variables in callback function of array_map function we can do it by use
$flagValue = 1;
$csmap_data = array_map(function($arr) use ($flagValue){
return $arr + ['flag' => $flagValue];
}, $csmap_data);

How to intersect Array of objects in PHP?

How do i get the result of intersection between two Array of Objects in PHP.
For Example,
the value of $array1 is
Array
(
[0] => stdClass Object
(
[id] => 2
[influencer_id] => 2
[follower_id] => 1
)
)
and the value of $array2 is,
Array
(
[0] => stdClass Object
(
[id] => 2
[influencer_id] => 1
[follower_id] => 2
),
[1] => stdClass Object
(
[id] => 3
[influencer_id] => 3
[follower_id] => 2
),
)
So, what i want to get in $result is
Array
(
[0] => stdClass Object
(
[id] => 2
[influencer_id] => 2
[follower_id] => 1
)
)
What is the best way to get it?
Thanks in advance!
You can do that using array_uintersect function and defining manually your callback comparison function :
$arr1 = json_decode('[{"id":2,"influencer_id":2,"follower_id":1}]');
$arr2 = json_decode('[{"id":2,"influencer_id":2,"follower_id":1},{"id":3,"influencer_id":3,"follower_id":2}]');
$arr3 = array_uintersect($arr1, $arr2, function ($e1, $e2) {
if($e1->id == $e2->id && $e1->influencer_id == $e2->influencer_id && $e1->follower_id == $e2->follower_id) {
return 0;
} else {
return 1;
}
});
var_dump($arr3);
Try to use array_intersect
array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

Sorting this multidimention array in php [duplicate]

This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 8 years ago.
I have tried this problem
how do you sort this array using the value that is elements in [1].
I would also appreciate if someone demonstrate how print each key and it's value of this
array
Array
(
[0] => Array
(
[0] => 9
[1] => 0
)
[1] => Array
(
[0] => 10
[1] => 290
)
[2] => Array
(
[0] => 12
[1] => 852
)
[3] => Array
(
[0] => 13
[1] => 9
)
[4] => Array
(
[0] => 14
[1] => 896
)
)
please help
You could use uasort
function cmp($a, $b) {
if ($a[1] == $b[1]) {
return 0;
}
return ($a[1] < $b[1]) ? -1 : 1;
}
uasort($array, 'cmp');
To print each key, value... just iterate over it with a foreach

How do I sort an array by a specific value inside of it? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sorting an array based on its value
I want to sort an array by a value inside of it. I tried usort but it leads to some unexpected results in actually changing the value of the output instead of just shifting elements in the array.
Below is the array I want to sort:
Array
(
[element1] => Array
(
[Total] => 1
[paTotal] => 0
[totalPreregistrations] => 7
[totalPreregistrationsToDate] => 26
[pas] => Array
(
[0] => Array
(
[id] => 119
)
)
)
[element2] => Array
(
[Total] => 1
[paTotal] => 0
[totalPreregistrations] => 0
[totalPreregistrationsToDate] => 58
[pas] => Array
(
[0] => Array
(
[id] => 107
)
)
)
... element3, 4, etc...
I want to sort by the "totalPreregistrations" number so that element2 goes above element1 if element2's totalPreregistrations count goes above element1's.
And of course I want the sub-arrays to be retained as well.
Thank you!
Documentation: uasort()
function mySort($a, $b) {
if( $a['totalPreregistrations'] == $b['totalPreregistrations'] ) {
return 0;
} else if( $a['totalPreregistrations'] > $b['totalPreregistrations'] ) {
return 1;
} else {
return -1;
}
}
uasort($array, 'mySort');
You can use uasort instead of usort
uasort($array, function ($a, $b) {
$a = $a['totalPreregistrations'];
$b = $b['totalPreregistrations'];
return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
});
echo "<pre>";
print_r($array);
Output
Array
(
[element2] => Array <-------------------------- element key remains intact
(
[Total] => 1
[paTotal] => 0
[totalPreregistrations] => 0
[totalPreregistrationsToDate] => 58
[pas] => Array
(
[0] => Array
(
[id] => 107
)
)
)
[element1] => Array
(
[Total] => 1
[paTotal] => 0
[totalPreregistrations] => 7
[totalPreregistrationsToDate] => 26
[pas] => Array
(
[0] => Array
(
[id] => 119
)
)
)
)

Categories