How to add an element into subarray in php

php add to multidimensional array
array push php
php add to array with key
php add array to array
php add key value to array
php add to associative array
array_slice php
php array_splice

I have an array :

$main_arr = [
  0=>[
         "el 01",
         "el 02",
         "el 03",
     ],
  1=>[
         "el 11",
         "el 12",
         "el 13",
     ],
  2=>[
         "el 21",
         "el 22",
         "el 23",
     ],
];

Is there a function or single statement in php like ninja_array_method($main_arr, "el new") that add new element into all sub arrays to be :

$main_arr = [
  0=>[
         "el 01",
         "el 02",
         "el 03",
         "el new",
     ],
  1=>[
         "el 11",
         "el 12",
         "el 13",
         "el new",
     ],
  2=>[
         "el 21",
         "el 22",
         "el 23",
         "el new",
     ],
];

I just want to add these element "el new" into all arrays without any change in one single line or just method like add_el($main_arr, "el new")

Use foreach loop with reference &:

foreach($data as $ind=>&$ar){
     $ar[] = 'el new';
}

Demo

Reference & mutates your subarray.

If you need a function:

function add_el($data,$elem){
    foreach($data as $ind=>&$ar){
         $ar[] = $elem;
    }
    return $data;
}

Demo

array_push - Manual, array_slice() returns the sequence of elements from the array array as If offset is non-negative, the sequence will start at that offset in the array . add a note� Adding an array element to a subarray in PHP. Ask Question Asked 7 years, 7 months ago. How to insert an item into an array at a specific index (JavaScript)?

if you are familiar with laravel collection

You can use push Method

$main_arr = [
    0=>[
           "el 01",
           "el 02",
           "el 03",
       ],
    1=>[
           "el 11",
           "el 12",
           "el 13",
       ],
    2=>[
           "el 21",
           "el 22",
           "el 23",
       ],
  ];

  $expectedArray = [
    0=>[
           "el 01",
           "el 02",
           "el 03",
           "el new",
       ],
    1=>[
           "el 11",
           "el 12",
           "el 13",
           "el new",
       ],
    2=>[
           "el 21",
           "el 22",
           "el 23",
           "el new",
       ],
  ];

  $collection = collect($main_arr)
                    ->map(function($eachArray){
                    return collect($eachArray)->push('el new')->toArray();
                })
                ->toArray();

  dd($expectedArray,$collection);

array_slice - Manual, So ar[2][1] references the second element in the third sub-array. (JavaScript array indexes start at zero.) Adding and Removing Elements in Multidimensional� While you can use square brackets to add an element with a string index to an array, the functions below can only add numerically indexed elements. array_push. You can use PHP's array_push function to add multiple elements to the end of an array. Pass the array as the first argument followed by any number of elements in the order in which you

If you want a one-liner here's a few options:

In place

$value = 'el new';
array_walk($main_arr, function (&$entry) use ($value) { $entry[] = $value; });

Immutable initial array

$value = 'el new';
$modified = array_map(function ($entry) use ($value) { return array_merge($entry, [$value]); }, $main_arr);

Immutable with a bit of array_map trickery

$value = 'el new';
$modified = array_map(function ($entry, $value) { return array_merge($entry, [$value]); }, $main_arr, array_fill(0, count($main_arr), $value));

The sky is the limit

JavaScript Multidimensional Arrays, Start the slice from the third array element, and return the rest of the elements in the array: <?php -2 means start at the second last element of the array. Definition and Usage. The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).

PHP array_slice() Function, You can insert variable into a PHP array by using function array_push(). It simply inserts new values in the end of an array. Example: <?php $sub=array("maths"� array_slice can be used to remove elements from an array but it's pretty simple to use a custom function. One day array_remove() might become part of PHP and will likely be a reserved function name, hence the unobvious choice for this function's names.

How to insert a variable into a PHP array, Check if an array can be split into subsets of K consecutive elements � Split array into two subarrays such that difference of their maximum is� How to Insert Elements in the Middle of an Array. The array splice method can be used for adding and/or removing elements from an array. The first argument specifies the location at which to begin adding or removing elements. The second argument specifies the number of elements to delete, if any.

Split the array and add the first part to the end, You can add one element or multiple elements at a time using array_push() function. The array_push() treats an array as a stack and pushes the� The idea is to maintain a set while traversing the array. While traversing, if an element is already found in the set, then increase the count of subarray by 1 as we have to include the current element in the next subarray and clear the set for new subarray. Then, proceed for the complete array in a self-similar manner.

Comments
  • no its just a map and push. Collection has lots and lot of functions