I have an array like this. There are some array with the same name. Such as Grant 1, Grant 2... and they have many Projects that should be in the same place. For example: Grant 1 should contain the information that belongs to Grant 1. The same thing that should happen to Grant 2. And so on
array:5 [▼
0 => array:2 [▼
0 => "Grant 1"
1 => Project {#423 ▶}
]
1 => array:2 [▼
0 => "Grant 1"
1 => Project {#421 ▶}
]
2 => array:2 [▼
0 => "Grant 2"
1 => Project {#412 ▶}
]
3 => array:2 [▼
0 => "Grant 1"
1 => Project {#424 ▶}
]
4 => array:2 [▼
0 => "Grant 2"
1 => Project {#419 ▶}
]
]
I want to combine them to:
array:5 [▼
0 => array:2 [▼
0 => "Grant 1"
1 => Project {#423 ▶}
2 => Project {#421 ▶}
3 => Project {#424 ▶}
]
1 => array:2 [▼
0 => "Grant 1"
1 => Project {#412 ▶}
2 => Project {#419 ▶}
]
]
Please help. Thanks,
I think you can use foreach:
$newArray = [];
foreach ($array as $value) {
$newArray[$value[0]][] = $value[1];
}
And you have array like this:
array:5 [▼
"Grant 1" => array:2 [▼
1 => Project {#423 ▶}
2 => Project {#421 ▶}
3 => Project {#424 ▶}
]
"Grant 2" => array:2 [▼
1 => Project {#412 ▶}
2 => Project {#419 ▶}
]
]
Or something like this(thanks #AbraCadaver):
$newArray = [];
foreach ($array as $value) {
if (isset($newArray[$value[0]] {
$newArray[$value[0]][] = $value[1];
} else {
$newArray[$value[0]] = $value;
}
}
Array what you need:
array:5 [▼
0 => array:2 [▼
0 => "Grant 1"
1 => Project {#423 ▶}
2 => Project {#421 ▶}
3 => Project {#424 ▶}
]
1 => array:2 [▼
0 => "Grant 1"
1 => Project {#412 ▶}
2 => Project {#419 ▶}
]
]
Related
I have a structure like below, there are movies and for each movie the genres and some other info that is not relevant for the question.
The dump:
dd($moviesInformation['movies']['data']);
shows:
^ array:8 [▼
0 => array:22 [▶]
1 => array:22 [▼
"id" => 75
"name" => array:1 [▼
"label" => "movie title"
]
"genres" => array:2 [▼
"data" => array:32 [▼
0 => array:3 [▼
"id" => 64
"name" => array:1 [▼
"label" => "Comedy"
]
"on_request" => 1
]
1 => array:3 [▼
"id" => 65
"name" => array:1 [▼
"label" => "Action"
]
"on_request" => 1
]
2 => array:3 [▶]
3 => array:3 [▶]
4 => array:3 [▶]
5 => array:3 [▶]
]
"meta" => array:1 [▶]
]
...
]
2 => array:22 [▶]
3 => array:22 [▶]
4 => array:22 [▶]
5 => array:22 [▶]
6 => array:22 [▶]
7 => array:22 [▶]
The output should be just the unique values of all movies for genres separated by "," like for example:
Comedy, Action, ...
But I'm not understanding how to properly achieve that. Do you know how it can be?
Your main problem is you are not treating genre like an array in your example and also missing the nested data array.
Collections will make this logic far more readable. This should give you a comma separated strings of genres.
collect(data_get($moviesInformation, 'genres.data', []))
->map(function ($genre) {
return data_get($genre, 'name.label');
})->implode(', ')
Screenshot in Tinkerwell of the output.
This dump:
dd($moviesInformation['movies']['data'][1]['genres']);
show all genres for each movie returned like this:
^ array:2 [▼
"data" => array:32 [▼
0 => array:3 [▶]
1 => array:3 [▶]
2 => array:3 [▼
"id" => 446
"genre" => array:1 [▼
"label" => "Comedy"
]
"on_request" => 1
]
3 => array:3 [▶]
4 => array:3 [▶]
5 => array:3 [▶]
And I want to, based on this, have an output with all genres separated by ",", like this:
'result' => [Comedy, Action, etc],
However doing like this:
collect($moviesInformation['movies']['data'])->map(function ($movie) {
return [
'result' => implode(',', $movie['genres'])
];
});
Im getting "Array to string conversion". Do you know how to properly achieve that?
I have a very complex problem, I have products and modifiers, and also title of the modifiers, I want to store into array the title, product id and modifiers, the way of storing is if the title is the same and the product id the same then store into array and so on for others, after that I want in blade to display product id without repeat the same product id and under it the modifier title without repeat as if i want to use groupby and under the title to show the modifiers, I tried this code but it shows every title in one array, I hope you got what I need sorry for my bad language, please note that Im new to laravel so would be great if you help me to show the result in the blade view, thank you in adavance
$customizeorders = OrderCustomize::where('userorder_id',$order_number)->with('customizeproduct')->get();
$wholeData = [];
$wrapper = [];
$previousTitle = $customizeorders[0]->customizeproduct->customizetitle->name;
$previousProduct = $customizeorders[0]->product_id;
foreach ($customizeorders as $data) {
$singleItem = [];
if ( ($data->customizeproduct->customizetitle->name == $previousTitle) AND ($data->product_id == $previousProduct) ) {
$title = $data->customizeproduct->customizetitle->name;
$product = $data->product_id;
$modifiers = $data->customizeproduct->customize_title;
array_push($singleItem, $title, $product, $modifiers);
array_push($wrapper, $singleItem);
} else {
$previousTitle = $data->customizeproduct->customizetitle->name;
$previousProduct = $data->product_id;
$modifiers = $data->customizeproduct->customize_title;
$title = $data->customizeproduct->customizetitle->name;
$product = $data->product_id;
$modifiers = $data->customizeproduct->customize_title;
array_push($singleItem, $title, $product, $modifiers);
array_push($wrapper, $singleItem);
}
array_push($wholeData, $wrapper);
$wrapper = [];
}
The result:
array:13 [▼
0 => array:1 [▼
0 => array:3 [▼
0 => "TELL US IF YOU'LL NEED"
1 => 2606
2 => "Napkins"
]
]
1 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2610
2 => "Sausage"
]
]
2 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2610
2 => "Bacon"
]
]
3 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2610
2 => "Onions"
]
]
4 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2610
2 => "Hot Giardiniera"
]
]
5 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2610
2 => "Garlic"
]
]
6 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2610
2 => "BBQ Sauce"
]
]
7 => array:1 [▼
0 => array:3 [▼
0 => "SELECT SODA TYPES"
1 => 2662
2 => "Assorted Regular Sodas"
]
]
8 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2618
2 => "Grilled Chicken"
]
]
9 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2618
2 => "Anchovies"
]
]
10 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2618
2 => "Black Olives"
]
]
11 => array:1 [▼
0 => array:3 [▼
0 => "ADD TOPPINGS"
1 => 2618
2 => "Pepperoni"
]
]
12 => array:1 [▼
0 => array:3 [▼
0 => "TELL US IF YOU'LL NEED"
1 => 2618
2 => "Napkins"
]
]
]
I have the following arrays:
array:2 [▼
0 => array:2 [▼
"id" => 3
"total" => 2
]
1 => array:2 [▼
"id" => 4
"total" => 1
]
]
and
array:2 [▼
0 => array:2 [▼
"id" => 3
"total" => 5
]
1 => array:2 [▼
"id" => 4
"total" => 5
]
2 => array:2 [▼
"id" => 5
"total" => 2
]
]
I need to merge them together, keeping ids and summing the results, so I would get the resulting array as:
array:2 [▼
0 => array:2 [▼
"id" => 3
"total" => 7
]
1 => array:2 [▼
"id" => 4
"total" => 6
]
2 => array:2 [▼
"id" => 5
"total" => 2
]
]
These arrays come from a laravel database query using the toArray() method, so answers containing laravel default collection methods are welcome too.
try this
arr3 = arr1->merge(arr2);
arr3->map(function(itemM)use(arr1, arr2){
return itemM->total = arr1->sum(function(itemS)use(itemM){
return itemS->id == itemM->id ? itemS->total : 0;
}) +
arr2->sum(function(itemS)use(itemM){
return itemS->id == itemM->id ? itemS->total : 0;
});
})
I'm relatively new in PHP/Symfony, can you help me with this?
I want to create a CSS tree and print an encapsulated array which contains 6 main categories and n-child categories, structured in this way:
Every array-node contains null-key-array with main-category with name(string) --> contains the category name (1. , 1.1, 1.1.1. ) and n-arrays with sub-nodes. The tree-array is structured by name, contains 6 main-category nodes and n sub- and sub-sub-categories for each node. looks like this:
array:6 [▼
1 => array:8 [▼
"" => array:1 [▼
"mydata" => array:3 [▼
"id" => 182
"name" => "1."
"titel" => "Maincategorie"
]
]
1 => array:9 [▼
"" => array:1 [▼
"mydata" => array:3 [▼
"id" => 1
"name" => "1.1."
"titel" => "Maincategorie - subcat1"
]
]
1 => array:1 [▼
"" => array:1 [▼
"mydata" => array:3 [▼
"id" => 2
"name" => "1.1.1."
"titel" => "Mainkategorie - subcat1_2"
]
]
]
2 => array:6 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
]
2 => array:7 [▶]
3 => array:10 [▶]
4 => array:4 [▶]
5 => array:17 [▶]
6 => array:6 [▶]
7 => array:5 [▶]
]
2 => array:5 [▶]
3 => array:7 [▶]
4 => array:5 [▶]
5 => array:7 [▶]
6 => array:6 [▶]
]
My questions are:
Is it possible to call a recursive function in Twig or something like a self-called macro?
Or, do I need a function to set the depth of array first in the controller? In the empty key "", what is my main-category?
What is the best solution to create a tree from an array?
Thanks a lot - Ifgenia.