0

I have array value like this

   Array
(
    [0] => Array
        (
            [channel] => 15
            [id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
        )

    [1] => Array
        (
            [channel] => 16
            [id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
        )

    [2] => Array
            (
                [channel] => 17
                [id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
            )

)

Now i want to add another array value in specific index .lets say i wants to add this array value at index 1

 [1] => Array
            (
                [channel] => 20
                [id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
            )

Now the result output should be like this

     Array
(
    [0] => Array
        (
            [channel] => 15
            [id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
        )
   [1] => Array
            (
                [channel] => 20
                [id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
            )

    [2] => Array
        (
            [channel] => 16
            [id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
        )

    [3] => Array
            (
                [channel] => 17
                [id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
            )

)

this is my foreach loop to serlize channel and id

foreach ($channel as $key => $ch) {
                    $user_hash['channel'] = json_encode($ch);
                    $user_hash['id'] = random_string('alnum', 30);

                    array_push($user_hash_array, $user_hash);
                }
web_hacks
  • 1
  • 1
  • 2
    Does this answer your question? [Insert new item in array on any position in PHP](https://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php) – catcon Jul 13 '21 at 06:57
  • no i do not find any solution nothing worked in my case – web_hacks Jul 13 '21 at 07:29
  • Can you show the code you have tried to insert the new value? – catcon Jul 13 '21 at 12:54

2 Answers2

0

You need to split the array into 2, then insert your new value at the end of the first sub-array, then merge it with the second sub-array. EG: an array which looks like [1,3,4,5] and you want to insert "2" at position 2, then you split at position one to have [1] and [3,4,5]; then you append "2" at the end of first sub-array to form [1,2], then merge this new subarray with the other sub-array([3,4,5]) to form [1,2] + [3,4,5].

For your implementation, try this code:

$array = array() // the original array you want to modify
$insert = array() // the array you want to push into the original one above
$position = 1 // the position at which you want to insert the new item
$newArray = array_slice($array, 0, $position, TRUE) + $insert + array_slice($array, $position, NULL, TRUE);
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

you can use array_splice array method for add element in array at particular position

<?php
  $original_array = array(
        array("channel"=>15,"id"=>"sdfdfsf1"),
        array("channel"=>16,"id"=>"sdfdfsf2"),
        array("channel"=>17,"id"=>"sdfdfsf3")
        );
        
   echo "<pre>";print_r($original_array);

   $inserted_element = 
   array(array("channel"=>20,"id"=>"xxxxxxxxxxewqeqwexxxxxxxewrewrw"));
   $position=1;

    array_splice( $original_array, $position, 0, $inserted_element ); 

    echo "<pre>";print_r($original_array);
 ?>

Output will be as following

Array
 (
   [0] => Array
    (
        [channel] => 15
        [id] => sdfdfsf1
    )

[1] => Array
    (
        [channel] => 16
        [id] => sdfdfsf2
    )

[2] => Array
    (
        [channel] => 17
        [id] => sdfdfsf3
    )

)
Array
 (
[0] => Array
    (
        [channel] => 15
        [id] => sdfdfsf1
    )

[1] => Array
    (
        [channel] => 20
        [id] => xxxxxxxxxxewqeqwexxxxxxxewrewrw
    )

[2] => Array
    (
        [channel] => 16
        [id] => sdfdfsf2
    )

[3] => Array
    (
        [channel] => 17
        [id] => sdfdfsf3
    )

)
Vishal_VE
  • 1,852
  • 1
  • 6
  • 9