1

我有 2 副。具有相同结构但只有一个 ID 相同的数组。每次特定 ID 相同时,我都需要从 IncludeArray 向 MainArray 添加内容。

这是数组的一个示例(MainArray 最多可以容纳 100 个或更多项目,该示例仅包含真实内容的一部分):

$MainArray = Array
 (
   [0] => Array
      (
        [item_id] => 1
        [name] => Test1
        [helptxt] => Helptext for item-id 1.
        [type_id] => 1      #could be the same for many other items!!
      )
   [1] => Array
      (
        [item_id] => 2
        [name] => Test2
        [helptxt] => Helptext for item-id 2.
        [type_id] => 2      #could be the same for many other items!!
      )
   and a lot more Arrays
 )


$IncludeArray = Array
(
  [0] => Array
      (
        [type_id] => 1
        [typetitle] => Number
        [typedesc] => Description Text type_id 1
      )
  [1] => Array
      (
        [type_id] => 2
        [typetitle] => Value
        [typedesc] => Description Text type_id 2
      )
 )

所需的新数组应为:

 $NewMainArray = Array
 (
   [0] => Array
      (
        [item_id] => 1
        [name] => Test
        [helptxt] => Helptext for item-id 1.
        [type_id] => 1
        [typetitle] => Number
        [typedesc] => Description Text type_id 1
      )
   [1] => Array
      (
        [item_id] => 2
        [name] => Test2
        [helptxt] => Helptext for item-id 2.
        [type_id] => 2
        [typetitle] => Value
        [typedesc] => Description Text type_id 2
      )
    And so on with all other items in the Array.
 )

因此,每次在 $MainArray 中找到 type_id 时,都应将 [typetitle] + [typedesc] 的内容添加到 $NewMainArray。

我的大多数测试都以仅合并数组或仅添加一次 $IncludeArray 结束。甚至我的论坛搜索也没有让我找到解决方案。

数组是从 2 个单独的 DB-Requests 创建的,我无法加入(已经尝试了几次)。这与不同的 WHERE 和 AND 子句有关,它们不适用于联合请求。

我希望有一种聪明的方法来获得所需的 $NewMainArray。顺便说一句,我使用 PHP4,而且我是 PHP 的新手(至少对于这类问题)。

非常感谢你的帮助。

4

2 回答 2

1

尝试这个:


/**
 * Simulates relational table behaviour by joining data matching an ID value.
 * Works with single-nested arrays.
 *
 * @author Joel A. Villarreal Bertoldi
 *
 * @param  $source           array     The source array.
 * @param  $include_to_row   array     The row where we'll deposit the joined data.
 * @param  $match            string    Unique id field name.
 * 
 * @return array             Row with joined data.
 */

function includeFromArray($source, $include_to_row, $match)
{
  foreach ($source as $source_row)
  {
    if ($source_row[$match] == $include_to_row[$match])
    {
      foreach ($source_row as $source_column_key => $source_column_value)
      {
        $include_to_row[$source_column_key] = $source_column_value;
      }
    }
  }
  return $include_to_row;
}

for ($ma = 0; $ma < count($MainArray); $ma++)
  $NewMainArray[$ma] = includeFromArray($IncludeArray, $MainArray[$ma], "type_id");

结果:


Array
(
    [0] => Array
        (
            [item_id] => 1
            [name] => Test1
            [helptxt] => Helptext for item-id 1.
            [type_id] => 1
            [typetitle] => Number
            [typedesc] => Description Text type_id 1
        )

[1] => Array
    (
        [item_id] => 2
        [name] => Test2
        [helptxt] => Helptext for item-id 2.
        [type_id] => 2
        [typetitle] => Value
        [typedesc] => Description Text type_id 2
    )

)

于 2010-01-23T12:38:01.013 回答
-1

看看 array_merge_recursive 函数;)

http://nl2.php.net/manual/en/function.array-merge-recursive.php

于 2010-01-23T12:11:15.803 回答