所有,
我有以下JSON数据.我需要帮助在PHP中编写一个函数,它接受一个categoryid并返回一个数组中属于它的所有URL.
像这样的东西::
<?php
function returnCategoryURLs(catId)
{
//Parse the JSON data here..
return URLArray;
}
?>
{
"jsondata": [
{
"categoryid": [
20
],
"url": "www.google.com"
},
{
"categoryid": [
20
],
"url": "www.yahoo.com"
},
{
"categoryid": [
30
],
"url": "www.cnn.com"
},
{
"categoryid": [
30
],
"url": "www.time.com"
},
{
"categoryid": [
5,
6,
30
],
"url": "www.microsoft.com"
},
{
"categoryid": [
30
],
"url": "www.freshmeat.com"
}
]
}
Run Code Online (Sandbox Code Playgroud)
谢谢
从这个示例数组获取条形码和sku有一点问题
{
"title": "This is the title",
"variants": [{
"barcode": "123456789",
"sku": "sku1"
}]
}
Run Code Online (Sandbox Code Playgroud)
PHP
foreach ($products as $product) {
$products[] = Array(
"title" => $product["title"], // product title
"barcode" => $product["variants"]["barcode"], // product barcode
"sku" => $product["variants"]["sku"] // product SKU
);
}
Run Code Online (Sandbox Code Playgroud) 致命错误:未捕获错误:无法使用 stdClass 类型的对象作为数组
$code = json_decode($src);
echo $code["items"];
Run Code Online (Sandbox Code Playgroud)
var_dump 显示以下内容(截断):
object(stdClass)#7 (3) { ... }
Run Code Online (Sandbox Code Playgroud)
我不知道它是什么stdClass以及如何使用它。
编辑:
stdClass是一个创建简单对象实例的类。类的属性将使用->而不是[...]符号来访问。
根据 的文档json_decode,简单地将第二个参数设置为true将导致结果是关联数组,它可以像数组一样依次访问。
在发布这个问题时,我没有尝试搜索如何解码 JSON - 因为这非常简单而且我已经开始工作了。我刚刚收到另一个错误(上图),并且没有找到如何解决这个问题的运气。我相信人们也有类似的问题,因为这个问题也得到了一些看法。
{
"receiver_uid":[
"58a43a3e3fbbf3.61108490",
"58a43be07a3bc3.90311110",
"58da53ab5ce8d6.84754819"
]
}
Run Code Online (Sandbox Code Playgroud)
这是我想转换为数组的值。我知道这是一个非常简单的问题,但请有人帮助我吗?
在我的代码中,我想打印一个特定的值,即ticket来自JSON数据,但是当我这样做时,Notice: Trying to get property of non-object每当我不知道我做错了什么时我就会出错
这是我的JSON数组转储
array(3) {
["status"]=>
int(200)
["msg"]=>
string(2) "OK"
["result"]=>
array(6) {
["ticket"]=>
string(79) "w-_xdRcVDJQ~55de39b726745a28~1505246565~def~FzuGpPpNoQEus6lK~1~Dhyp8PJM83-pMwAe"
["captcha_url"]=>
string(50) "https://myurl.com/images/FzuGpPpNoQEus6lK.png"
["captcha_w"]=>
int(160)
["captcha_h"]=>
int(70)
["wait_time"]=>
int(0)
["valid_until"]=>
string(19) "2017-09-12 20:17:46"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的PHP代码
$response = file_get_contents($url);
$obj = json_decode($response, TRUE);
$printjson = $obj->result->ticket;
echo $printjson;
Run Code Online (Sandbox Code Playgroud)