PHP搜索数组,对其中id的是平等的位置添加内容 [英] PHP search array and add content on position where id's are equal
问题描述
我有2个协会。其具有相同的结构,但只有一个ID是相同的阵列。我需要的内容从IncludeArray每次具体的ID是一样添加到MainArray。
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.
下面是阵列的一个样品(MainArray可容纳多达100个或更多的项目,样品仅包含实际内容的一部分):
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.
)
所以eachtime一个TYPE_ID在$ MainArray发现的[typetitle]的含量+ [TYPEDESC]应当加到$ NewMainArray
So eachtime a type_id is found in $MainArray the content of [typetitle] + [typedesc] should be added to the $NewMainArray.
我的大多数测试中仅有合并或刚刚添加的$ IncludeArray只有一次阵列结束。即使我的论坛搜索不会让我一个解决方案。
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.
该阵列由2个独立的DB-请求,这是我无法加入创建(已经尝试过多次尝试)。这涉及到不同的WHERE和AND子句,不上加入请求工作。
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.
我希望有一个聪明的方式来获得所需的$ NewMainArray。 BTW我用PHP4和我是一个新手到PHP(至少对于这种问题)。
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).
非常感谢你的帮助。
推荐答案
试试这个:
/**
* 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
)
)
这篇关于PHP搜索数组,对其中id的是平等的位置添加内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!