簡體   English   中英

將連續數組拆分為子數組

[英]Split continuous array into subarrays

我正在嘗試轉換格式如下的數組:

object(Categories_store_tree)#519 (1) { 
    ["list_of_sections":"Categories_store_tree":private]=> array(5) {                  
        ["id"]=> int(1) 
        ["name"]=> string(11) "Main Store" 
        ["parent_id"]=> NULL 
        ["children"]=> array(2) { 
            [0]=> array(5) { 
                ["id"]=> int(2) 
                ["name"]=> string(4) "Food" 
                ["parent_id"]=> int(1) 
                ["children"]=> array(0) { } 
            } 
            [1]=> array(5) { 
                ["id"]=> int(3) 
                ["name"]=> string(14) "Electronics" 
                ["parent_id"]=> int(1) 
                ["children"]=> array(2) { 
                    [0]=> array(5) { 
                        ["id"]=> int(4) 
                        ["name"]=> string(8) "Headphones" 
                        ["parent_id"]=> int(3) 
                        ["children"]=> array(0) { } 
                    } 
                    [1]=> array(5) { 
                        ["id"]=> int(5) 
                        ["name"]=> string(5) "Smartphones" 
                        ["parent_id"]=> int(3) 
                        ["children"]=> array(0) { } 
                    } 
                } 
            } 
        } 
    } 
} 

對於這種數組結構:

object(Categories_store_tree)#964 (1) { 
    ["list_of_sections":"Categories_store_tree":private]=> array(5) { 
        [0]=> array(4) { 
            ["id"]=> int(1) 
            ["name"]=> string(11) "Main Store" 
            ["parent_id"]=> NULL 
        } 
        [1]=> array(4) { 
            ["id"]=> int(2) 
            ["name"]=> string(4) "Food" 
            ["parent_id"]=> int(1) 
        } 
        [2]=> array(4) { 
            ["id"]=> int(3) 
            ["name"]=> string(14) "Electronics" 
            ["parent_id"]=> int(1) 
        } 
        [3]=> array(4) { 
            ["id"]=> int(4) 
            ["name"]=> string(8) "Headphones" 
            ["parent_id"]=> int(3) 
        } 
        [4]=> array(4) { 
            ["id"]=> int(5) 
            ["name"]=> string(5) "Smartphones" 
            ["parent_id"]=> int(3) 
        } 
    } 
}

目前我正在手動操作,但想法是使其自動化。 我嘗試過使用此代碼,但它返回一個空數組,我也嘗試過使用 function 但我被困了幾天,不知道該怎么辦。

$clean_array = array();
            $cont = 0;
            foreach ( $new_tree as $key => $value ) {
                if ( is_array( $value ) ) {
                    $cont++;
                    foreach ( $value as $key1 => $value1 ) {
                        if ( is_array( $value1 ) ) {
                            $cont++;
                            foreach ( $value1 as $key2 => $value2 ) {
                                $clean_array[$cont][$key2] = $value2;
                            }
                        } else {
                            $clean_array[$cont][$key1] = $value1;
                        }
                    }
                } else {
                    $clean_array[$cont][$key] = $value;
                }
            }

也許你可以嘗試這樣的事情。

將 object 轉換為數組:

function objectToArray($d) {
    if (is_object($d))
       $d = get_object_vars($d);
       return is_array($d) ? array_map(__METHOD__, $d) : $d;
}

然后:

$arr = objectToArray($object);
print_r($arr);

資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM