PHP search array and add content on position where

2019-08-07 11:34发布

I have 2 assoc. Arrays which have the same structure, but only one ID is identical. I need to add content to the MainArray from the IncludeArray everytime the specific ID is identical.

Here are a sample of the Arrays (MainArray could hold up to 100 or more items, the sample contains only a portion of the real content):

$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
      )
 )

The desired new array should be:

 $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.
 )

So eachtime a type_id is found in $MainArray the content of [typetitle] + [typedesc] should be added to the $NewMainArray.

Most of my tests ended in having the Arrays only merged or just added the $IncludeArray only once. Even my Forum Searches don't got me to a solution.

The Arrays are created from 2 separate DB-Requests, which I couldn't join (Already tried several attempts). This is related to different WHERE and AND clauses, which don't work on a joined request.

I hope there is a smart way to get the desired $NewMainArray. BTW I use PHP4 and I'm a newbie to PHP (at least for this kind of problem).

Thanks a lot for your help.

2条回答
闹够了就滚
2楼-- · 2019-08-07 12:10

Try this:


/**
 * 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");

Results:


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
    )

)

查看更多
\"骚年 ilove
3楼-- · 2019-08-07 12:11

Take a look at the array_merge_recursive function ;)

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

查看更多
登录 后发表回答