亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從多維數組中提取同名數組

從多維數組中提取同名數組

PHP
冉冉說 2021-11-05 14:49:55
我有一個來自表示表單的 json 的數組。我必須將所有 [components] 收集到一個新數組中我解決了對 foreach 的認真處理,并在每個循環中使用了邏輯:如果不存在數組“組件”-> 檢查是否是類型列如果是,檢查是否存在“組件”并保存在新數組中如果不保存在新數組中如果再次存在,則對 3 或 4 個子級別應用與第 1 點相同的條件。這是我需要的最后一個數組Array        (            [0] => Array                (                    [key] => number1                    [type] => number                    [input] => 1                    [label] => Test Number Field                )            [1] => Array                (                    [key] => testRadioFiled                    [type] => radio                    [label] => Test Radio Filed                    [values] => Array                        (                            [0] => Array                                (                                    [label] => First                                    [value] => first                                    [shortcut] =>                                 )                        )                )            ......            [2] => Array                (                    [key] => testSelectMultipleField                    [data] => Array                        (                            [values] => Array                                (                                    [0] => Array                                        (                                            [label] => First                                            [value] => first                                        )                                    [1] => Array                                        (                                            [label] => Second                                            [value] => second                                        )
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

如果我理解你正確遞歸可以幫助:


function extractComponents($arrayWithComponents)

{

    $components = [];


    if (!isset($arrayWithComponents['components']) || !is_array($arrayWithComponents['components'])) {

        return $components;

    }


    foreach ($arrayWithComponents['components'] as $component) {

        if (isset($component['columns'])) {

            foreach ($component['columns'] as $column) {

                $components = array_merge($components, extractComponents($column));

            }

        } else {

            $components = array_merge($components, extractComponents($component));

            unset($component['components']);

            $components[] = $component;

        }

    }


    return $components;

}


var_dump(extractComponents($data));


查看完整回答
反對 回復 2021-11-05
  • 1 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號