vinu
vinu

Reputation: 1

How to convert one dimensional array into multidimensional?

i have an array like

$arr($name=>"vinu",$street0=>"xxx",$street1=>"yyy").

i need to convert this as

$arr1($name=>"vinu",$street=>array("xxx","yyy")).

How can i do this?

Upvotes: 0

Views: 118

Answers (1)

user187291
user187291

Reputation: 53960

like

foreach($ary as $k => $v)
    if(preg_match("~(.+?)(\d+)$~", $k, $m))
        $out[$m[1]][$m[2]] = $v;
    else
        $out[$k] = $v;

basically, if a key is "something and digits" put its value into result[something][digits] otherwise simply copy the value into the result array

Upvotes: 3

Related Questions