PHP无法获得正确的数组格式

时间:2014-01-26 17:10:55

标签: php arrays loops

我以某种方式遇到了以下问题: 我想要实现的是基于键合并以下数组:

{"Entities":{"submenu_id":"Parents","submenu_label":"parents"}}
{"Entities":{"submenu_id":"Insurers","submenu_label":"insurers"}}
{"Users":{"submenu_id":"New roles","submenu_label":"newrole"}}
{"Users":{"submenu_id":"User - roles","submenu_label":"user_roles"}}
{"Users":{"submenu_id":"Roles - permissions","submenu_label":"roles_permissions"}}
{"Accounting":{"submenu_id":"Input accounting data","submenu_label":"new_accounting"}}

需要像这样输出:

[{"item_header":"Entities"}, 
 {"list_items" :
    [{"submenu_id":"Parents","submenu_label":"parents"}, 
    {"submenu_id":"Insurers","submenu_label":"insurers"}]
}]
[{"item_header":"Users"}, 
 {"list_items" : 
    [{"submenu_id":"New roles","submenu_label":"newrole"}
    {"submenu_id":"User - roles","submenu_label":"user_roles"}
    {"submenu_id":"Roles - permissions","submenu_label":"roles_permissions"}]
}]
[{"item_header":"Accounting"}, 
 {"list_items" :
    [{"submenu_id":"Input accounting data","submenu_label":"new_accounting"}]
}]

过去两个小时我一直在尝试各种各样的事情,但是每次尝试都返回了不同的格式,因此失败了。不知何故,我无法弄清楚 为了完成这项工作,你有建设吗?
我很想知道你对此事的看法。

感谢。

4 个答案:

答案 0 :(得分:1)

$input = array(
    '{"Entities":{"submenu_id":"Parents","submenu_label":"parents"}}',
    '{"Entities":{"submenu_id":"Insurers","submenu_label":"insurers"}}',
    '{"Users":{"submenu_id":"New roles","submenu_label":"newrole"}}',
    '{"Users":{"submenu_id":"User - roles","submenu_label":"user_roles"}}',
    '{"Users":{"submenu_id":"Roles - permissions","submenu_label":"roles_permissions"}}',
    '{"Accounting":{"submenu_id":"Input accounting data","submenu_label":"new_accounting"}}',
);

$input = array_map(function ($e) { return json_decode($e, true); }, $input);

$result = array();
$indexMap = array();
foreach ($input as $index => $values) {
    foreach ($values as $k => $value) {
        $index = isset($indexMap[$k]) ? $indexMap[$k] : $index;
        if (!isset($result[$index]['item_header'])) {
            $result[$index]['item_header'] = $k;
            $indexMap[$k] = $index;
        }
        $result[$index]['list_items'][] = $value;
    }
}
echo json_encode($result);

答案 1 :(得分:0)

我更喜欢OO方法。

首先是list_item的一个对象:

{"submenu_id":"Parents","submenu_label":"parents"}

第二个item_header的对象:

{"item_header":"Entities", "list_items" : <array of list_item> }

最后一个对象或数组:

{ "Menus: <array of item_header> }

相应的getter / setter等。

答案 2 :(得分:0)

以下代码将为您提供必要的数组,您可以通过该数组进行迭代以获得所需的输出。

$final_array = array();  
foreach($array as $value) { //assuming that the original arrays are stored inside another array. You can replace the iterator over the array to an iterator over input from file       
    $key  =  /*Extract the key from the string ($value)*/
    $existing_array_for_key = $final_array[$key];
    if(!array_key_exists ($key , $final_array)) {
        $existing_array_for_key = array();
    }
    $existing_array_for_key[count($existing_array_for_key)+1] = /*Extract value from the String ($value)*/
    $final_array[$key] = $existing_array_for_key;
}

答案 3 :(得分:0)

你在这里! 在这种情况下,首先我将所有数组添加到一个数组中进行处理 我以为他们先是在同一个阵列,但现在我意识到他们不是。 只需填空$array=[],然后将其全部添加到$array[]=$a1$array[]=$a2等中......

   $array = '[{"Entities":{"submenu_id":"Parents","submenu_label":"parents"}},
   {"Entities":{"submenu_id":"Insurers","submenu_label":"insurers"}},
   {"Users":{"submenu_id":"New roles","submenu_label":"newrole"}},
   {"Users":{"submenu_id":"User - roles","submenu_label":"user_roles"}},
   {"Users":{"submenu_id":"Roles - permissions","submenu_label":"roles_permissions"}},
   {"Accounting":{"submenu_id":"Input accounting data","submenu_label":"new_accounting"}}]';

   $array = json_decode($array, true);


    $intermediate = []; // 1st step

    foreach($array as $a)
    {
       $keys = array_keys($a);
       $key = $keys[0]; // say, "Entities" or "Users"
       $intermediate[$key] []= $a[$key];
    }

    $result = []; // 2nd step

    foreach($intermediate as $key=>$a)
    {
       $entry = ["item_header" => $key, "list_items" => [] ];
       foreach($a as $item) $entry["list_items"] []= $item;
       $result []= $entry;
    }

    print_r($result);