1

私は2アソシエイトを持っています。同じ構造を持つが、1 つの 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 を 1 回だけ追加するだけで終了しました。フォーラムで検索しても解決策が見つかりません。

配列は、私が参加できなかった 2 つの個別の DB 要求から作成されます (既にいくつかの試行を試みました)。これは、結合されたリクエストでは機能しない、さまざまな 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 に答える