0

I tried to insert a new array inside my multidimensional/Associative array. BUt it does not work, neither array_push() does. My current code is as follow:

$myJson = array(
  array (
    'id' => 1,
    'name' => 'Jane Doe', 
    'age' => 26
  ),
  array (
    'id' => 2,
    'name' => 'Josélito',
    'age' => 35
  )
);


$myAr = array(
  'id' => 5,
  'channel' => 'pimbaTv',
  'followers' => 15014
);

foreach($myJson AS $js) {
  $js['event'][] = $myAr;
  //$js['event'] = $myAr;
}

So I would have something like this:

  array (
    'id' => 1,
    'name' => 'Jane Doe', 
    'age' => 26,
    'event' => array(
       'id' => 5,
       'channel' => 'pimbaTv',
       'followers' => 15014
     );
  ),
  array (
    'id' => 2,
    'name' => 'Josélito',
    'age' => 35,
    'event' => array(
       'id' => 5,
       'channel' => 'pimbaTv',
       'followers' => 15014
     );
  )

I'm trying everything I can find but still no solution.

PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
  • Share what you've tried please. – Jay Blanchard Sep 17 '18 at 13:26
  • 1
    in loops where you have array without object you must explicitly use reference & for values. Otherwise the table won't be modified. – Robert Sep 17 '18 at 13:47
  • @Robert Is there a reason to use `reference` instead of the example showed by @Madhur ? Because both gives me the same result. – PlayHardGoPro Sep 17 '18 at 13:51
  • 1
    in @Madhur he modifies the original array using key addressing both will do the same the drawback of reference is that the reference $value remains after the loop. – Robert Sep 18 '18 at 08:40

4 Answers4

2

You can use pass by reference to array in for loop like this. For reference PHP Pass by reference in foreach

foreach($myJson AS &$js) {
    $js['event'] = $myAr;
}
1

You need to access your original array $myJson using $key from the loop, for assigning new values. By default, $value inside a foreach loop, are not passed by reference.

Do the following:

foreach($myJson AS $key => $value) {
  $myJson[$key]['event'] = $myAr;
}

You can see other answers as well, utilizing passing by reference.

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
1

In new php versions, you should use references to edit its content.

Instead of doing :

foreach($myJson AS $js) {
    $js['event'][] = $myAr;
}

You shoud do :

foreach($myJson AS &$js) {
    $js['event'][] = $myAr;
}

http://php.net/manual/en/control-structures.foreach.php

"In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference."

Philoupe
  • 101
  • 8
  • Would there be a reason to use `references` instead of the example provided by Madhur ? WOuld this be `Good Practice` or maybe something related to performance? – PlayHardGoPro Sep 17 '18 at 13:53
  • If you use copy ( as in Madhur answer ), you would create new objects on each loop, even if you don't use them. So, yeah, this is a performance issue to use copies instead of references, even if you just read data from arrays. ;) – Philoupe Sep 18 '18 at 14:10
1

The array_push() function is normally used to push new data in the array but must have a sequential index: $arr[0] , $ar[1].. etc.

You cannot use it in associative array directly. But looking to your array structure, since your sub array have this kind of index you can still use array push but you must specify the index. This is an example:

array_push($myJson["event"], $myAr);

Hope it is more clear for you.

Sigma
  • 387
  • 3
  • 17