2016-11-24 104 views
-2

我有一個多維數組如下重新排列數組索引

Array 
    (
     [0] => attributewidget Object 
      (
       [_child] => Array 
        (
        ) 

       [_parent] => 
       [_oid] => a_1 
       [_errorMsg] => 
       [_debugTraceFunc] => 
       [classname] => AttributeWidget 
       [version] => 1.0.0.0 
       [attributeName] => 
       [comparableProperties] => Array 
        (
         [0] => abbreviation 
         [1] => attributeName 
         [2] => classname 
         [3] => conditionRef 
        ) 

      ) 

     [1] => Array 
      (
       [0] => attributewidget Object 
        (
         [_child] => Array 
          (
          ) 

         [_parent] => 
         [_oid] => a_2 
         [_errorMsg] => 
         [_debugTraceFunc] => 
         [classname] => AttributeWidget 
         [version] => 1.0.0.0 
         [attributeName] => 
         [comparableProperties] => Array 
          (
           [0] => abbreviation 
           [1] => attributeName 
           [2] => classname 
           [3] => conditionRef 
          ) 

        ) 

      ) 
     [2] => Array 
     (
      [0] => attributewidget Object 
       (
        [_child] => Array 
         (
         ) 

        [_parent] => 
        [_oid] => a_3 
        [_errorMsg] => 
        [_debugTraceFunc] => 
        [classname] => AttributeWidget 
        [version] => 1.0.0.0 
        [attributeName] => cp1 
        [comparableProperties] => Array 
         (
          [0] => abbreviation 
          [1] => attributeName 
          [2] => classname 
          [3] => conditionRef 
         ) 

       ) 

     ) 
    ) 

我理想想刪除[1] =>數組[2] =>數組和重新排列的索引。 有沒有一種方法可以讓索引重新排列如下。

Array 
    (
     [0] => attributewidget Object 
      (
       [_child] => Array 
        (
        ) 

       [_parent] => 
       [_oid] => a_1 
       [_errorMsg] => 
       [_debugTraceFunc] => 
       [classname] => AttributeWidget 
       [version] => 1.0.0.0 
       [attributeName] => 
       [comparableProperties] => Array 
        (
         [0] => abbreviation 
         [1] => attributeName 
         [2] => classname 
         [3] => conditionRef 
        ) 

      ) 

       [1] => attributewidget Object 
        (
         [_child] => Array 
          (
          ) 

         [_parent] => 
         [_oid] => a_2 
         [_errorMsg] => 
         [_debugTraceFunc] => 
         [classname] => AttributeWidget 
         [version] => 1.0.0.0 
         [attributeName] => Name? 
         [comparableProperties] => Array 
          (
           [0] => abbreviation 
           [1] => attributeName 
           [2] => classname 
           [3] => conditionRef 
          ) 

        ) 



      [2] => attributewidget Object 
       (
        [_child] => Array 
         (
         ) 

        [_parent] => 
        [_oid] => a_3 
        [_errorMsg] => 
        [_debugTraceFunc] => 
        [classname] => AttributeWidget 
        [version] => 1.0.0.0 
        [attributeName] => cp1 
        [comparableProperties] => Array 
         (
          [0] => abbreviation 
          [1] => attributeName 
          [2] => classname 
          [3] => conditionRef 
         ) 

       ) 


    ) 
+2

這個數組是如何創建得如此不一致?爲什麼'[0]'直接指向一個對象,但是'[1]'和'[2]'中有一個額外的數組?也許你應該修復創建數組的代碼,所以它不會這樣做。 – Barmar

+0

臨發佈提示:[避免請求志願者緊急](http://meta.stackoverflow.com/q/326569)。 – halfer

回答

2

循環遍歷數組,並用第一個元素替換嵌套數組中的任何元素,以刪除額外的間接級別。

foreach ($array as &$element) { 
    if (is_array($element) { 
     $element = $element[0]; 
    } 
}