简体   繁体   English

PHP:使用值作为键从多维数组创建二维数组

[英]PHP: create 2 dimensional array from multidimensional array with value as key

I try to create a new array from an complex array. 我尝试从复杂的数组创建一个新数组。 If there's no easy solution, every hint would help to search more successful. 如果没有简单的解决方案,则每个提示都将有助于搜索更成功的项目。

I have this complex array (shortened a lot) and want to build a new 2 dimensional array: 我有这个复杂的数组(缩短了很多),想建立一个新的二维数组:

array (
    [key1] => value1
    [key2] => value2
    [category] => array (
        [key3] => value3
        [key4] => array (
            [small] => value6
            [large] => value7 
            )
        [items] => array (
            [0] => array (
                [aTag] => #PU2RRL
                [name] => 3PL
                [position] => 25
                [versions] => array (
                    [0] => array (
                        [bTag] => #KF67RL
                        [color] => blue
                        [id] => 001
                        )
                    [1] => array (
                        [bTag] => #Z8TR4
                        [color] => red
                        [id] => 002
                        )
                )
            )
            [1] => array (
              ...

This is the array I want to create: 这是我要创建的数组:

array(
  [001] => array (
    [aTag] => #PU2RRL
    [name] => 3PL
    [position] => 25
    [bTag] => #KF67RL
    [color] => blue
  )
  [002] => array (
    [aTag] => #PU2RRL
    [name] => 3PL
    [position] => 25
    [bTag] => #Z8TR4
    [color] => blue))

With ID as key and this values: 以ID为键,其值为:

$itemAll = array(
  $array[category][items][0][versions][0][id]  => array(
    "aTag" => $array[category][items][0][aTag],
    "name" =>  $array[category][items][0][name],
    "position" =>  $array[category][items][0][position],
    "bTag" => $array[category][items][0][versions][0][bTag],
    "color" => $array[category][items][0][versions][0][color],
  )
);

I have no clue how to create this array with foreach loops for "items" and versions with the ID as primary key, any hints? 我不知道如何使用foreach循环为“项目”和以ID作为主键的版本创建此数组,是否有任何提示?


EDIT: huge thanks to @DeeDuu! 编辑:非常感谢@DeeDuu! Because I had multiple items I added another foreach: 因为我有多个项目,所以我添加了另一个foreach:

$new_array = array();
// i have multiple items, so I removed [0]:
$items = $array["category"]["items"];
// added another foreach
foreach ($items as $item) {
  // changed $items to $item
  $shared_properties = array(
  "aTag" => $item["aTag"],
  "name" => $item["name"],
  "position" => $item["position"]
  );
  // changed $items to $item
  foreach ($item["versions"] as $version) {
  $specific_properties = array(
      "bTag" => $version["bTag"],
      "color" => $version["color"]
  );
  $new_entry = array_merge(
      $shared_properties,
      $specific_properties
  );
  $new_array[$version["id"]] = $new_entry;  }}

If I understand correctly what you want, something like the following should work. 如果我正确理解您想要的内容,则应执行以下操作。

 // This is the target array where we will save the recombinated data
 $new_array = array();

 // Store the relevant subarray in a new variable;
 // This makes the subsequent code shorter
 $items = $array["category"]["items"][0];

 // This gives the data that will be common to all the new entries,
 // for all versions seem to share aTag, name and position
 $shared_properties = array(
     "aTag" => $items["aTag"],
     "name" => $items["name"],
     "position" => $items["position"]
 );

 // Alright, let's loop over the versions
 foreach ($items["versions"] as $version) {
     // Another array for the data that is specific
     // to the version we're currently looking at
     $specific_properties = array(
         "bTag" => $version["bTag"],
         "color" => $version["color"]
     );

     // The new entry is a combination of the shared and the
     // specific properties; array_merge will do that for us
     $new_entry = array_merge(
         $shared_properties,
         $specific_properties
     );

     // Now add to the new array
     $new_array[$version["id"]] = $new_entry;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM