tags:

views:

43

answers:

2

Given original codes as:

foreach($option as $name=>$value)
    $array[$name] = $value;

and $name as

button[0][text], button[0][value], button[1][text], spider[0][name], ...

The result array would be

array('button[0][text]' => 'its text',
    'button[0][value]' => 'its value',
    'button[1][text]' => 'its text',
    'spider[0][name]' => 'its name',
)

However, what i want is

array('button' => array( array('text'=>'its text', 'value'=>'its value'), // this is $array[button][0]
                         array('text'=>'its text') // this is $array[button][1]
                  ),
      'spider' => array( array('name'=>'its name') // this is $array[spider][0]
                  )
)

How could i do this? ...

A: 

I just worked out a solution myself, but still want to see other solutions. For your interesting, paste my solution here:

if(preg_match('/^([^\[\]]+)(\[.+\])$/', $name, $matches)) {
    eval('$myarray[$matches[1]]'. $matches[2]. ' = $value;');
Edward
+1  A: 
elias
agree, that's why i want to see more solutions and you gave a good example. the only problem is the key number is not known. it can be 2, 3 or more
Edward