Agis Soleas
Agis Soleas

Reputation: 343

How to push an associative element into a multidimensional array?

I have this code:

$g = 0;
while ($g < $c) {
    $a[$g] = array("$groupsid[$g]");
    $g++;
}

which enters value in an array and the output is that

[Groups] => Array(
    [0] => Array(
        [0] => Array(
            [0] => value1
        )
        [1] => Array(
            [0] => value2
        )
    )
)

How can I change the [0] in the inner array to a string I want for example id and have something like this:

[userGroups] => Array(
    [0] => Array(
        [0] => Array(
            [id] => value1
        )
        [1] => Array(
            [id] => value2
        )
    )
)

Upvotes: 0

Views: 29

Answers (1)

pavel
pavel

Reputation: 27092

Add key id into array definition.

$a[$g] = array('id' => $groupsid[$g]);

Upvotes: 2

Related Questions