how to append element to array with specific index in php

php array push with key
array push php
php array add key value pair to existing array
php insert into associative array at position
php add to associative array
php add to multidimensional array
array_splice
php add key value to array

I am new to php. I am trying to get key and value of each element in special array. I want to save/push them into another array start from zero index. Of course (foreach loop) is the best tool here. Please help me BY YOUR CODE. I know describing this problem is a bit hard, so I have some drawings for you.

$info = array(
    <--key-->     <--value--> 
    [10] => 'apple',
    [8] => 'orange',
    [2] => 'banana',
    [23] => 'peach',
)

now I want to have an array like this:

$myGoal = array(
  <-key->   <--------value---------> 
    [0] => array('10'=>'apple'),
    [1] => array('8'=>'orange'),
    [2] => array('2'=>'banana'),
    [3] => array('23'=>'peach')
)

I guess the code I need should be something like below:

$info = array(
    [10] => 'apple',
    [8] => 'orange',
    [2] => 'banana',
    [23] => 'peach',
)

$index = 0;
$myGoal = array();
foreach($info as $amount => $friut){
     $myGoal[$index] = [$amount][$friut];
     $index++;
}

I'll be thank of anybody can help me.

About the syntax $myGoal[$index] = [$amount][$friut];

The part after the equals sign is creating an array with a single value of $amount and then tries to index into the array with the value of $friut which does not exists.

You could update the syntax to add an array to the index with key => value

Change

$myGoal[$index] = [$amount][$friut];

to

$myGoal[$index] = [$amount => $friut];

Output

Array
(
    [0] => Array
        (
            [10] => apple
        )

    [1] => Array
        (
            [8] => orange
        )

    [2] => Array
        (
            [2] => banana
        )

    [3] => Array
        (
            [23] => peach
        )

)

Php demo

As @kaczmen points out, if you only want to append you don't need the index and you could use

$myGoal[] = [$amount => $friut];

How to insert element into arrays at specific position?, Here's a simple function that you could use. Just plug n play. This is Insert By Index, Not By Value. you can choose to pass the array, or use one that you already have declared. array_push (PHP 4, PHP 5, PHP 7) If you use array_push() to add one element to the array, Add elements to an array before or after a specific index or key:

You can do this with array_chunk using a chunk size of 1.

$myGoal = array_chunk($info, 1, true);

Be sure to set the third argument to true to preserve the keys.


Also, not directly related to what you're asking, but I'd like to suggest that using amount as an array key is risky. If you ever have the same amount of two items, the second one will overwrite the first.

array_push - Manual, Note: If you use array_push() to add one element to the array, it's better to use $​array[] Add elements to an array before or after a specific index or key: <?php array_splice (array &$input, int $offset [, int $length= 0 [, mixed $replacement]]) is the function that you are looking for. Format to use this function to add new element at certain position in an array is array_splice ($array, $insert_position, 0, $element_to_insert);. See below for an example of how this function behaves.

Firstly you intialize an array after that store the key value into a new array.

$info = array("10" => 'apple',"8" => 'orange',"2" => 'banana',"23" => 'peach');
$new=array();
foreach($info as $key=>$info1)
{
    $new[] = [$key=>$info1];
}
echo "<pre>";
print_r($new);

Hope This Helps.

Program to Insert new item in array on any position in PHP , How to insert an item into array at specific index in JavaScript? PHP program to add item at the beginning of associative array · Insert string at specified position in  Remember, the PHP Array index is starting from 0. T his operation is also called PHP add to the array. Adding multiple values to PHP array. PHP array_push() function takes multiple elements and appends all the elements into the array. It will add in the order that they are added. It does not change its order.

You can use array_walk, You do not need to declare the $index=0 if you want to start it by 0. You can just use $n[] it will automatic start from 0 index

 array_walk($info,function($v,$k)use(&$n){$n[]=[$k=>$v];});
 print_r($n);

Working example :- https://3v4l.org/DTACm

PHP array_push() Function, 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  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 would like them to be added. The array_push function returns the number of elements in the modified array.

How to Insert an Item into an Array at a Specific Index in JavaScript, You can use the splice() method to insert a value or an item into an array at a specific index in JavaScript. This is a very powerful and versatile method for  This is a beginner’s tutorial on how to append an element to the end of a PHP array. In this guide, I will show you two different methods that you can use in order to do this. Using square brackets to add an element to a PHP array. The most commonly-used method involves placing two square brackets at the end of the array variable.

Insert an element into an array at specific index in Java, The insertion should shift the element currently at that index and any subsequent elements Insert an element into an array at specific index in Java C++, Java, Python, JavaScript, C#, PHP and many more popular programming languages. Removing Array Element and Re-Indexing in PHP In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically.

How to Add Elements to Arrays in PHP, When you want to add individual elements to the end of an array in PHP, square bracket syntax is the most efficient. You can include an index (integer or string)  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).

Comments
  • You can also drop $index variable and make it $myGoal[] = ... if you only care about appending.
  • your idea was the shortest way and the result is OK, tnx
  • a great useful way. thank you anyway