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);
Related
This question already has answers here:
Turning multidimensional array into one-dimensional array [duplicate]
(11 answers)
Closed 2 years ago.
I have a multidimensional array, but I want to put all values into one single array:
array:28 [▼
0 => array:8 [▼
0 => "id"
1 => "monkey"
2 => "horse"
3 => "bird"
4 => "elephant"
5 => "cat"
6 => "whale"
7 => "frog"
]
1 => array:4 [▼
0 => "id"
1 => "whale"
2 => "lion"
3 => "dog"
]
2 => array:4 [▶]
3 => array:4 [▶]
4 => array:3 [▶]
5 => array:4 [▶]
6 => array:10 [▶]
7 => array:3 [▶]
8 => array:5 [▶]
9 => array:18 [▶]
10 => array:3 [▶]
11 => array:4 [▶]
12 => array:5 [▶]
13 => array:3 [▶]
14 => array:6 [▶]
15 => array:3 [▶]
16 => array:3 [▶]
17 => array:3 [▶]
18 => array:3 [▶]
19 => array:4 [▶]
20 => array:6 [▶]
21 => array:3 [▶]
22 => array:5 [▶]
23 => array:8 [▶]
24 => array:3 [▶]
25 => array:5 [▶]
26 => array:3 [▶]
27 => array:5 [▶]
]
My approach:
$singleArray = array();
foreach ($multiArray as $key => $value){
$singleArray[] =$value;
array_merge($singleArray, $value);
}
dump($singleArray);
But this is again creating a multidimensional array
It looks like you want all sub array values to be in the single array.
$singleArray = [];
foreach($multiarray as $array) {
$singleArray = array_merge($singleArray, array_values($array));
}
This may contain some values as a duplicate. To clean them up you can do
$uniqueValues = array_unique($singleArray);
You can use this function to convert nested array to one array.
<?php
$a = ["a","b","c",["d","e",["f","g"]],["p","q","r"],["s","t","u"]];
function convert(array $array){
$arr = [];
foreach ($array as $item) {
if(is_array($item)){
$arr = array_merge($arr, convert($item));
}else {
$arr[] = $item;
}
}
return $arr;
}
echo "<pre>";
print_r(convert($a));
echo "</pre>";
?>
Did you try with array_merge?
$array = array(yourArray1(), yourArray2());
$oneLevelArray = array_reduce($array, 'array_merge', array());
Need Help to make new array using two array.
One array is
array:2 [▼
"Date Date 2018-07-23" => array:1 [▼
6 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
]
"Date Date 2018-07-28" => array:3 [▼
1 => array:2 [▼
"avg" => 0.031472222222222
"line_n0" => 1
]
4 => array:2 [▼
"avg" => 0.011111111111111
"line_n0" => 4
]
5 => array:2 [▼
"avg" => 0.011111111111111
"line_n0" => 5
]
]
]
and another array is
array:6 [▼
1 => array:2 [▼
"avg" => 0
"line_n0" => 1
]
2 => array:2 [▼
"avg" => 0
"line_n0" => 2
]
3 => array:2 [▼
"avg" => 0
"line_n0" => 3
]
4 => array:2 [▼
"avg" => 0
"line_n0" => 4
]
5 => array:2 [▼
"avg" => 0
"line_n0" => 5
]
6 => array:2 [▼
"avg" => 0
"line_n0" => 6
]
array:2 [▼
"Date Date 2018-07-23" => array:1 [▼
1 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
2 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
3 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
4 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
5 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
6 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
]
"Date Date 2018-07-28" => array:3 [▼
1 => array:2 [▼
"avg" => 0.031472222222222
"line_n0" => 1
]
2 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
3 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
4 => array:2 [▼
"avg" => 0.011111111111111
"line_n0" => 4
]
5 => array:2 [▼
"avg" => 0.011111111111111
"line_n0" => 5
]
6 => array:2 [▼
"avg" => 0.0
"line_n0" => 6
]
]
]
Please help me how i will combine the array ,I want to push 2nd array element if not fund in first array key.I trying but not clear to make new array. I have tried array merge and array combine but not clear to make this new array.
Use array_merge() function Example below
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Output like this:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
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]);
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 ▶}
]
]
i have a collection which has an array in it as item and i want to know how can i access it in my code here is the dd of the collection :
Collection {#990
#items: array:1 [
0 => RoomPricingHistory {#971
#fillable: array:19 [
0 => "accommodation_room_id"
1 => "net_price"
2 => "sales_price"
3 => "extra_bed_price"
4 => "half_charge_price"
5 => "half_board_price"
6 => "full_board_price"
7 => "foreign_net_price"
8 => "foreign_sales_price"
9 => "foreign_extra_bed_price"
10 => "foreign_half_charge_price"
11 => "foreign_half_board_price"
12 => "foreign_full_board_price"
13 => "operator_id"
14 => "commission_percent"
15 => "discount_percent"
16 => "from_date"
17 => "to_date"
18 => "is_deleted"
]
#connection: "mysql"
#table: "room_pricing_histories"
#primaryKey: "id"
and here is my try for code :
$sum_price[] = $room_price[$i]->sales_price;
its inside a for loop but i get this error :
Property [sales_price] does not exist on this collection instance.