有没有办法根据密钥中的相同值加入两个数组? 作为MySQL中的一个例子,当两个字段具有相同的值时,您可以将两个表连接起来。
第一个名为'phoneArr'的阵列是一个带有person_id和电话号码的阵列 名为“clientDate”的第二个数组是一个具有person_id和约会日期的数组。
以下是数组:
$phoneArr = array();
$phoneArr[0]['person_id'] = "123456";
$phoneArr[0]['phone'] = "555-2222";
$phoneArr[1]['person_id'] = "7654321";
$phoneArr[1]['phone'] = "555-1111";
$clientDate = array();
$clientDate[0]['person_id'] = "123456";
$clientDate[0]['date_time'] = "01-07-13 13:00";
$clientDate[1]['person_id'] = "7654321";
$clientDate[1]['date_time'] = "01-07-13 10:30";
现在,如果clientDate中的人员ID将始终存在于phoneArr中,而不是其他的人。 phoneArr中的人并不总是存在于clientDate中。
我想要的是获得这些数组的匹配,我将留下一个包含两个数组信息的新数组,但只有'person_id'上有匹配。
没有MySQL,这可行吗?
答案 0 :(得分:1)
如果您要按键值查找数据,并且该键值在表格上是唯一的,您可以考虑将其用作数组键,因此您的$ phoneArr将设置为:
$phoneArr["123456"]['phone'] = "555-2222";
$phoneArr["7654321"]['phone'] = "555-1111";
对其他数组执行相同操作
然后你可以:
foreach ($phoneArr AS $id=>$record) {
if (array_key_exists($id,$clientDate)) {
// the client id is $id, $record has their phone #, and $clientDate[$id] has their date/time
// do something with it here - either process it (preferable) or put the data in another array.
}
}