Forum Moderators: coopster

Message Too Old, No Replies

Using variables as keys in a multimdimensional array

         

Murdoch

9:02 pm on Jul 3, 2008 (gmt 0)

10+ Year Member



I'm trying to construct a loop that builds a multimdimensional array using categorical placemarkers as the initial key, taken from an exploded string. It looks something like this:

$array = explode('*',$string);
foreach($array as $arr) {
list($cat,$value) = split(':',$arr);
$newray[] = array($cat => array($value => "text"));}

Can't figure out what I'm doing wrong here. I looked up some tutorials on multidimensional arrays but all I can find are instances where the second value can be text as a key but the first cannot.

Any help is appreciated.

Thanks

NomikOS

1:17 am on Jul 4, 2008 (gmt 0)

10+ Year Member


running code looks well.

$string = "a:bc*1:23*@:@@";
debug($newray,1);

[0] => Array(1)
[a] => Array(1)
[bc] => string 'text'

[1] => Array(1)
[1] => Array(1)
[23] => string 'text'

[2] => Array(1)
[@] => Array(1)
[@@] => string 'text'

what output you expect?

[1][[b]edited by[/b]: NomikOS at 1:25 am (utc) on July 4, 2008][/1]

vincevincevince

6:02 am on Jul 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



NomikOS is right... we need a bit more information. Perhaps an example of an input $string and an output $newray?

Incidentally, if this is about storing arrays in databases... use the functions serialize() and unserialize()

Murdoch

8:49 pm on Jul 4, 2008 (gmt 0)

10+ Year Member



Problem was I was going one array too deep.

Last line should've been:

$newray[$cat] = array($val => "text");

Thanks for the answers though.

-Doc