我现在有一个数组,输出如下:
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
(
[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
)
)
)
)
)
)
)
)
我需要做的就是删除那些有int型键的级别。例如,如果没有中间的[0]
, [total_events]
和[events_list]
在同一水平上。对于有[<int>]
键的其他区域也是如此。
我正在使用PHP,并尝试了各种使其扁平化的尝试,但还没有能够完全达到那里。
提前感谢!
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]);
}
}
}
没有真正测试过,但类似的东西应该可以工作
试试这个:
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);
输出: Array
(
[foo] => baa
[test] => ..
)