PHP curl API 请求有效。使用 explode 制作数组,不提供所需的键值对
PHP curl API request works. Using explode to make array, not giving key value pairs desired
正如标题所说,我收到了 API 的回复。我已经用几种不同的方式清理了响应数据。尝试使用 explode 时,我无法让它将字符串分配为所需的键值对。我将 post 下面的一些数据。如果我需要以不同方式分隔数据以方便使用,我会这样做。在过去的 14 小时内尝试了数十个示例。请帮忙!
# Find the part of the string after the description
$mysearchstring = 'color';
# Minus one keeps the quote mark in front of color
$position = (stripos($response, $mysearchstring)-1);
#This selects the text after the description to the end of the file
$after_descrip = substr($response, $position);
echo "</br>__________Show the data after the description________________</br>";
echo $after_descrip;
echo "</br>__________Show the cleaned data________________</br>";
$cleaned = $after_descrip;
$cleaned1 = str_replace('{', "", $cleaned);
$cleaned2 = str_replace('}', "", $cleaned1);
$cleaned3 = str_replace('[', "", $cleaned2);
$cleaned4 = str_replace(']', "", $cleaned3);
$cleaned5 = str_replace('https://', "", $cleaned4);
$cleaned6 = str_replace('"', "", $cleaned5);
echo $cleaned6;
echo "</br>__________Explode is next________________</br>";
#Turn the string into an array but not the array I want
$last_half = explode(':', $cleaned6);
print_r($last_half);
}
清理后的数据如下所示:
color:#3C3C3D,iconType:vector,iconUrl:cdn.coinranking.com/rk4RKHOuW/eth.svg,websiteUrl:www.ethereum.org,socials:name:ethereum,url: twitter.com/ethereum,type:twitter,name:ethereum,url:www.reddit.com/r/ethereum/
生成的数组如下所示:
Array ( [0] => color [1] => #3C3C3D,iconType [2] => vector,iconUrl [3] => cdn.coinranking.com/rk4RKHOuW/eth.svg,websiteUrl [4] => www.ethereum.org,socials [5] => name [6] => ethereum,url [7] => twitter.com/ethereum,type [8] => twitter ,name [9] => ethereum,url [10] => www.reddit.com/r/ethereum/,type [11] => reddit,name [12] => ethtrader,url [13] =>
颜色应该是第一个键,#3C3C3D 应该是第一个值。其余数据应遵循该格式。
您认为最好的方法是什么?谢谢。
-生锈
您应该使用 json_decode
函数。 https://www.php.net/manual/en/function.json-decode.php
这个函数可以将json解码为数组和对象
所以你想要一个数组,其中“颜色”->“#3C3C3D”?
就是这样:
$last_half = explode(',', $last_half);
$new_array = [];
foreach ($last_half as $item) {
[$key, $value] = explode(':', $item, 2);
$new_array[$key] = $value;
}
现在新数组应该像你想要的那样:)
但是等等,刚注意到最初你有json,所以你应该始终使用本机函数并使用它来解析这个字符串。 (正如其他答案所说)
最好的,
intxcc
再走一步...
$last_half = explode(',', $cleaned6);
foreach($last_half as $item){
list($k, $v) = explode(':', $item);
$result[$k] = $v;
}
print_r($result);
正如标题所说,我收到了 API 的回复。我已经用几种不同的方式清理了响应数据。尝试使用 explode 时,我无法让它将字符串分配为所需的键值对。我将 post 下面的一些数据。如果我需要以不同方式分隔数据以方便使用,我会这样做。在过去的 14 小时内尝试了数十个示例。请帮忙!
# Find the part of the string after the description
$mysearchstring = 'color';
# Minus one keeps the quote mark in front of color
$position = (stripos($response, $mysearchstring)-1);
#This selects the text after the description to the end of the file
$after_descrip = substr($response, $position);
echo "</br>__________Show the data after the description________________</br>";
echo $after_descrip;
echo "</br>__________Show the cleaned data________________</br>";
$cleaned = $after_descrip;
$cleaned1 = str_replace('{', "", $cleaned);
$cleaned2 = str_replace('}', "", $cleaned1);
$cleaned3 = str_replace('[', "", $cleaned2);
$cleaned4 = str_replace(']', "", $cleaned3);
$cleaned5 = str_replace('https://', "", $cleaned4);
$cleaned6 = str_replace('"', "", $cleaned5);
echo $cleaned6;
echo "</br>__________Explode is next________________</br>";
#Turn the string into an array but not the array I want
$last_half = explode(':', $cleaned6);
print_r($last_half);
}
清理后的数据如下所示:
color:#3C3C3D,iconType:vector,iconUrl:cdn.coinranking.com/rk4RKHOuW/eth.svg,websiteUrl:www.ethereum.org,socials:name:ethereum,url: twitter.com/ethereum,type:twitter,name:ethereum,url:www.reddit.com/r/ethereum/
生成的数组如下所示:
Array ( [0] => color [1] => #3C3C3D,iconType [2] => vector,iconUrl [3] => cdn.coinranking.com/rk4RKHOuW/eth.svg,websiteUrl [4] => www.ethereum.org,socials [5] => name [6] => ethereum,url [7] => twitter.com/ethereum,type [8] => twitter ,name [9] => ethereum,url [10] => www.reddit.com/r/ethereum/,type [11] => reddit,name [12] => ethtrader,url [13] =>
颜色应该是第一个键,#3C3C3D 应该是第一个值。其余数据应遵循该格式。
您认为最好的方法是什么?谢谢。
-生锈
您应该使用 json_decode
函数。 https://www.php.net/manual/en/function.json-decode.php
这个函数可以将json解码为数组和对象
所以你想要一个数组,其中“颜色”->“#3C3C3D”?
就是这样:
$last_half = explode(',', $last_half);
$new_array = [];
foreach ($last_half as $item) {
[$key, $value] = explode(':', $item, 2);
$new_array[$key] = $value;
}
现在新数组应该像你想要的那样:)
但是等等,刚注意到最初你有json,所以你应该始终使用本机函数并使用它来解析这个字符串。 (正如其他答案所说)
最好的, intxcc
再走一步...
$last_half = explode(',', $cleaned6);
foreach($last_half as $item){
list($k, $v) = explode(':', $item);
$result[$k] = $v;
}
print_r($result);