How can I add elements to a multidimensional array?

Asked

Viewed 1,861 times

3

how do I add elements to a multidimensional array. For example instead of creating a PHP array in this way:

$i = 0;
$aArray[$i]['title'][] = 'teste';
$aArray[$i]['link'][] = 'teste de link';

Be able to do so

$aArray[$i] = array( 'title' => ??????, 'link' => ????? ) 

o ???? should add an element at the end of this array

  • Looking at this Post seems to be the same doubt Voce has. 
 http://stackoverflow.com/a/19099153

1 answer

3


From what I understand, you must be wanting to simplify the process.

I don’t know if it applies to your case, but one way to do that is by using the references - through the sign &.

$test =& $array[1]['test'];

$test[] = 'stack';

$test[] = 'overflow';

print_r($array);

Upshot:

Array
(
    [1] => Array
        (
            [test] => Array
                (
                    [0] => stack
                    [1] => overflow
                )

        )

)
  • 1

    Interesting. But these types of operations have not been discontinued?

  • 1

    No. Attribution by reference is only depreciated in instances of classes

  • 1

    Okay, I’ve seen it and it’s great! Hug

  • 1

    so I love php.. nor in dream can one do this in "serious" rsrsrs languages

Browser other questions tagged

You are not signed in. Login or sign up in order to post.