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
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");
Supposing that the indices of the arrays are aligned, I would do this:
<?php
for($i = 0; $i < count($codici_definitivi); $i++) {
echo $codici_definitivi[0] . ' -> ' . $codici_prezzi[0];
// or set up any other mapping you need, e.g. key => value in another array
}
?>
Edit: you should place this mapping before the implode
statements, otherwise you overwrite the original arrays with strings.
$new_codici_prezzi = array();
if(count($codici_definitivi) === count($codici_prezzi)){
for($i = 0; $i < count($codici_definitivi); $i++){
$new_codici_prezzi[$codici_definitivi[$i]] = $codici_prezzi[$i];
}
}
//After that, $new_codici_prezzi is your new array.