【问题标题】:how to merge two arrays values in one array php如何将两个数组值合并到一个数组php中
【发布时间】:2016-10-11 05:01:30
【问题描述】:

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

我的输出低于...

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

【问题讨论】:

  • 您的代码看起来不像生成该数组。您必须在searchCourier()-方法之后的其他地方处理结果,对吧?
  • 顺便说一句,if (!$err) 的意义何在?您在其正上方的行中设置$err = false;。它将始终包含错误...
  • 所以$this-&gt;UserDetail-&gt;query() 不是您简单地返回结果的数据库调用,而是为您提供上述数组的服务?例如,只是想弄清楚"user_details": { ... 的添加位置。
  • 这仍然没有回答我之前评论中的问题。顺便说一句,这是您在方法中拥有的所有代码,还是这只是一个 sn-p,甚至是为了我们的利益而重写?看起来您的数据仍在其他地方进行处理和结构化。
  • 不..现在我放了完整的代码......那里......流程是我从休息客户端172.10.1.3:8056/rajnikantb/parcelapp/api/Orders/…点击这个url并提供lat和long like {"lat":正文中的“21.0371687”,“long”:“79.01362560000007”}...这将为我提供上述输出...

标签: php arrays cakephp


【解决方案1】:

试试这个代码。希望对你有帮助

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

【讨论】:

  • 我刚刚编辑了我的答案,哦,你的意思是你想要它为 json 格式?。
  • 不..我的意思是你的代码也在不同的数组中得到距离......不在同一个数组中......正如我在我的预期结果中所示......我正在输出在我从你的代码中得到的问题中......
  • 我的意思是你试过这个?? $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]['距离'] ) );
  • 结果如何?
  • $data = $this->UserDetail->query("SELECT id,courier_address,courier_lat,courier_long,( 3959 * acos( cos( 弧度( '$lat' ) ) * cos( 弧度( courier_lat ) ) * cos( 弧度( courier_long ) - 弧度( '$long' ) ) + sin( 弧度( '$lat' ) ) * sin( 弧度( courier_lat ) ) ) ) 作为 user_details 的距离 HAVING distance
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
相关资源
最近更新 更多