2

I have the following array that I need to use in a laravel email view template

$inputs['test']

Which looks like this when I dd($inputs['test']);

Array:1[
    "order" => array:2[
        0 => 523
        1 => 522
     ]
 ]

I've tried this in my foreach loop but it doesn't work

foreach($inputs['test']->order as $test){
        echo $test;}

What syntax would I need to echo each value from the order nested array?

7 Answers 7

5

You use square braces [] to access array values, arrows -> to access properties on objects

foreach($inputs['test']['order'] as $test){
        echo $test;
}
Sign up to request clarification or add additional context in comments.

Comments

4

You have to use array not object loop:

foreach($inputs['test']['order'] as $test){           
        echo $test;

}

Comments

3

Simply use the helper method for Arr facade and avoid all these complications.

For instance, if you've something like

$array = ['products' => ['desk' => ['price' => 100]]];

You can do this

return Arr::get($array, 'products.desk.price');

and it will return 100. The best part is if the key price doesn't exist, it doesn't throw and error.

So, if the array had to be like this

$array = ['products' => ['desk' => []];

and you did this

return Arr::get($array, 'products.desk.price');

It would return null and not any error even though the item doesn't exist. You can also set a default value if you want like this

return Arr::get($array, 'products.desk.price', 2);

This would return 2 if the price doesn't exist. https://laravel.com/docs/8.x/helpers#method-array-get

3 Comments

Thanks for being the only Laravel-like answer, this is exactly the kind of headache Laravel is meant to solve.
Check out their helpers section in the doc. It will blow your mind the kind of hazards that it solves. I've worked with django, fastapi but nothing matches the convenience of laravel. @HashimAziz
use Illuminate\Support\Arr; if needed
2

Try

$inputs['test']['order'][0]

Basically, php reads the nested arrays as arrays in arrays .. so no matter how many arrays nested you can always use [][][][][]

php manual

Example #6 Accessing array elements

<?php
$array = array(
    "foo" => "bar",
    42    => 24,
    "multi" => array(
         "dimensional" => array(
             "array" => "foo"
         )
    )
);

var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>

and you can use it in looping as such

foreach($inputs['test']['order'] as $test){
    echo $test;} 

Comments

1

First you have what is key and what is value of arrays
They are multiple ways to echo arrays this is one way

foreach($inputs['test']['order'] as $key =>  $test){
        echo 'Key ->'.$key.'<br/>';    
        echo 'Value ->'. $test. '<br/>';
    }

Comments

1
$myEcho = function($x){
    if(is_array($x)){foreach($x as $one){$myEcho($one);}
    }else{ echo $x; }
  };
array_map($myEcho, $inputs['test']['order']);

Comments

-1

data_get($mainArrayName, '.nestedArrayName..NestedArrayKeyName');

try it..hope this work No need a foreach here

1 Comment

data_get() is not an existing core PHP function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.