dongzheng4556 2017-11-16 16:05
浏览 16
已采纳

PHP:将数组与另一个从curl中取出的数组相关联

I have the following code that fetches arrays from a cURL.

$codici_hotel_arr = array();                  
foreach($data_destin['results'] as $key=>$val) {        
    $codici_hotel_arr[] = '"' . $val['hotel_code'] . '"'; 
    $codici_price_arr[] = '"' . $val['products'][0]['price'] . '"';  
}
$codici_definitivi = implode(', ', $codici_hotel_arr);
$codici_prezzi = implode(', ', $codici_price_arr);

The code returns these values:

**$codici_definitivi:**
"1074d0", "19f726", "1072ba", "183444", "1071bf", "112438", "1b326e", "15d8ab", "19d885", "193e61", "10aab2", "107155", "110669", "189b95", "16d78f", "18dd7d"

**$codici_prezzi**
"160.16", "234.32", "256.88", "325.42", "342.22", "353.30", "365.78", "372.84", "395.72", "478.75", "503.36", "543.39", "584.61", "584.61", "597.70", "601.63".

I would need to get a $codici_prezzi for each $codici_definitivi. As a response of a string cURL, both codes are as follows:

1074d0 -> 160.16;

19f726 -> 234.32;

1072ba -> 256.88;

etc...

It's possible?

Thank you

  • 写回答

3条回答 默认 最新

  • dpa55065 2017-11-16 16:26
    关注

    If I don't misunderstood your requirement then this should work. Remove extra imploding and try to array_combine() the two corresponding arrays.

      // Your initial code, after removing imploding
      $codici_hotel_arr = $codici_price_arr = [];                  
      foreach($data_destin['results'] as $key=>$val) {        
           $codici_hotel_arr[] = '"' . $val['hotel_code'] . '"'; 
           $codici_price_arr[] = '"' . $val['products'][0]['price'] . '"'; 
      }
    
     // I assume your array structure will be after looping curl response
     $codici_hotel_arr = ["1074d0", "19f726", "1072ba", "183444", "1071bf", "112438", "1b326e", "15d8ab", "19d885", "193e61", "10aab2", "107155", "110669", "189b95", "16d78f", "18dd7d"];
     $codici_price_arr = ["160.16", "234.32", "256.88", "325.42", "342.22", "353.30", "365.78", "372.84", "395.72", "478.75", "503.36", "543.39", "584.61", "584.61", "597.70", "601.63"];
    
     $result = array_combine($codici_hotel_arr,$codici_price_arr);
     print '<pre>';
     print_r($result);
     print '</pre>';
    

    Result:

    Array (
        [1074d0] => 160.16
        [19f726] => 234.32
        [1072ba] => 256.88
        [183444] => 325.42
        [1071bf] => 342.22
        [112438] => 353.30
        [1b326e] => 365.78
        [15d8ab] => 372.84
        [19d885] => 395.72
        [193e61] => 478.75
        [10aab2] => 503.36
        [107155] => 543.39
        [110669] => 584.61
        [189b95] => 584.61
        [16d78f] => 597.70
        [18dd7d] => 601.63 
    )
    

    Edit: Because I don't understand your expected result that's why I post it also.If you want it on string format then try like this,

    function print_string($v, $k) {
       echo "$k->$v
    ";
    }
    
    array_walk($result, "print_string");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作