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.
I have two array two array. First is multidimensional and other is single dimensional. I want to find difference between them. How do I found.
$arrayresult
Array 1
Array (
[0] => Array ( [0] => ishani.lad [1] => 9033187384 )
[1] => Array ( [0] => rajkumar.prajapati [1] => 8460078459 )
[2] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[3] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[4] => Array ( [0] => vishal.dake [1] => 9879815299 )
[5] => Array ( [0] => mohsin [1] => 8347163123 )
)
$useduser
Array 2
Array (
[0] => ishani.lad
[1] => rajkumar.prajapati
[2] => lokesh.bhandari
)
I need difference as result as below
Result
Array (
[0] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[1] => Array ( [0] => vishal.dake [1] => 9879815299 )
[2] => Array ( [0] => mohsin [1] => 8347163123 )
)
I have used solution as
$resultremainig = [];
foreach($arrayresult as $val2){
if(!in_array($val2[0], $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
}
But it show last record also. Result of above code is as below. It always show me last record in second array's also
Array (
[0] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[1] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[2] => Array ( [0] => vishal.dake [1] => 9879815299 )
[3] => Array ( [0] => mohsin [1] => 8347163123 )
)
If you wanted you could try using nested loops like so:
<?php
$arrOne = $arrFinal = [
["ishani.lad", 9033187384],
["rajkumar.prajapati", 8460078459],
["lokesh.bhandari" , 9687060900],
["shishanshu.rai" , 8401915337],
["vishal.dake" , 9879815299],
["mohsin" , 8347163123],
];
$arrTwo = [
"ishani.lad",
"rajkumar.prajapati",
"lokesh.bhandari",
];
foreach($arrOne as $key=>$item){
foreach($arrTwo as $k=>$v){
if(in_array($v, $item)){
unset($arrFinal[$key]);
}
}
}
var_dump($arrFinal);
// PRODUCES:::
array (size=3)
3 =>
array (size=2)
0 => string 'shishanshu.rai' (length=14)
1 => int 8401915337
4 =>
array (size=2)
0 => string 'vishal.dake' (length=11)
1 => int 9879815299
5 =>
array (size=2)
0 => string 'mohsin' (length=6)
1 => int 8347163123
You could use array_filter():
$output = array_filter($arrayresult, function($a) use ($useduser) {
return !in_array($a[0], $useduser);
});
Hi You can also try this
$one = array(array('ishani.lad',9033187384),array('rajkumar.prajapati',8460078459),array('lokesh.bhandari',9687060900),array('shishanshu.rai',8401915337),array('vishal.dake',9879815299),array('mohsin',8347163123));
$two = array('ishani.lad','rajkumar.prajapati','lokesh.bhandari');
foreach($one as $array){
if(!in_array($array[0],$two)){
$final[] = $array;
}
}
echo "<pre>";print_r($final);
Output
Array
(
[0] => Array
(
[0] => shishanshu.rai
[1] => 8401915337
)
[1] => Array
(
[0] => vishal.dake
[1] => 9879815299
)
[2] => Array
(
[0] => mohsin
[1] => 8347163123
)
)
Trim the value before checking in $useduser array
$resultremainig = [];
foreach($arrayresult as $val2){
// this removes any extra spaces from the search string
if(!in_array(trim($val2[0]), $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
You need to use the array_diff function.
Store your 2 arrays in variables and compare them.
I have toe different multidimensional array following :
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
[2] => Array
(
[0] => 1
)
)
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 1
)
)
I want to check small multidimensional array exists in bigger array. Any suggestion please.
I am using
$diff = Hash::diff(samllarray, $bigger array);
of cakephp and its result is
Array
(
[2] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 1
)
)
but in result I want only 3rd key but its also given me 2rd key see above
You can use is_array() to see if a variable is an array.
$arrs = array(
0 => "big array",
1 => "big array",
3 => array(
0 => "nested array",
1 => "nested array"
)
);
foreach ($arrs as $key=>$value) {
if (is_array($value)) {
echo "we've got an array at index {$key}";
}
}
How do I merge these associative arrays so that the indices ([0],[1]) are preserved and var_id, name and id are merged? I've tried array_combine and array_merge_recursive without succes.
Input
Array (
[0] => Array (
[var_id] => 43
)
[1] => Array (
[var_id] => 25
)
)
Array (
[0] => Array (
[name] => Tortoise
)
[1] => Array (
[name] => Black
)
)
Array (
[0] => Array (
[id] => 1907
)
[1] => Array (
[id] => 1908
)
)
Desired output
Array (
[0] => Array (
[var_id] => 43
[name] => Tortoise
[id] => 1907
)
[1] => Array (
[var_id] => 25
[name] => Black
[id] => 1908
)
)
Cheers,
Adnan
Assuming your three arrays are called $array1, $array2, and $array3 here's a loop that will do what you want:
foreach(array($array1, $array2, $array3) AS $array) {
foreach($array AS $key => $value) {
foreach($value AS $subkey => $subvalue) {
$final[$key][$subkey] = $subvalue;
}
}
}
Working example: http://3v4l.org/GY9oa
If you have an unknown number of input arrays to merge, it would be trivial to turn this into a function to handle that.
How would I go about flipping an array and establishing relationships between all the values and keys? For example:
I am trying to turn this:
Array (
[11913] => Array (
[0] => 4242
[1] => 3981
)
[9878] => Array (
[0] => 2901
[1] => 3981
)
[11506] => Array (
[0] => 3981
[1] => 2901
)
)
Into this:
Array (
[3981] => Array (
[0] => 11506
[1] => 9878
[2] => 11913
)
[2901] => Array (
[0] => 11506
[1] => 9878
)
[4242] => Array (
[0] => 11913
)
)
Are there any PHP functions that will already do this automatically? If not what would be a way of going about this? Can't seem to wrap my head around it.
Here you go.
$final_array = array();
foreach($initial_array as $key => $val){
foreach($val as $v){
$final_array[$v][] = $key;
}
}