Extract array index and merge the item in other array PHP - php

I have this array $all_designs_from_select_articles in PHP. Each index contains the objects Design:
array:7 [▼
0 => array:12 [▼
0 => Design {#297 ▶}
1 => Design {#298 ▶}
2 => Design {#299 ▶}
3 => Design {#300 ▶}
4 => Design {#301 ▶}
5 => Design {#302 ▶}
6 => Design {#303 ▶}
7 => Design {#304 ▶}
8 => Design {#305 ▶}
9 => Design {#306 ▶}
10 => Design {#307 ▶}
11 => Design {#308 ▶}
]
1 => array:15 [▼
0 => Design {#309 ▶}
1 => Design {#310 ▶}
2 => Design {#311 ▶}
3 => Design {#312 ▶}
4 => Design {#313 ▶}
5 => Design {#314 ▶}
6 => Design {#315 ▶}
7 => Design {#316 ▶}
8 => Design {#317 ▶}
9 => Design {#318 ▶}
10 => Design {#319 ▶}
11 => Design {#320 ▶}
12 => Design {#321 ▶}
13 => Design {#322 ▶}
14 => Design {#323 ▶}
]
]
But I want an array with all Design keys combined, like this:
array:26 [▼
0 => Design {#297 ▶}
1 => Design {#298 ▶}
2 => Design {#299 ▶}
3 => Design {#300 ▶}
4 => Design {#301 ▶}
5 => Design {#302 ▶}
6 => Design {#303 ▶}
7 => Design {#304 ▶}
8 => Design {#305 ▶}
9 => Design {#306 ▶}
10 => Design {#307 ▶}
11 => Design {#308 ▶}
12 => Design {#309 ▶}
13 => Design {#310 ▶}
14 => Design {#311 ▶}
15 => Design {#312 ▶}
16 => Design {#313 ▶}
17 => Design {#314 ▶}
18 => Design {#315 ▶}
19 => Design {#316 ▶}
20 => Design {#317 ▶}
21 => Design {#318 ▶}
22 => Design {#319 ▶}
23 => Design {#320 ▶}
24 => Design {#321 ▶}
25 => Design {#322 ▶}
26 => Design {#323 ▶}
...
etc
]
And this is my query to my API and get the first array values:
foreach ($search_articles as $article) {
$all_designs_from_select_articles[] = ApiDesign::getList([
'article_id' => $article,
'visibility' => 'ALL',
'order' => '-POPULARITY,NAME',
'lang' => 'multi,es',
]);
}
Is there a function in PHP to do this?
I´m using PHP7 and the framework laravel 5.2

You could merge the subarrays together after the fact with
$all_designs_from_select_articles = array_reduce(
$all_designs_from_select_articles, "array_merge", []);
Or during the creation of the array:
foreach ($search_articles as $article) {
$all_designs_from_select_articles = array_merge(
$all_designs_from_select_articles,
ApiDesign::getList(...));
}
I think I'd prefer the latter approach, but it's a marginal preference.

I haven't tested this code but it should be as simple as just using array_merge().
$newArray = array_merge($array[0], $array[1]);

Related

Show genre labels from movies

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.

Print tree-array in Twig

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.

How to get value from all index of collection in laravel Illuminate?

I have to query data from DB to using Illuminate in laravel then I want to get all the attribute value from all index of this array.
array:36 [▼
0 => RepaymentSchedule {#553 ▶}
1 => RepaymentSchedule {#554 ▶}
2 => RepaymentSchedule {#555 ▶}
3 => RepaymentSchedule {#556 ▶}
4 => RepaymentSchedule {#557 ▶}
5 => RepaymentSchedule {#558 ▶}
6 => RepaymentSchedule {#559 ▶}
7 => RepaymentSchedule {#560 ▶}
8 => RepaymentSchedule {#561 ▶}
9 => RepaymentSchedule {#562 ▶}
10 => RepaymentSchedule {#563 ▶}
11 => RepaymentSchedule {#564 ▶}
12 => RepaymentSchedule {#565 ▶}
13 => RepaymentSchedule {#566 ▶}
14 => RepaymentSchedule {#567 ▶}
15 => RepaymentSchedule {#568 ▶}
16 => RepaymentSchedule {#569 ▶}
17 => RepaymentSchedule {#570 ▶}
18 => RepaymentSchedule {#571 ▶}
19 => RepaymentSchedule {#572 ▶}
20 => RepaymentSchedule {#573 ▶}
21 => RepaymentSchedule {#574 ▶}
22 => RepaymentSchedule {#575 ▶}
23 => RepaymentSchedule {#576 ▶}
24 => RepaymentSchedule {#577 ▶}
25 => RepaymentSchedule {#578 ▶}
26 => RepaymentSchedule {#579 ▶}
27 => RepaymentSchedule {#580 ▶}
28 => RepaymentSchedule {#581 ▶}
29 => RepaymentSchedule {#582 ▶}
30 => RepaymentSchedule {#583 ▶}
31 => RepaymentSchedule {#584 ▶}
32 => RepaymentSchedule {#585 ▶}
33 => RepaymentSchedule {#586 ▶}
34 => RepaymentSchedule {#587 ▶}
35 => RepaymentSchedule {#588 ▶}
]
I have try to used bellow
$collapsed = $loan->schedule->all();
dd($collapsed);
$collection = $collapsed->each(function ($item, $key) {
dd($key);
});
In laravel 5.1 and above you can use the pluck method of the Collection object:
$plucked = $collapsed->pluck('id');
dd($plucked->all());
Docs: https://laravel.com/docs/5.2/collections#method-pluck
But since you are using L5.0 this isnt possible, I suggest you solve this by the PHP5 method array_column function instead:
$ids = array_column($collapsed->toArray(), 'id');
dd($ids);
Edit:
If you want to retrieve more than one attribute you can use the ->lists() method:
$values = $loan->schedule->lists('id', 'interest', 'principal');
dd($values);

How to combine multi - Arrays with the same key?

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 ▶}
]
]

Display an array in a readable/hierarchical format without mess up the path

I have an array
array:10 [▼
0 => array:3 [▼
"name" => "Xperia Z Ultra"
"img" => "xperia-z-ultra.jpg"
"img_path" => "/Applications/MAMP/htdocs/code/site/public/images/photos/devices/"
]
1 => array:3 [▶]
2 => array:3 [▶]
3 => array:3 [▶]
4 => array:3 [▶]
5 => array:3 [▶]
6 => array:3 [▶]
7 => array:3 [▶]
8 => array:3 [▶]
9 => array:3 [▶]
]
I want to make it pretty so I did this
$pretty_devices = json_encode($devices, JSON_PRETTY_PRINT);
VIEW
<div><pre>{{$pretty_devices}}</pre></div>
I got it display in pretty format as I wanted, but it kind mess up my
img_path.
How do I stop that ?
If you only want to see it nicely try print_r() instead.
echo "<div><pre>".print_r($pretty_devices, true)."</pre></div>"

Categories