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

时间:2016-10-11 05:01:31

标签: php arrays cakephp

我想将两个数组的值合并到一个数组中。

我的输出低于输出......

{
  "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"
        }
      }
    ]
  }
}

在上面的数据中,距离正在另一个数组中。 但我想合并距离的值是user_details数组...

预期输出

{
  "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"
        }
      }
    ]
  }
}

以下是我的代码

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

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"
    }
  }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码。希望它有所帮助

TaskSerilizer