开发者

Flatten a PHP Array one level based on key

I currently have an array that's outputting as follows:

Array
(
  [events_response] => Array
    (
      [total_events] => 3
      [0] => Array
        (
          [events_list] => Array
            (
              [0] => Array
                (
                  [event] => Array
                    (
                      [id] => 41
                      [ages] => All
                      [buy_link] =>
                      [created] => 2011-06-06 22:13:02
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 7
                              [name] => Metrodome
                              [website] =>
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => 123 Any Street
                                      [city] => My City
                                      [state_province] =>
                                      [postal_code] => 12345
                                      [country] =>
                                    )
                                )
                            )
                        )
                      [1] => Array
                        (
                          [collaborators_list] => Array
                            (
                              [0] => Array
                                (
                                  [collaborator] => Array
                                    (
                                      [id] => 3
                                      [name] => derp
                                      [website] => http://derp.com/
                                    )
                                )
                              [1] => Array
                                (
                                  [collaborator] => Array
                                    (
                                      [id] => 4
                                      [name] => "Foo" Bar
                                      [website] => http://www.foobar.com/
                                    )
                                )
                            )
                        )
                      [2] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
              [1] => Array
                (
                  [event] => Array
                    (
                      [id] => 64
                      [ages] => 21+
                      [buy_link] => dsfdsaf
                      [created] => 2011-07-05 21:35:52
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
               开发者_如何学C         (
                          [location] => Array
                            (
                              [id] => 8
                              [name] => name
                              [website] => website.com
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => street address
                                      [city] => city
                                      [state_province] => WI
                                      [postal_code] => 53103
                                      [country] => USA
                                    )
                                )
                            )
                        )
                      [collaborators_list] =>
                      [1] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
              [2] => Array
                (
                  [event] => Array
                    (
                      [id] => 65
                      [ages] => 21+
                      [buy_link] => dsfdsaf
                      [created] => 2011-07-05 21:36:12
                      [date] => 2012-06-06 07:00 pm
                      [description] =>
                      [price] => 0.00
                      [0] => Array
                        (
                          [location] => Array
                            (
                              [id] => 8
                              [name] => name
                              [website] => website.com
                              [0] => Array
                                (
                                  [address] => Array
                                    (
                                      [street_address] => street address
                                      [city] => city
                                      [state_province] => WI
                                      [postal_code] => 53103
                                      [country] => USA
                                    )
                                )
                            )
                        )
                      [collaborators_list] =>
                      [1] => Array
                        (
                          [account] => Array
                            (
                              [id] => 1
                              [account_name] => Brand New
                            )
                        )
                    )
                )
            )
        )
    )
)

What I need to do is basically remove the levels where there is a key that is an int. So, for instance, [total_events] would be on the same level as [events_list] without that [0] in between. The same goes for other areas where there is a [<int>] key.

I'm using PHP and have tried various attempts at flattening it but haven't been able to quite get there.

Thanks in advance!


function flattenByRemoveingIntKeys($array)
{
    foreach ($array as $key => $val)
    {
        if (is_array($val))
            $array[$key] = flattenByRemoveingIntKeys($val);
        else if (is_numeric($key))
        {
            $array .= $val[$key];
            unset($array[$key]);
        }
    }
}

Not really tested but something like that should work.


Try this:

function RemoverIntKeys($array) { 
   foreach($array as $key => $value) {
        if(preg_match("/^\d+$/",$key)) {
         unset($array[$key]); 
       }
  }
           return $array;
}

$array = array("foo" => "baa", 2 => "hehe", "test" => "..", 4 => "hello"); 
$newarray = RemoverIntKeys($array); 
echo "<pre>";
print_r($newarray); 

Output:

 Array
    (
        [foo] => baa
        [test] => ..
    )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜