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.
Related
I have array value like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[2] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
Now i want to add another array value in specific index .lets say i wants to add this array value at index 1
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
Now the result output should be like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[3] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
this is my foreach loop to serlize channel and id
foreach ($channel as $key => $ch) {
$user_hash['channel'] = json_encode($ch);
$user_hash['id'] = random_string('alnum', 30);
array_push($user_hash_array, $user_hash);
}
You need to split the array into 2, then insert your new value at the end of the first sub-array, then merge it with the second sub-array. EG: an array which looks like [1,3,4,5] and you want to insert "2" at position 2, then you split at position one to have [1] and [3,4,5]; then you append "2" at the end of first sub-array to form [1,2], then merge this new subarray with the other sub-array([3,4,5]) to form [1,2] + [3,4,5].
For your implementation, try this code:
$array = array() // the original array you want to modify
$insert = array() // the array you want to push into the original one above
$position = 1 // the position at which you want to insert the new item
$newArray = array_slice($array, 0, $position, TRUE) + $insert + array_slice($array, $position, NULL, TRUE);
you can use array_splice array method for add element in array at particular position
<?php
$original_array = array(
array("channel"=>15,"id"=>"sdfdfsf1"),
array("channel"=>16,"id"=>"sdfdfsf2"),
array("channel"=>17,"id"=>"sdfdfsf3")
);
echo "<pre>";print_r($original_array);
$inserted_element =
array(array("channel"=>20,"id"=>"xxxxxxxxxxewqeqwexxxxxxxewrewrw"));
$position=1;
array_splice( $original_array, $position, 0, $inserted_element );
echo "<pre>";print_r($original_array);
?>
Output will be as following
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[2] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[3] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)
I'd like to know how to avoid dupplicate of element while using a foreach in foreach with multidimensional array ?
The first level of my array can have several item (here's just 2, but maybe I can have 7 level). I've a lot trouble with this. Then this ID is going to be used as a parameter in a sql request, but this is another story.
This is my array :
Array
(
[0] => Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 11
)
[2] => Array
(
[id] => 12
)
)
[1] => Array
(
[0] => Array
(
[id] => 11
)
[1] => Array
(
[id] => 12
)
)
)
This is my foreach loop :
foreach($dataListe as $listeDiff){
foreach($listeDiff as $$item){
// echo $item[0].'<br />';
echo "<pre>".print_r($item, true)."</pre>";
}
}
Result :
Array
(
[id] => 10
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
Wanted :
Array
(
[id] => 10
)
Array
(
[id] => 11
)
Array
(
[id] => 12
)
use array_unique()
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
$result = array_unique($result);
Following should work
$dataListe = array(
array(array('id'=>10),array('id'=>20),array('id'=>20),array('id'=>10),array('id'=>20)),
array(array('id'=>10),array('id'=>30),array('id'=>20),array('id'=>10),array('id'=>20))
);
$result = array();
foreach($dataListe as $listeDiff){
foreach($listeDiff as $item){
if(!(in_array($item, $result))){
$result[] = $item;
echo "<pre>".print_r($item, true)."</pre>";
}
}
}
sample out put
Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 20
)
[2] => Array
(
[id] => 30
)
)
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}";
}
}
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;
}
new here so thanks for taking the time to read my question.
I am running some PHP code that compares the numbers enter on the screen with those in a database. The problem I am having is ordering the two dimensional array after manipulating each line. It looks as though the array id numbers are being removed. I would like to order the array by column [2] in descending order. Can anyone offer any help?
while( $a_row = mysql_fetch_array( $result))
{
$draw = array($a_row['Drawn1'],
$a_row['Drawn2'],
$a_row['Drawn3'],
$a_row['Drawn4'],
$a_row['Drawn5'],
$a_row['Drawn6'],
$a_row['Drawn7'],
$a_row['Drawn8']);
$numbers = array("6", "9", "4", "8", "14", "18");
if (count(array_intersect($draw, $numbers)) >= 1) {
$rs = array(($a_row['DrawNo']), join(" , ",array_intersect($draw, $numbers)), count(array_intersect($draw, $numbers)));
} else {
$rs = null;
}
array_multisort($rs[1], SORT_NUMERIC, SORT_DESC, $rs[0], SORT_ASC, SORT_STRING);
print_r ($rs);
echo "<br />";
}
This is what the output looks like.
Array ( [0] => A0048 [1] => 14 [2] => 1 )
Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 )
Array ( [0] => A0050 [1] => 14 [2] => 1 )
Array ( [0] => A0051 [1] => 14 [2] => 1 )
Array ( [0] => A0052 [1] => 18 [2] => 1 )
Array ( [0] => A0053 [1] => 6 , 14 [2] => 2 )
Array ( [0] => A0054 [1] => 6 [2] => 1 )
Array ( [0] => A0055 [1] => 14 [2] => 1 )
Array ( [0] => A0056 [1] => 4 [2] => 1 )
Array ( [0] => A0057 [1] => 9 , 6 , 4 [2] => 3 )
Thanks for your time
zeroanarchy
Numeric array keys being reindexed is part of the documented behavior of array_multisort(). If you need keys preserved, you need to convert them to string keys.
you are probably looking for:
http://php.net/manual/en/function.array-multisort.php