PHP check if array value occurs in all multidimensional arrays - php

I have the following multi dimensional PHP array. What I'm trying to do is return only the value which occurs in all of the arrays.
So in the array below the only value which occurs in every array is "2018-02-22", so I want to create a new array with only this value.
I feel like it can't be too difficult, but I just can't get my head around how to do this. If any one can help it would be much appreciated!
Array
(
[0] => Array
(
[0] => 2018-02-22
)
[1] => Array
(
[0] => 2018-02-22
[1] => 2018-02-21
[2] => 2018-02-20
[3] => 2018-02-16
[4] => 2018-02-14
)
[2] => Array
(
[0] => 2018-02-20
[1] => 2018-02-19
[2] => 2018-02-21
[3] => 2018-02-22
[4] => 2018-02-14
)
[3] => Array
(
[0] => 2018-02-22
[1] => 2018-02-12
[2] => 2018-02-01
)
)
So to clarify the output I'm aiming for is:
Array
(
[0] => 2018-02-22
)

You can do it by array_intersect, to get common value 2018-02-22.
<?php
$dates = [
["2018-02-22","2018-02-23"],
["2018-02-22","2018-02-24"],
["2018-02-22","2018-02-25"],
];
$common = array_shift($dates);
foreach($dates as $key=>$date){
$common = array_intersect($common, $date);
}
print_r($common);
?>
Live Demo
Output is :
Array (
[0] => 2018-02-22
)

Related

Remove duplicate data in multidimension array

I want to remove duplicate data in my array.
That's my array :
$monTableau = array (
array("pomme","noix de coco","pêche"),
array("fraise","pomme", "framboise"),
array("ananas","citron","raisin"),
array("pêche","pruneau","pomme")
);
My multidimensionnal array :
Array
(
[0] => Array
(
[0] => pomme
[1] => noix de coco
[2] => pêche
)
[1] => Array
(
[0] => fraise
[1] => pomme
[2] => framboise
)
[2] => Array
(
[0] => ananas
[1] => citron
[2] => raisin
)
[3] => Array
(
[0] => pêche
[1] => pruneau
[2] => pomme
)
)
and that's my code to try remove duplicate data:
$monTableau = array_map("unserialize", array_unique(array_map("serialize", $monTableau)));
Don't work unfortunately :(
Advance Thanks,
Your code does work properly, but there is no duplicates in the main array. If you want one single array as output that contains only unique elements of the whole list take a look at this code:
$monTableau = array (
array("pomme","noix de coco","pêche"),
array("fraise","pomme", "framboise"),
array("ananas","citron","raisin"),
array("pêche","pruneau","pomme")
);
$merged = call_user_func_array('array_merge', $monTableau);
$unique = array_unique($merged);
Output of $unique:
Array
(
[0] => pomme
[1] => noix de coco
[2] => pêche
[3] => fraise
[5] => framboise
[6] => ananas
[7] => citron
[8] => raisin
[10] => pruneau
)
Duplicates of pomme have been removed

Sort array by date

My array has an array for each field (i.e date, name, etc.). How do I sort the array by date? Should I create another array? Can I use sort or unsort here. If yes, how? Here is my array:
Array
(
[date] => Array
(
[0] => 03/11/2019
[1] => 03/19/2019
[2] => 03/15/2019
[3] => 12/15/2018
)
[name] => Array
(
[0] => Lowa
[1] => Stephanie
[2] => Allan
[3] => Joffer
)
[number] => Array
(
[0] => 178989898
[1] => 111111111
[2] => 222222222
[3] => 333333333
)
[unit] => Array
(
[0] => HR
[1] => VPP
[2] =>
[3] => OAT
)
[department] => Array
(
[0] => Chemistry
[1] => IT
[2] => Lab
[3] => Contractor
)
)
At the end, my first element will be:
03/19/2019 Stephanie 111111111 VPP IT
I think your data can be better organized:
$newArr = Array
(
[0] => Array
(
[date] => 03/11/2019
[name] => Lowa
[number] => 178989898
[unit] => HR
[department] => Chemistry
)
[1] => Array
(
[date] => 03/19/2019
[name] => Stephanie
[number] => 111111111
[unit] => VPP
[department] => IT
)
[2] => Array
(
[date] => 03/15/2019
[name] => Allan
[number] => 222222222
[unit] =>
[department] => Lab
)
[3] => Array
(
[date] => 12/15/2018
[name] => Joffer
[number] => 333333333
[unit] => OAT
[department] => Contractor
)
);
Then, you can simply sort it by:
function cmp($a, $b) {
if ($a["date"] == $b["date"]) return 0;
return ($a["date"] < $b["date"]) ? -1 : 1;
}
usort($newArr, "cmp");
Please be warned that dates in the format "Month/Day/Year" ar not alphabetically sortable.
You definitively should use a Year/Month/Day format for your dates, or write a more specific cmp() function...
UPDATE: To answer OP's question in comment: just reverse $row and 'field' order:
for ($row = 0; $row < count($date); $row++) {
$newArr[$row]['date'] = $date[$row];
$newArr[$row]['name'] = $name[$row];
...
}
First save the keys of your array - then by using array_value convert to integer keys so you can use the ... operator.
Then you can use array_filter with null function to re-organize your array. Next step will be to get the keys back using array_map and array_combine.
Last step - sort by "data" with usort
Consider the following example:
$arr = ["date" => ["3", "7", "5"], "name" => ["aa", "bb", "cc"]]; // this can have a lot more sub-array inside
$keys = array_keys($arr); // extract the keys for later use
$res = array_map(null, ...array_values($arr)); // transposed the array
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res); // return the keys to the transposed array
usort($res, function ($a, $b) {return strcmp($a['date'], $b['date']);} ); // sort all by "date"
Reference:
array-keys, array-filter, array-map, usort, array-values
Notice #MarcoS post comment regarding the comparing dates

array unique make item disapear

I have this array :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => 11
[id_list] => 2
[id] => 2
)
[1] => Array
(
[0] => 12
[id_list] => 2
[id] => 2
)
)
[2] => Array
(
[0] => Array
(
[0] => 13
[id_list] => 4
[id] => 4
)
)
)
and this code (where $dataListe is the result of a fetchAll query) :
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
// $resultUnique = array_unique($result);
echo "<pre>".print_r($result, true)."</pre>";
as you can see, there's some contact similar in my first and my second array (but contact can be the same in the 1st and the 3rd array, is I choose to add my contact in my 3rd array).
I want to remove the duplicate of each element in the general array.
But when I use array unique, I get this result :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
)
Please I need help to only keep 1 item of each array at the end !
EDIT : I have almost the good result with the code below, but the id 12 is missing
$result = [];
foreach($dataListe as $listeDiff){
foreach($listeDiff as $contact){
if(!in_array($contact,$result)){
$result[] = $contact;
}
break;
}
}
As the PHP docs says :
Note: Note that array_unique() is not intended to work on multi dimensional arrays. (http://php.net/manual/en/function.array-unique.php)
You can try this solution
$uniqueResult = array_map("unserialize", array_unique(
array_map("serialize", $result)
));
as suggested by #daveilers on this question How to remove duplicate values from a multi-dimensional array in PHP.

php formatting array results

I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}

How to sort a PHP by date

Here is my array:
Array (
[0] => Array ( [0] => content here [1] => 2010-02-04 01:25:34 )
[1] => Array ( [0] => content here [1] => 2010-02-04 04:51:37 )
[2] => Array ( [0] => content here [1] => 2010-02-04 04:52:31 )
[3] => Array ( [0] => content here [1] => 2010-02-04 05:50:48 )
[4] => Array ( [0] => content here [1] => 2010-02-04 03:25:34 )
[5] => Array ( [0] => content here [1] => 2010-02-04 05:39:33 )
[6] => Array ( [0] => content here [1] => 2010-02-04 03:25:34 )
[7] => Array ( [0] => content here [1] => 2010-02-04 07:07:09 )
[8] => Array ( [0] => content here [1] => 2010-02-04 07:07:23 )
[9] => Array ( [0] => content here [1] => 2010-02-04 08:51:18 )
)
How can I sort it by the timestamp?
Or usort() with strtotime():
function compare($e1, $e2) {
$t1 = strtotime($e1[1]));
$t2 = strtotime($e2[1]));
if($t1 == t2) {
return 0;
}
return ($t1 > $t2) ? 1 : -1;
}
usort($array, 'compare');
Use usort() with a cmp_function that compares index 1 of each of the passed arguments.
Use array_multisort.
array_multisort() It's a nasty but powerful little function. Basically you'll have to iterate through your array, pulling out the date stamp into a new array - maintaining key association. Then, sort that new array, still maintaining key association. Then throw both your newly sorted array and the original array into array_multisort() and your original array will be sorted to have keys in the same order as your sorted array.
Clear as mud? The examples on that doc page should help.
How about Bubble sort?
That means looping through each date, check if the previous is bigger, etc.

Categories