2014-01-23 147 views
-1

我得到我的輸出是這樣的:刪除索引鍵從一個數組

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

而你在做什麼得出結果? – Pietu1998

+1

似乎所有你需要做的是寫一個簡單的循環。什麼阻止你? – Barmar

+0

也許你應該修復創建原始數組的代碼。這似乎是一個非常愚蠢的組織方式。 – Barmar

回答

0

array_merge()刪除鍵和重指數的陣列,但+運營商,合併兩個數組沒有重建索引,那就是你想要的。

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

結果是這樣的

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

非常感謝。 Ur解決方案適用於我。 – chintsk

+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

'array_merge()'重置鍵,代碼是錯誤的。 –

+0

是的,數字鍵。忘記了(在字符串上測試)。我會更新 –

+1

不,它沒有。請使用現代PHP版本。 –