check multidimensional array exists another multidimensional array - php

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}";
}
}

Related

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.

how to get the nested array count with key values seperated in php

This is my array
Array
(
[2] => Array
(
[0] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
[1] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:17 AM
[status] => 0
)
)
)
From this I need sub array count i.e., the array having two indexes such as 2 & 1 from this 2 & 1 there are some nested arrays found such as 0 & 1 for each
Here,I need array count as follows
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
How should I get this..
Someone help me out of this...
Thank you..
It is very easy foreach your array and use count or sizeof function.
$desiredArray = array();
foreach ($myarray as $key => $value) {
$desiredArray [$key] ['count'] = sizeof ($value);
}
print_r ($desiredArray);
The output will be as your desired output
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
It's simple, and is better to create new array where you can save count of elements of main array items:
$counts = array();
foreach ($array as $k => $values) {
$counts[$k] = count($values);
}
print($counts); // gives desired result
Also you don't need to have extra array for the $counts array, what you get is:
array (
2 => 2,
1 => 1
)

Delete a dimension in a array

Is there a way to delete a dimension in a array (only if it's empty), it's pretty dificult to explain with words, so that's what i want to do :
I have an array that returns :
(
[region1] => Array
(
[] => Array
(
[0] => citie1
[1] => citie2
)
[region2] => Array
(
[] => Array
(
[0] => citie1
[1] => citie2
[2] => citie3
)
)
)
I want it to be :
(
[region1] => Array
(
[0] => citie1
[1] => citie2
)
[region2] => Array
(
[0] => citie1
[1] => citie2
[2] => citie3
)
)
foreach($array as $key => $value) {
$array[$key] = reset($value);
}
That will replace each value in the outer array with the first element of that value.

Merge two arrays without replacing values

Im a bit lost or mind is not working as it should. I read other questions around but can get mine to work.
I got this array:
Array
(
[89] => Array
(
[0] => 16
[1] => 2
)
)
And i got this :
Array
(
[84] => Array
(
[0] => 2
)
[83] => Array
(
[0] => 2
)
[87] => Array
(
[0] => 2
[1] => 3
)
[88] => Array
(
[0] => 2
)
[89] => Array
(
[0] => 2
)
[90] => Array
(
[0] => 2
)
)
I should get all results but on key 89 i should get the value from first array.
Array
(
[84] => Array
(
[0] => 2
)
[83] => Array
(
[0] => 2
)
[87] => Array
(
[0] => 2
[1] => 3
)
[88] => Array
(
[0] => 2
)
[89] => Array
(
[0] => 16
[1] => 2
)
[90] => Array
(
[0] => 2
)
)
Array merge wont work :( .
Also after i get the result if the first array its :
Array
(
[89] => Array
(
[1] => 2
)
)
The resulting array should update to one record.
Im sure its a 1 min code for you gurus but arrays always been a pain for me.
Thanks
UPDATE : if i use array_merge_recursive it wont keep my keys :
print_r(array_merge_recursive($array1,$array2));
Array
(
[0] => Array
(
[0] => 16
[1] => 2
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 2
)
[3] => Array
(
[0] => 2
[1] => 3
)
[4] => Array
(
[0] => 2
)
[5] => Array
(
[0] => 2
)
[6] => Array
(
[0] => 2
)
)
array_merge_recursive should do the job; look at the documentation for more pointers: http://www.php.net/manual/en/function.array-merge-recursive.php
EDIT
The function behaved differently than I initially thought, hereĀ“s a different version of the function to solve your problem:
function array_merge_recursive_distinct(array &$array1, array &$array2) {
$merged = $array1;
foreach($array2 as $key => &$value) {
if(is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
Thanks to the community of php.net http://www.php.net/manual/en/function.array-merge-recursive.php#92195
If I understand it correctly, adding arrays should suffice:
$result_array = $array1 + $array2;
In contrast to array_merge, it won't overwrite values in first array and won't renumber numerical keys.
$newarray=array();
foreach(array_merge($array1,$array2) as $k=>$arr){
$newarray[$k]=array_merge($array1[$k],$array2[$k]);
}
foreach($firstArr as $key=>$val){
if(in_array($key,$secondArray)){
$secondArray[$key] = $firstArr[$key];
}
}
Try this may help you.

How do i merge the arrays in a particular format?

I have following arrays:
1) for total placed
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalplaced] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalplaced] => 1
)
)
)
2) for total working
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalworking] => 4
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalworking] => 1
)
)
)
3) for total trained
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totaltrained] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totaltrained] => 1
)
)
)
I wanted to merge these arrays so that the resultant array should look like this
[newarray] => Array(
[0] => Array (
[centers] => Array
(
[name] => delhi
[id] => 1
[totalplaced] => 8
[totalworking] => 4
[totaltrained] => 8
)
)
[1]=> Array(
[centers] => Array
(
[name] => mumbai
[id] => 2
[totalplaced] => 1
[totalworking] => 1
[totaltrained] => 1
)
)
)
This is the tabular representation of the above data which i want to display
centername totalplaced totalworking totaltrained
delhi 8 4 8
mumbai 1 1 1
Please help me on this.
Thanks
Pankaj Khurana
The difficulty here is that PHP's functions such as array_merge() and array_merge_recursive() will not merge data into numeric keys, but rather will re-key any duplicate numeric key. So for example given two arrays:
array(
'test' => 'abc',
0 => 'xyz'
);
array(
'test' => 'def',
0 => 'uvw'
);
Merging them together with array_merge() will produce an array like:
array(
'test' => 'def',
0 => 'xyz',
1 => 'uvw'
);
So, you need a custom function to be "additive" on any key, regardless of whether it is a string or numeric key. Try this:
function mixed_key_array_merge() {
$args = func_get_args();
$result = array();
foreach ($args as $arg) {
// discard non-array arguments; maybe this could be better handled
if (!is_array($arg)) {
continue;
}
foreach ($arg as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = $value;
} else if (is_array($result[$key])) {
$result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
}
}
}
return $result;
}

Categories