mysql键值数组怎么使用,PHP在键值上匹配两个数组(例如mysql join)

Is there a way to join two arrays based upon a same value in a key?

As an example in MySQL you can left join two tables when two fields have the same value in it.

The first array called 'phoneArr' is one with a person_id and a phone number

The second array called 'clientDate' is one with a person_id and a appointment date.

Here are the arrays:

$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";

Now if the person id in clientDate will always exist in the phoneArr, but not the other wat around. The persons in the phoneArr do not always exist in the clientDate.

What I want is to get a match of these arrays where I will be left with a new array with the info of both arrays, but only of there is a match on the 'person_id'.

Is this doable without MySQL?

解决方案

If you are going to look up data by a key value, and that key value is unique over the table, you might consider using that as the array key, so your $phoneArr would be set up as:

$phoneArr["123456"]['phone'] = "555-2222";

$phoneArr["7654321"]['phone'] = "555-1111";

Do the same with the other array

Then you can:

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.

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值