PHP curl API请求工作正常。使用explode生成数组,不提供所需的键值对

PHP curl API请求工作正常。使用explode生成数组,不提供所需的键值对,php,arrays,key,explode,Php,Arrays,Key,Explode,正如标题所说,我得到的API响应很好。我已经用几种不同的方法清理了响应数据。当尝试使用explode时,我无法让它将字符串指定为所需的键和值对。我将在下面发布一些数据。如果为了方便起见需要对数据进行不同的划分,我会这样做。在过去的十四个小时里尝试了几十个例子。请帮忙 # Find the part of the string after the description $mysearchstring = 'color'; # Minus one keeps the quote mark in

正如标题所说,我得到的API响应很好。我已经用几种不同的方法清理了响应数据。当尝试使用explode时,我无法让它将字符串指定为所需的键和值对。我将在下面发布一些数据。如果为了方便起见需要对数据进行不同的划分,我会这样做。在过去的十四个小时里尝试了几十个例子。请帮忙

# 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);
#查找描述后面的字符串部分
$mysearchstring='color';
#减1将引号保留在颜色前面
$position=(stripos($response,$mysearchstring)-1);
#这将选择文件末尾描述后面的文本
$after_descripp=substr($response,$position);
echo“
显示描述后的数据”
; 描述后回显$; echo“
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
”; $cleaned=$after_descripp; $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$cleaned 6; echo“
下一步是爆炸
”; #将字符串转换为数组,但不是我想要的数组 $last_half=爆炸(“:”,$cleaned6); 打印(最后一半);
}

清理后的数据如下所示:

颜色:3C3C3D,iconType:vector,iconUrl:cdn.coinranking.com/rk4RKHOuW/eth.svg,网址:www.ethereum.org,社交网站:名称:以太坊,网址:twitter.com/ethereum,类型:twitter,名称:以太坊,网址:

生成的数组如下所示:

数组([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]=>11]=>reddit,name[12]=>ethrader,url[13]>

颜色应该是第一个键,#3c3d应该是第一个值。其余数据应该遵循该格式

你认为哪条路最好?谢谢


-Rusty

您应该使用
json\u decode
函数


此函数可以将json解码为数组和对象

,因此您需要一个“color”->“#3c3d”的数组

就是这样:

$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=爆炸(',',$cleaned6);
foreach(最后一半为$item){
列表($k,$v)=分解(“:”,$item);
$result[$k]=$v;
}
打印(结果);

嘿,亚历克斯,非常感谢!我甚至尝试过使用该示例,但没有正确使用。这非常好用。你太棒了!