I want to use foreach to showing orderIds in my output.
Here is my code :
$orders = $results['result']['data'];
foreach ($orders as $key => $order)
{
dd($order[$order]['orderId']);
}
here is $orders result :
1 => array:3 [
"orderId" => 4
"orderTotalPrice" => 100
}
"resId" => 1
]
2 => array:3 [
"orderId" => 18
"orderTotalPrice" => 100
}
"resId" => 1
]
3 => array:3 [
"orderId" => 34
"orderTotalPrice" => 100
}
"resId" => 1
]
4 => array:3 [
"orderId" => 64
"orderTotalPrice" => 100
}
"resId" => 1
]
Any suggestion?
By using foreach you can do this:
$orderIDs = [];
foreach ($orders as $order){
$orderIDs[] = $order['orderId'];
}
Or you can use the pluck method. It will retrieve all of the collection values for a given key:
collect($orders)->pluck('orderId')->all();
Maybe something like this?
$order_ids = [];
foreach ($orders as $order)
{
array_push($order_ids, $order['orderId']);
}
return $order_ids;
As per #IsmailRBOUH's answer, use his solution if you're adding/pulling x-handful of data. If you're looping over a heavy amount of data, would be better (performance wise) to use array_push. But honestly, it's a fractional difference between them both. $array[] usually landing on top...
PHP's website:
Note: If you use array_push() to add one element to the array it's
better to use $array[] = because in that way there is no overhead of
calling a function.
"For showing orderId's in your output:"
$orders = $results['result']['data'];
foreach ($orders as $key => $order)
{
echo $order['orderId'];
}
Related
I am currently trying to learn Laravel and PHP in general.
I need to be able to import an Excel file, then get the data from that file. Currently, the import part works and I can see the data from the file. However, I am having trouble accessing the data.
I've read about the toArray() function in Laravel, and is using that as below:
$data = Excel::load($path, function($reader) {})->skipColumns(2)->get();
$data = $data->toArray();
foreach ($data as $key => $value) {
//We only need some of the available data.
echo $value->next_milestone_desc_cur._comp._incl_rltd;
echo $value->shipment_id;
}
Above code gives me below error:
Trying to get property 'next_milestone_desc_cur' of non-object
Below is an output from the array, which I have generated using dd($value):
array:543 [▼
0 => array:20 [▼
"next_milestone_desc_cur._comp._incl_rltd" => "005. DK - Add DropMode/Local Trp Org in Pickup Tab"
"milestone_cur._comp._sequence_no" => "005"
"cur._comp._export_validation" => "NOT OK"
"shipment_id" => "SBRY0162091"
"consol_id" => "CDK327188" ]
1 => array:20 [▼
"next_milestone_desc_cur._comp._incl_rltd" => "005. DK - Add DropMode/Local Trp Org in Pickup Tab"
"milestone_cur._comp._sequence_no" => "005"
"cur._comp._export_validation" => "NOT OK"
"shipment_id" => "SBRY0162124"
"consol_id" => "CDK327221"
]
What am I doing wrong here? I have also tried
echo $value[0]->next_milestone_desc_cur._comp._incl_rltd;, however this doesn't work either.
You are trying to access an array as an object hence your error and to address you second point, you need to use a nested loop to then access the sub-array.
Your array looks something like this:
$array = [
0 => [
0 => [
'test' => 'value'
],
1 => [
'test' => 'something'
]
],
1 => [
0 => [
'test' => 'value'
],
1 => [
'test' => 'something'
]
]
];
You need the following loop:
foreach ($array as $key => $nested) {
foreach ($nested as $nested_value) {
// You have access to your inner arrays.
echo $nested_value['test'] . ' | ';
}
}
Where your nested loops $nested would be $value.
Live Example
Repl
Your output from dd($value) shows that $value is an array. So you cannot use arrow notation that is for objects. Change these lines:
echo $value->next_milestone_desc_cur._comp._incl_rltd;
echo $value->shipment_id;
To:
echo $value[0]["next_milestone_desc_cur._comp._incl_rltd"];
echo $value[0]["shipment_id"];
Hi I have an array that contains two arrays that has the following structure:
categories [
"lvl0" => array:2 [
0 => "Cleaning"
1 => "Bread"
]
"lvl1" => array:2 [
0 => null
1 => "Bread > rolls"
]
]
I would like to remove any records of NULL from the 'lvl1' array but have not been able to find the correct method to do this.
I have tried:
array_filter($categories['lvl1'])
But this also removes all records associated to lvl1 and not just the NULL ones.
Any help would be greatly appreciated.
Thanks
array_filter() takes a callback as the second argument. If you don't provide it, it returns only records that aren't equal to boolean false. You can provide a simple callback that removes empty values.
array_filter() also uses a copy of your array (rather than a reference), so you need to use the return value.
For instance:
$categories = [
"lvl0" => [
"Cleaning",
"Bread"
],
"lvl1" => [
null,
"Bread > rolls"
]
];
$lvl1 = array_filter($categories['lvl1'], function($value) {
return !empty($value);
});
var_dump($lvl1);
That will return:
array(1) {
[1] =>
string(13) "Bread > rolls"
}
I was having the same issue on my last working day.Generally for associative array array_filter() needs the array key to filter out null, false etc values. But this small function help me to filter out NULL values without knowing the associative array key. Hope this will also help you, https://eval.in/881229
Code:
function array_filter_recursive($input)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = array_filter_recursive($value);
}
}
return array_filter($input);
}
$categories = [
"lvl0" => [
"Cleaning",
"Bread"
],
"lvl1" => [
null,
"Bread > rolls"
]
];
$result = array_filter_recursive($categories);
print '<pre>';
print_r($result);
print '</pre>';
Output:
(
[lvl0] => Array
(
[0] => Cleaning
[1] => Bread
)
[lvl1] => Array
(
[1] => Bread > rolls
)
)
Ref: http://php.net/manual/en/function.array-filter.php#87581
Robbie Averill who commented on my post with the following solved the issue:
$categories['lvl1'] = array_filter($categories['lvl1']);
I have a family tree app in laravel, and I want to be able to show an outline view (start with a family from long ago, show its kids, show those kids' families, those families' kids, etc).
So I made this recursive get_descendants function:
public static function get_descendants(Family $family, $results_array, $counter)
{
// start new round with a different temp array, to keep track
$counter++;
$this_array = "array_$counter";
$$this_array = [];
array_push ($$this_array, $family->caption);
$kids = FamilyController::get_kids_of_family($family);
// if family has no kids, return 0;
if (!count($kids))
{
return 0;
}
else // add kids and check for their families
{
foreach ($kids as $kid) {
array_push ($$this_array, $kid->firstname);
// get families made by kid- for each one, call get_descendants
$families_made = FamilyController::get_families_person_made($kid);
foreach ($families_made as $new_family) {
array_push($$this_array, self::get_descendants($new_family, $$this_array, $counter));
}
};
// we've gone through the kids, add this round's array to the general results array
array_push ($results_array, $$this_array);
}
return $results_array;
}
I've confirmed with print statements that the looping through is correct, but there's a problem with the way I'm saving the results. I want to get something like this, where the top family shows once, with children and their families nested:
array:1 [▼
0 => array:4 [▼
0 => "Padme & Anakin"
1 => "Leia"
2 => array:3 [▼
0 => "Leia & Han"
1 => "Kylo Ren"
]
3 => "Luke"
]
]
but I'm getting this (with an extra repeat in the middle):
array:1 [▼
0 => array:4 [▼
0 => "Padme & Anakin"
1 => "Leia"
2 => array:3 [▼
0 => "Padme & Anakin"
1 => "Leia"
2 => array:3 [▼
0 => "Leia & Han"
1 => "Kylo Ren"
]
]
3 => "Luke"
]
]
Can anyone see where my mistake is?
Update: it turns out that it works if I get rid of that final results_array and just use the dynamic one the whole way, like this:
public static function get_descendants(Family $family, $results_array, $counter)
{
// start new round with a different temp array, to keep track
$counter++;
$this_array = "array_$counter";
$$this_array = [];
array_push ($$this_array, $family->caption);
$kids = FamilyController::get_kids_of_family($family);
// if family has no kids, return 0;
if (!count($kids))
{
return 0;
}
else // add kids and check for their families
{
foreach ($kids as $kid) {
array_push ($$this_array, $kid->first);
// get families made by kid- for each one, call get_descendants
$families_made = FamilyController::get_families_person_made($kid);
if (count($families_made))
{
foreach ($families_made as $new_family) {
array_push($$this_array, self::get_descendants($new_family, $$this_array, $counter));
}
}
};
}
return $$this_array;
}
Looks unnecessarily complex, with dynamic arrays, and you keep populating the same array, so that's why Luke appears in the wrong place.
A cleaner solution might be to be very specific about where people are in the array rather than using a dynamic array name. Just a suggestion -
public static function getDescendants(Family $family)
{
$family = [];
$family['name'] = $family->caption;
if ($kids = static::getKidsOfFamily($family)) {
foreach ($kids as $kid) {
$family['children'][] = $kid->firstname;
$subfamilies = static::getFamiliesPersonMade($kid);
foreach ($subfamilies as $subfamily) {
$family['subfamilies'][] = static::getDescendants($subfamily);
}
};
}
return $family;
}
would produce something like
array [
"name" => "Padme & Anakin"
"children" => array [
"Leia",
"Luke"
],
"subfamilies" => array [
array [
"name" => "Leia & Han"
"children" => array [
"Kylo Re"
]
]
]
]
I am very new to PHP, learning fast but not fast enough! I am also learning Laravel 5.1.
I am trying to build a HTML select list array from an Eloquent query output, in the correct format for form builder (Form::select).
I have the following call to Eloquent to pull the data:
// Get list of States for address select
$states = State::all()->toArray();
It returns the following array:
array:8 [▼
0 => array:2 [▼
"id" => "1"
"state" => "ACT"
]
1 => array:2 [▼
"id" => "2"
"state" => "NSW"
]
...
];
I want to loop through it and generate the following output:
array = [
'' => 'State', <-- This is the default for the select list
'1' => 'ACT',
'2' => 'NSW',
...
];
I am using Laravel 5.1, so I am using the included array_add() function in my helper.
I call my function like this:
$states = create_select_list($states, 'State');
I next want to format the output so it is ready for the Form::select statement. I have tried the code below (as the final try from a few iterations!) but unsuccessfully.
function create_select_list($data, $default)
{
// Declare array and set up default select item
$container = ['' => $default];
// Loop through data entries and build select list array
foreach($data as list($entry, list($key, $value))) {
$container = array_add($container, $key, $value);
}
// Return the select list array
return $container;
}
All help or suggestions are appreciated!
This answer is not about loop fix. I think previous comment should help you.
Just another idea. You can try use array_map instead foreach for this case.
For example:
$states = ['' => 'State'];
array_map(function($item) use (&$states) {
$states[$item['id']] = $item['state'];
}, State::all()->toArray());
Loop like below:
foreach($data as $key => $keyArr ) {
$container = array_add($container, $keyArr['id'], $keyArr['state']);
}
You don't need to use list() in your foreach loop, instead try:
foreach($data as $key => $value) {
$container = array_add($container, $key, $value);
}
The PHP documentation gives a good overview of what list() actually does.
I need some help wrapping my head around a problem. I have an array filled with other arrays. I need to:
Loop through the entire array and build a new array called finalOptions
Each iteration of the loop will take a new SearchIndex and apply the other paramenters
i.e
SearchIndex => SportingGoods
MinPercentageOff => 50
MinimumPrice => 1
ItemPage => 1
Sort => salesrank
BrowseNode => 2342470011
THEN:
Final array should contain data like this
SearchIndex => SportingGoods
MinPercentageOff => 60
MinimumPrice => 100
ItemPage => 2
Sort => salesrank
BrowseNode => 3403201
Basically, I'm creating a new array and sending it to another method that will execute a call to an API and return a result, then doing it again until my array options are complete.
This might not be the way to go and I'm looking for suggestions/pseudo code on an approach. Here is the basics of what I have so far:
Starting with this code
$allOptions = array(
"SearchIndex" => array("SportingGoods", "Tools"),
"MinPercentageOff" => array("50", "60", "70"),
"MinimumPrice" => array("1", "100", "1000"),
"ItemPage" => array("1", "2"),
"Sort" => array("salesrank")
"BrowseNode" => array(
"SportingGoods" => array("2342470011", "3403201"),
"Tools" => array("511364")
)
)
$finalOptions = array();
foreach($allOptions as $options){
foreach($options["SearchIndex"] as $searchIndex){
$finalOptions[] = "SearchIndex" => $searchIndex[]
}
$this->itemSearch($finalOptions);
}
EDIT
The arrays will contain more values. i.e "ItemPage" => array("1", "2"), will have 1 - 10. The others will have more values as well.
From the given array it will produce 54 possible combinations as you described.
Also you need to make sure you have array in $allOptions['BrowseNode'] indexed as each value of $allOptions['SearchIndex']. Otherwise it will produce error.
Cartesian function from here.
$allOptions = [
"SearchIndex" => ["SportingGoods", "Tools"],
"MinPercentageOff" => ["50", "60", "70"],
"MinimumPrice" => ["1", "100", "1000"],
"ItemPage" => ["1", "2"],
"Sort" => ["salesrank"],
"BrowseNode" => ["SportingGoods" => ["2342470011", "3403201"], "Tools" => ["511364"] ] ];
$finalOptions = $allOptions; // copy our initial $allOptions array
unset($finalOptions['BrowseNode']); // deal with BrowseNode key later with custom iterator
$cartesian_product = cartesian($finalOptions); // find cartesian except BrowseNode
foreach($cartesian_product as $cartesian) // each member of cartesian product will iterate here
{
foreach($allOptions['BrowseNode'][$cartesian['SearchIndex']] as $possible)
/*
We have unset the BrowseNode, so need to refer original $allOptions array for BrowseNode,
In every cartesian product, we will get $cartesian['SearchIndex'] and it will contain either
'SportingGoods' or 'Tools' , so in our original array, look for 'BrowseNode' value, having key
same as $cartesian['SearchIndex'].
$allOptions['BrowseNode'][$cartesian['SearchIndex']] <---- is similar to below two lines
$key = $cartesian['SearchIndex'];
$allOptions['BrowseNode'][$key];
Finally iterate through $allOptions['BrowseNode'][$cartesian['SearchIndex']] will iterate as many times,
as many values there are
*/
{
$cartesian['BrowseNode'] = $possible; // assign the long waited key here to 'BrowseNode'
var_dump($cartesian); // here you can do $this->itemSearch($cartesian);
}
}
function cartesian($input) {
$input = array_filter($input);
/*
will renove any false values in input array,
in our array's case, it will do nothing.
*/
$result = [[]];
foreach ($input as $key => $values) {
$append = [];
foreach($result as $product) {
foreach($values as $item) {
$product [$key] = $item;
$append [] = $product;
}
}
$result = $append;
}
return $result;
}