douzhengzuo7283 2018-06-28 08:17
浏览 86
已采纳

如何根据特定的键值从数组2中将数据提取到数组1中?

I have two arrays.

$primary = array(
           [0] => array(
                     'name' => 'PPT Shop',
                     'place_id'   => '1000',
                     'category' => '220,221',
                     'address' =>
                   ),  
           [1] => array(
                     'name' => 'Meat Shop',
                     'place_id'   => '1001',
                     'category' => '220,221'
                     'address' =>
                   ),
           [2] => array(
                     'name' => 'Bikini Shop',
                     'place_id'   => '1010',
                     'category' => '100,102'
                     'address' =>
                   ),
           [3] => array(
                     'name' => 'Knife Shop',
                     'place_id'   => '1012',
                     'category' => '1,3'
                     'address' =>
                   )
)

$moredata = array(
           [0] => array(
                     'id'   => '1000',
                     'category' => '900,901'
                     'address' => '35 Lawrence Park',
                     'phone'  => '9000000099'
                   ),  
           [1] => array(
                     'id'   => '1001',
                     'category' => '909,300'
                     'address' => '39 Park Avenue',
                   ),
           [2] => array(
                     'id'   => '1010',
                     'category' => '50,45'
                     'address' => '35 Trump Park',
                     'phone'  => '8900000099'
                   )
)

I want to compare each data of $moredata with each data of $primary, and check if the place_id from $primary exists in $moredata. If it matches, then the corresponding records of that particular key will be updated. For example,

$newPrimary = array(
           [0] => array(
                     'name' => 'PPT Shop',
                     'place_id'   => '1000',
                     'category' => '220,221,900,901',
                     'address' => '35 Lawrence Park',
                     'phone'  => '9000000099'
                   ),  
           [1] => array(
                     'name' => 'Meat Shop',
                     'place_id'   => '220,221,1001',
                     'category' => '220,221,909,300',
                     'address' => '39 Park Avenue',
                   ),
           [2] => array(
                     'name' => 'Bikini Shop',
                     'place_id'   => '1010',
                     'category' => '100,102,50,45'
                     'address' => '35 Trump Park',
                     'phone'  => '8900000099'
                   ),
           [3] => array(
                     'name' => 'Knife Shop',
                     'place_id'   => '1012',
                     'category' => '1,3'
                     'address' =>
                   )
)

place_id(1000) of primary matches with id(1000) of moredata, so the place_id(1000) of newPrimary is like this:-

array(
     'name' => 'PPT Shop',
     'place_id'   => '1000',
     'category' => '220,221,900,901', // the categories get concated
     'address' => '35 Lawrence Park',
     'phone'  => '9000000099'
)

However, for place_id(1001) of primary doesn't have phone field, so the id(1001) of newPrimary is like this:-

array(
     'name' => 'Meat Shop',
     'place_id'   => '1001',
     'category' => '220,221,909,300', 
     'address' => '39 Park Avenue',
)

place_id(1012) has no match, so it remains unchanged.

How can I create an array similar to newPrimary? It would be better if we can append the fields from moredata to the corresponding record from primary. I achieved the same using double foreach loop. I want to achieve this is a way to have lesser execution time.

foreach($primary as $key_fs => $prm)
{
    foreach($moredata as $key_place => $pc)
    {
        if($prm['place_id'] == $pc['id'])
        {
            if(isset($pc['address']) && !empty($pc['address']))
                $primary[$key_fs]['address']  = $pc['address'];


            if(isset($pc['phone']) && !empty($pc['phone']))
                $primary[$key_fs]['phone']  = $pc['phone'];

            if(isset($pc['category']) && !empty($pc['category']))
               $primary[$key_fs]['category']  .= ','.$pc['category'];

            break;
        }
    }
}

Note:- The two arrays will have same dimension, but may not have same order.

  • 写回答

1条回答 默认 最新

  • dongqian6554 2018-06-28 08:30
    关注

    You can use array_column to make the 2 arrays into multidimensional array. Use array_replace_recursive to merge the arrays.

    You can use array_values if you want a simple array instead of multi dimensional output.

    $primary = //Your array
    $moredata  = //Your array
    $result = array_replace_recursive (array_column($primary, null, 'id') ,array_column($moredata, null, 'id') );
    $result = array_values($result); //To convert the multidimensional array to simple array
    

    Update:

    You can use array_column to make a temp array for $moredata. This will make it easier to check if the id exist. Use the foreach to loop thru the array. If id exist on $moredata, simply concatenate the category.

    $newMoreData = array_column($moredata, null, 'id');
    $newPrimary = array();
    
    foreach( $primary as $val ) {
        if ( isset( $newMoreData[$val['place_id']] ) ) {
            $temp = array_merge( $val, $newMoreData[$val['place_id']] );
            $temp['category'] = $val['category'] . ',' . $newMoreData[$val['place_id']]['category'];
            unset($temp['id']);
            $newPrimary[] = $temp;
        } else {
            $newPrimary[] = $val;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办