2015-07-20 118 views
2

我有以下陣列,分割多維數組兩

Array 
(
    [0] => stdClass Object 
     (
      [test] => 0 
     ) 

    [1] => stdClass Object 
     (
      [73] => stdClass Object 
       (
        [test1] => stdClass Object 
         (
          [96] => 6 
          [116] => 0 
         ) 

       ) 

     ) 

    [2] => stdClass Object 
     (
      [73] => stdClass Object 
       (
        [test1] => stdClass Object 
         (
          [96] => 6 
          [116] => 0 
         ) 

       ) 

     ) 

) 

我想陣列拆分到兩個相同

Array 
(
    [0] => stdClass Object 
     (
      [test] => 0 
     ) 
); 

Array 
(
    [0] => stdClass Object 
     (
      [73] => stdClass Object 
       (
        [test1] => stdClass Object 
         (
          [96] => 6 
          [116] => 0 
         ) 

       ) 

     ) 

    [1] => stdClass Object 
     (
      [73] => stdClass Object 
       (
        [test1] => stdClass Object 
         (
          [96] => 6 
          [116] => 0 
         ) 

       ) 

     ) 

); 

怎麼辦呢?請幫忙。我曾嘗試array_slice和它拆分數組,但生成像Warning: array_slice() expects parameter 1 to be array, null given.

警告是否可以,如果生成警告?

回答

4

只需使用array_shift和所有做

$new_arr = array_shift($your_current_array); 

print_r($new_arr); 
print_r($your_current_array); 
+0

是的,但同樣的事情發生overhere,它從數組中刪除第一個元素,但生成像警告警告:array_shift()期望參數1爲數組,null。這背後的原因是什麼,它會影響動態的情況嗎? –

+0

因爲你傳遞了一個空數組,所以你需要檢查條件爲if(is_array($ array)&& count($ array)> 0)' –

1

在這種情況下,你可以只array_shift第一陣列出來:

$array1 = array_shift($array); 

這是你的輸入數組將只包含其餘項目後(這是你想要什麼)。