0

I have the following multi dimension array and I am not able to do a foreach loop (with laravel). I want to show the name. Any idea how to loop trough that array to show just the name? I reduced the showed array -> ...

I want to loop trough that array not in a view but in a controller because i want to create a database entry for every client

array:1 [▼
  "client" => array:52 [▼
    0 => array:11 [▼
      "name" => "Company One"
     ...
    ]
    1 => array:11 [▼
      "name" => "Company 2"
     ...
    ]

Thanks for your help.

3
  • 4
    can we take a look of what you have done so far? Commented Sep 4, 2018 at 9:16
  • share the code where you print it and how to send it to view ? Commented Sep 4, 2018 at 9:18
  • Can we see your .blade.php view where you @foreach? Commented Sep 4, 2018 at 9:19

2 Answers 2

1
$array = [
    'client' => [
        [
            'name' => 'Company One',
            'foo' => 'Foo One',
        ],[
            'name' => 'Company 2',
            'foo' => 'Foo 2',
        ]
    ]
];

$names = array_pluck($array['client'], 'name');

foreach($names as $name) {
    echo $name; // Replace this with the logic to create DB entry
}
Sign up to request clarification or add additional context in comments.

Comments

1

Its easy all you have to do is this ,lets assume that your array are in the varibale $myArray

    $myArray = [▼
  "client" => array:52 [▼
    0 => array:11 [▼
      "name" => "Company One"
     ...
    ]
    1 => array:11 [▼
      "name" => "Company 2"
     ...
    ]

then you have to do:

    @foreach ($myArray->client as $data)

         {{$data->name}}

   @endforeach

1 Comment

both parent and childs are arrays, not objects ;). Using object operator -> to access them wont work

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.