-1

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?

2
  • How do you create this array? Is it just statically, or from some external source (database,xml) Commented Sep 9, 2011 at 10:10
  • 1
    What do the variables $name $street0 and $street1 look like ? Commented Sep 9, 2011 at 10:19

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please explain your regexp, it's not that intuitive

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.