从数组中删除索引键

时间:2014-01-23 06:07:30

标签: php arrays multidimensional-array

我的输出是这样的:

Array(
0 => Array(10593 => Array("title" => 'Contact', "desc" => '')),
1 => Array(11468 => Array("title" => 'Sign Up', "desc" => 'Newsletter sign up')),
2 => Array(19187 => Array("title" => 'Health', "desc" => 'This is your form description. Click here to edit.')),
3 => Array(25707 => Array("title" => 'Sample Form', "desc" => 'This is Sample Form.')),
4 => Array(27027 => Array("title" => ' ksdfmlskdfmlskdfmlsdfm', "desc" => 'This is your form description. Click here to edit')),
5 => Array(27908 => Array("title" => 'dkjnkxcjnxckjnv ', "desc" => 'This is your form description. Click here to edit.')),
6 => Array(28164 => Array("title" => 'test ', "desc" => 'This is your form description. Click here to edit.'))
);

我想从当前数组输出中删除所有索引键,我该如何操作它?请建议。

输出应为:

Array(
    10593 => Array("title" => 'Contact', "desc" => ''),
    11468 => Array("title" => 'Sign Up', "desc" => 'Newsletter sign up'),
    19187 => Array("title" => 'Health', "desc" => 'This is your form description. Click here to edit.'),
    25707 => Array("title" => 'Sample Form', "desc" => 'This is Sample Form.'),
    27027 => Array("title" => ' ksdfmlskdfmlskdfmlsdfm', "desc" => 'This is your form description. Click here to edit'),
    27908 => Array("title" => 'dkjnkxcjnxckjnv ', "desc" => 'This is your form description. Click here to edit.'),
    28164 => Array("title" => 'test ', "desc" => 'This is your form description. Click here to edit.'),
);

2 个答案:

答案 0 :(得分:2)

对于字符串键,您可以使用array_merge()call_user_func_array(),如下所示:

//$data is your input, $result is your output
$result = call_user_func_array('array_merge', $data);

但是,如下所述,array_merge()将重置(重新索引)数字键。那么你可以使用array_reduce()

$result = array_reduce($data, function(&$cur, $x)
{
   return $cur+=$x;
}, []);

答案 1 :(得分:0)

array_merge()删除键并重新索引数组,但+运算符合并两个数组而不重新编制索引,这就是你想要的。

$new = array();
foreach ($yourarray as $arr){
    $new = $new + $arr;
}
var_export($new);

结果是这样的

array (
  10593 => 
  array (
    'title' => 'Contact',
    'desc' => '',
  ),
  ...