Mariano
Mariano

Reputation: 25

add elements inside elements in associative array

I have an array that looks like this:

array
(
   [0] => personA
   [1] => personB
)

and I want to add elements to each person like this:

array
(
   [0] => personA
   (
        [0] => elemA
        [1] => elemB
        [2] => elemC
   )
   [1] => personB
)

I'm using this code:

foreach($proj as $key => $cat)
    {
        $proj[$key] = $this->ReturnFolders(WWW_ROOT . "img/proyectos/" . $cat);

    }

That function returns an array that looks like this:

array
    (
       [0] => elemA
       [1] => elemB
    )

But obviously is not working, I get this result:

array
(
   [0] => Array
   (
        [0] => elemA
        [1] => elemB
        [2] => elemC
   )
   [1] => Array
)

Upvotes: 0

Views: 71

Answers (1)

Marc B
Marc B

Reputation: 360702

Your "like this" structure is not possible. You cannot have a single array key have two different values like that (personA and the sub-array).

You'd have to build a more complex structure:

[0] => array(
    'name' => 'personA'
    'values' => array('elemA', 'elemB', 'elemC')
)

Upvotes: 1

Related Questions