Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在一个键上组合两个数组?_Php_Arrays - Fatal编程技术网

Php 在一个键上组合两个数组?

Php 在一个键上组合两个数组?,php,arrays,Php,Arrays,如果我想通过相同的值将一个数组中的项与另一个数组中的项关联起来,例如items.group\u id->groups.group\u id,是否有一个数组函数可以做到这一点?=) 我有两个阵列: $items = array( [0] => array( 'group_id' => 456, 'item_id' => 123,

如果我想通过相同的值将一个数组中的项与另一个数组中的项关联起来,例如items.group\u id->groups.group\u id,是否有一个数组函数可以做到这一点?=)

我有两个阵列:

$items = array(
              [0] => array(
                          'group_id' => 456,
                          'item_id' => 123,
                          // Rest of details
                     );
              [1] => array(
                          'group_id' => 457,
                          'item_id' => 124,
                          // Rest of details
                     );
              [2] => array(
                          'group_id' => 457,
                          'item_id' => 125,
                          // Rest of details
                     );
              [3] => array(
                          'group_id' => 456,
                          'item_id' => 126,
                          // Rest of details
                     );
          );

$groups = array(
                [0] => array(
                          'group_id' => 456,
                          'group_name' => 'General'
                     );
                [1] => array(
                          'group_id' => 457,
                          'group_name' => 'Ungeneral'
                     );
          );
我想要的结果是:

$groups = array(
                [0] => array(
                          'group_id' => 456,
                          'group_name' => 'General'
                          [0] => array(
                                     'item_id' => 123,
                                     // Rest of details
                                 );
                          [1] => array(
                                     'item_id' => 126,
                                     // Rest of details
                                 );
                     );
                [1] => array(
                          'group_id' => 457,
                          'group_name' => 'Ungeneral'
                          [0] => array(
                                     'item_id' => 124,
                                     // Rest of details
                                 );
                          [1] => array(
                                     'item_id' => 125,
                                     // Rest of details
                                 );
                     );
          );

这可能不太复杂,但我希望有一个简洁的解决方案已经在PHP中实现了!非常感谢您的帮助。

我认为没有内置函数来完成此操作,但下面是一些基本代码:

<?php
    // First, group the items by their group_id
    $items_by_group = array();
    foreach($items as $item) {
        $items_by_group[$item['group_id']][] = $item;
    }

    // Second, go through the groups and if they have any associated items,
    // add them to that group's array.
    foreach($groups as $key => $value) {
        if(isset($items_by_group[$value['group_id']])) {
            foreach($items_by_group[$value['group_id']] as $ikey => $ivalue) {
                unset($ivalue['group_id']);
                $groups[$key][$ikey] = $ivalue;
            }
        }
    }
?>

我不认为有内置函数可以实现这一点,但这里有一些基本代码可以处理它:

<?php
    // First, group the items by their group_id
    $items_by_group = array();
    foreach($items as $item) {
        $items_by_group[$item['group_id']][] = $item;
    }

    // Second, go through the groups and if they have any associated items,
    // add them to that group's array.
    foreach($groups as $key => $value) {
        if(isset($items_by_group[$value['group_id']])) {
            foreach($items_by_group[$value['group_id']] as $ikey => $ivalue) {
                unset($ivalue['group_id']);
                $groups[$key][$ikey] = $ivalue;
            }
        }
    }
?>

如果您按id而不是数字索引为组数组键入关键字,则会更容易

$newArray = array();
foreach($groups as $group) {
  // add the group, and an items array we can append to later.
  $newArray[$group['group_id']] = $group;
  $newArray[$group['group_id']]['items'] = array();  
}
foreach ($items as $item) {
  $newArray[$item['group_id']]['items'][] = $item;
}
应生成如下数组:

$newArray = array(
   [456] => array(
     'group_id' => 456,
     'group_name' => 'General'
     'items' => array(
       [0] => array(
         'item_id' => 123,
         // Rest of details
       );
       [1] => array(
         'item_id' => 126,
         // Rest of details
         );
       );
     );

如果您通过id而不是数字索引为组数组设置键,则会更容易

$newArray = array();
foreach($groups as $group) {
  // add the group, and an items array we can append to later.
  $newArray[$group['group_id']] = $group;
  $newArray[$group['group_id']]['items'] = array();  
}
foreach ($items as $item) {
  $newArray[$item['group_id']]['items'][] = $item;
}
应生成如下数组:

$newArray = array(
   [456] => array(
     'group_id' => 456,
     'group_name' => 'General'
     'items' => array(
       [0] => array(
         'item_id' => 123,
         // Rest of details
       );
       [1] => array(
         'item_id' => 126,
         // Rest of details
         );
       );
     );

我找到了一个适合我的解决方案,在你们的解决方案伙伴们的帮助下

$links_by_group = array();
        foreach($links as $link) {
            $linked_groups = $this->return_linked_groups($link['link_id']);
            foreach($linked_groups as $group){
                $links_by_group[$group][$link['link_id']] = $link;
            }
        }

谢谢你的帮助

我找到了一个适合我的解决方案,在你们的解决方案伙伴的帮助下

$links_by_group = array();
        foreach($links as $link) {
            $linked_groups = $this->return_linked_groups($link['link_id']);
            foreach($linked_groups as $group){
                $links_by_group[$group][$link['link_id']] = $link;
            }
        }
谢谢你的帮助