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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?