简体   繁体   English

如何在一个数组php中合并两个数组值

[英]how to merge two arrays values in one array php

I want to merge values of two array in to one array. 我想将两个数组的值合并到一个数组中。

i am getting below output... 我低于输出...

{
  "responseData": {    
    "result": [
      {
        "0": {
          "distance": "0"
        },
        "user_details": {
          "id": "13",
          "courier_address": "smartData Enterprises (I) Ltd., MIHAN, Nagpur, Nagpur, Maharashtra, India",
          "courier_lat": "21.0371687",
          "courier_long": "79.01362560000007"
        }
      },
      {
        "0": {
          "distance": "1.243540768279295"
        },
        "user_details": {
          "id": "14",
          "courier_address": "TCS, MIHAN, Nagpur, Telhara, Maharashtra, India",
          "courier_lat": "21.0540494",
          "courier_long": "79.02031090000003"
        }
      },
      {
        "0": {
          "distance": "1.578268494523629"
        },
        "user_details": {
          "id": "15",
          "courier_address": "Globallogic MIHAN SEZ Centre, MIHAN, Nagpur, Dahegaon, Maharashtra, India",
          "courier_lat": "21.0487625",
          "courier_long": "79.03471179999997"
        }
      }
    ]
  }
}

In my above data,the distance is coming in another arrays.. but i want to merge the values of distance is user_details array... 在我上面的数据中,距离正在另一个数组中..但是我要合并的距离的值是user_details数组...

Expected output 预期产量

{
  "responseData": {    
    "result": [
      {

        "user_details": {
          "id": "13",
          "courier_address": "smartData Enterprises (I) Ltd., MIHAN, Nagpur, Nagpur, Maharashtra, India",
          "courier_lat": "21.0371687",
          "courier_long": "79.01362560000007",
          "distance": "0"
        }
      },
      {

        "user_details": {
          "id": "14",
          "courier_address": "TCS, MIHAN, Nagpur, Telhara, Maharashtra, India",
          "courier_lat": "21.0540494",
          "courier_long": "79.02031090000003",
           "distance": "1.243540768279295"
        }
      },
      {

        "user_details": {
          "id": "15",
          "courier_address": "Globallogic MIHAN SEZ Centre, MIHAN, Nagpur, Dahegaon, Maharashtra, India",
          "courier_lat": "21.0487625",
          "courier_long": "79.03471179999997",
          "distance": "1.578268494523629"
        }
      }
    ]
  }
}

Below is my code 下面是我的代码

public function searchCourier(){      
    if($this->processRequest){
        $err = false; 
        if(empty($this->requestData['lat'])){
            $this->responceData['message'] = "Please enter latitude";
            $err = true;
        }
        if(empty($this->requestData['long'])){
            $this->responceData['message'] = "Please enter longitude";
            $err = true;
        }
        if(!$err){              
            $lat = $this->requestData['lat'];               
            $long = $this->requestData['long'];
            $this->loadModel('UserDetail'); 
            $data = $this->UserDetail->query("SELECT id,courier_address,courier_lat,courier_long,( 3959 * acos( cos( radians( '$lat' ) ) * cos( radians( courier_lat ) ) * cos( radians( courier_long ) - radians( '$long' ) ) + sin( radians( '$lat' ) ) * sin( radians( courier_lat ) ) ) ) AS distance
            FROM user_details HAVING distance <= 5 ORDER BY distance LIMIT 0 , 20");                
            $this->responceData['result'] = $data;
            $this->responceData['status'] = 1;
            $this->responceData['message'] = "Success";

        } else {
            $this->responceData['status'] = 0;
            $this->responceData['message'] = "Something Went Wrong.Please Try again";           
        }              
    }
}

output of var_dump($data) var_dump($ data)的输出

 array(3) {
  [0]=>
  array(2) {
    ["user_details"]=>
    array(4) {
      ["id"]=>
      string(2) "13"
      ["courier_address"]=>
      string(73) "smartData Enterprises (I) Ltd., MIHAN, Nagpur, Nagpur, Maharashtra, India"
      ["courier_lat"]=>
      string(10) "21.0371687"
      ["courier_long"]=>
      string(17) "79.01362560000007"
    }
    [0]=>
    array(1) {
      ["distance"]=>
      string(1) "0"
    }
  }
  [1]=>
  array(2) {
    ["user_details"]=>
    array(4) {
      ["id"]=>
      string(2) "14"
      ["courier_address"]=>
      string(47) "TCS, MIHAN, Nagpur, Telhara, Maharashtra, India"
      ["courier_lat"]=>
      string(10) "21.0540494"
      ["courier_long"]=>
      string(17) "79.02031090000003"
    }
    [0]=>
    array(1) {
      ["distance"]=>
      string(17) "1.243540768279295"
    }
  }
  [2]=>
  array(2) {
    ["user_details"]=>
    array(4) {
      ["id"]=>
      string(2) "15"
      ["courier_address"]=>
      string(73) "Globallogic MIHAN SEZ Centre, MIHAN, Nagpur, Dahegaon, Maharashtra, India"
      ["courier_lat"]=>
      string(10) "21.0487625"
      ["courier_long"]=>
      string(17) "79.03471179999997"
    }
    [0]=>
    array(1) {
      ["distance"]=>
      string(17) "1.578268494523629"
    }
  }
}

try this code. 试试这个代码。 hope it helps 希望能帮助到你

public function searchCourier(){      
    if($this->processRequest){
        $err = false; 
        if(empty($this->requestData['lat'])){
            $this->responceData['message'] = "Please enter latitude";
            $err = true;
        }
        if(empty($this->requestData['long'])){
            $this->responceData['message'] = "Please enter longitude";
            $err = true;
        }
        if(!$err){
            $lat = $this->requestData['lat'];
            $long = $this->requestData['long'];
            $this->loadModel('UserDetail'); 
            $data = $this->UserDetail->query("SELECT id,courier_address,courier_lat,courier_long,( 3959 * acos( cos( radians( '$lat' ) ) * cos( radians( courier_lat ) ) * cos( radians( courier_long ) - radians( '$long' ) ) + sin( radians( '$lat' ) ) * sin( radians( courier_lat ) ) ) ) AS distance
            FROM user_details HAVING distance <= 5 ORDER BY distance LIMIT 0 , 20");
            $this->responceData['result'] = array();
            // check if has data returned
            if($data) {
                foreach($data as $val) {
                    // try to var_dump or print_r the $val
                    // print_r($val);
                    // if $val data is = array(
                    //    'UserDetails' => array(
                    //        "id" => "15",
                    //       "courier_address" => "Globallogic MIHAN SEZ Centre, MIHAN, Nagpur, Dahegaon, Maharashtra, India",
                    //        "courier_lat" => "21.0487625",
                    //        "courier_long" => "79.03471179999997"
                    //    ),
                    //    0 => array(
                    //        "distance" => 123.3
                    //    )
                    // );

                    // store data as array
                    $this->responceData['result'][] = array(
                        'user_details' => array(
                            "id"=> $val['UserDetails']['id'],
                            "courier_address"=> $val['UserDetails']['courier_address'],
                            "courier_lat"=> $val['UserDetails']['courier_lat'],
                            "courier_long"=> $val['UserDetails']['courier_long'],
                            "distance"=> $val[0]['distance']
                        )
                    );
                }
            }
            $this->responceData['status'] = 1;
            $this->responceData['message'] = "Success";
            $result = array(
              'responseData' => array(
                 'result' => $this->responceData['result']
              )
            );
            return json_encode($result);

        } else {
            $this->responceData['status'] = 0;
            $this->responceData['message'] = "Something Went Wrong.Please Try again";           
        }              
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM