petritr
266
7
Laravel

Push to nested array

Posted 5 months ago by petritr

I need to push data to nested array, then return the complete data with the new appended data.

How can i return the original nested array with the new data inside ?

    0 => array:6 [▼
      "id" => 784
      "group" => "1"
      "port" => array:8 [▼
        0 => array:4 [▼
          "id" => 1785
          "port" => "1"
          "services" => array:6 [▼
            "id" => 241
            "service" => 1785
          ]

I go through the array :

        foreach ($Groups as $Group) {
            foreach ($Group['port'] as $item) {
                if ( ($item['services']) ) {
                    array_push($item['services'], 'name');
                    var_dump($item['services']);
                }
            }
        }

the var_dump($item['services']); will return the service with the data

array (size=7)
    "id" => 241
     "service" => 1785
     0 => string 'name' (length=4)

I need to return the appended data to the original array, how can i do that?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.