foreach in foreach multiple dimensional - php

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
)
)

Related

How do i make one array out of two arrays containing different values

I have two arrays, one called fetched_services and one called fetched_companies, they look like this:
fetched_services
(
[1] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 153
)
[1] => Array
(
[id] => 137
)
[2] => Array
(
[id] => 138
)
)
)
)
fetched_companies
(
[0] => stdClass Object
(
[services] => Array
(
[0] => 25
[1] => 102
)
)
)
What i want to achieve is to end up with an array like fetched_services but only having child_services with id of fetched_companies["services"].
What i have tried is this:
$services = [];
$isFound = false;
foreach ($fetched_services as $fetched_service) {
foreach ($fetched_service["child_services"] as $fetched_child_service) {
$fetched_service["child_services"] = [];
foreach ($fetched_companies as $fetched_company) {
if( (in_array($fetched_child_service["id"],$fetched_company->services)) ) {
$fetched_service["child_services"][] = $fetched_child_service;
$isFound = true;
}
}
if($isFound) {
$services[] = $fetched_service;
$isFound = false;
}
}
}
This outputs this:
Array
(
[0] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 116
)
)
)
[1] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 117
)
)
)
)
As you can see the resulting array have two arrays containing same id but the child_services are different.
What i want to end up with is this:
Array
(
[0] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 116
)
)
(
[0] => Array
(
[id] => 117
)
)
)
)
What am i doing wrong here? Thanks!
Your question is a bit unclear (I'll edit/delete this if/when you clarify, see my comment above), but you could probably make use of array_filter by only keeping services that are present in said list (via in_array):
$result = array_filter(
$fetched_services['child_services'],
fn(array $child_service): bool => in_array($child_service['id'], $fetched_companies[0]->services, true)
);
Demo

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.

Collecting values from a multidimensional array using foreach loops

There is an array which is built looks like the following (with some more values which i left away for this example):
Array
(
[0] => Array
(
[id] => 44
[cars] => Array
(
[0] => Array
(
[id] => 38
)
[1] Array
(
[id] => 39
)
)
)
[1] => Array
(
[id] => 45
[cars] => Array
(
[0] => Array
(
[id] =>136
)
[1] =>Array
(
[id] =>137
)
[2] =>Array
(
[id] =>138
)
)
)
)
I want to build another array from the above in the following form:
Array
(
[0] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 38
)
[1] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 39
)
[2] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 136
)
[3] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 137
)
[4] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 138
)
)
I tried to achieve this with following function:
foreach($filterSortSaveArray as $filterSortSaveArray['cars'] => $value){
$id = $filterSortSaveArray['id'];
foreach($value['cars'] as $value => $car){
$field_values['car_filter_sort_id'] = $id;
$field_values['car_id'] = $car['id'];
}
}
But the the result differs from what I have expected. Any suggestions?
There are two big issues in your code. First, are you referencing undefined value with $filterSortSaveArray['cars'], since there is no 'cars' key in the first level of the original array. Second, by assigning values to $field_values['car_filter_sort_id'] and $field_values['car_id'] in the loop you are just overriding them in each iteration. You need to push the values into an array using []= operator (which is equivalent to applying array_push()).
Try this:
$result = [];
foreach($filterSortSaveArray as $k => $v) {
if (!is_array($v['cars']))
continue;
$id = $v['id'];
foreach ($v['cars'] as $i => $car){
$result[] = [
'car_filter_sort_id' => $id,
'car_id' => $car['id']
];
}
}

count same categories from array and Store in new array with its count and category name

I have one array which I am getting from database query response.
Now I want to count same categories and print in option_name array.
I have tried it with different array functions but want get desire output.
I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.
Array
(
[0] => Array
(
[CNC] => Array
(
[id] => 5
[category_id] => 68
)
[GVO] => Array
(
[option_name] => Actors
)
)
[1] => Array
(
[CNC] => Array
(
[id] => 11
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[2] => Array
(
[CNC] => Array
(
[id] => 3
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[3] => Array
(
[CNC] => Array
(
[id] => 4
[category_id] => 74
)
[GVO] => Array
(
[option_name] => Musician
)
)
[4] => Array
(
[CNC] => Array
(
[id] => 7
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
[5] => Array
(
[CNC] => Array
(
[id] => 6
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
)
Desire Output:
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
Thanks in advance!
You simply need to loop through the array using foreach like as
$result = [];
foreach($arr as $k => $v){
if(isset($result[$v['GVO']['option_name']])){
$result[$v['GVO']['option_name']] += 1;
}else{
$result[$v['GVO']['option_name']] = 1;
}
}
print_R($result);
You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:
$counts = [];
foreach($array as $v) {
if(!isset($counts[$v['GVO']['option_name']])) {
$counts[$v['GVO']['option_name']] = 0;
}
$counts[$v['GVO']['option_name']]++;
}
print_r($counts);
Try this:
$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
if(in_array($value, $new_array)){
$new_array[$value] = $new_array[$value]+1;
}else{
$new_array[$value] = 1;
}
}
Output
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)

array manipulation and rearranging

Here's my deal.
I have this array:
Array // called $data in my code
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
[2] => Array
(
[name] => quantity
[value] => 0
)
[3] => Array
(
[name] => var_id
[value] => 5
)
)
which I need it to be like:
Array // called $temp in my code
(
[0] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
)
[2] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 5
)
)
)
and I did it using this code I made:
$data = $_POST['data'];
$temp = array();
foreach($data as $key => $datum)
{
if($key%2 == 0)
{
$temp[$key] = array();
array_push($temp[$key], $datum, $data[$key+1]);
}
}
But I think that my code is some kinda stupid, specially if I have a huge data.
eventually what I want to do is just have each two indexes combined in one array, and I know that there should be something better than my code to do it, any suggestions?
Discover array_chunk()
$temp = array_chunk($data, 2);
$cnt = count($data);
$temp = array();
for ($i = 0; $i < $cnt; $i = $i + 2)
{
$temp[] = array($data[$i], $data[$i+1]);
}
Take a look at array_chunk.
<?php
$array = array(
array(1),
array(2),
array(3),
array(4),
);
print_r(
array_chunk($array, 2, false)
);
/*
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
)
[1] => Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 4
)
)
)
*/

Categories