我正在尝试遍历json对象并将选定的值推送到数组中。
$json = '
{
"canonicalUrl": "/v1/products(offers.type=deal_of_the_day)?format=json&apiKey=946n9vuhdkgeyz2qx2dpxd54",
"currentPage": 1,
"from": 1,
"partial": false,
"products": [
{
"active": true,
"activeUpdateDate": "2014-11-03T19:43:46",
"lowPriceGuarantee": true,
"name": "LG - 1.1 Cu. Ft. Mid-Size Microwave - Black",
"new": false,
"onSale": true,
"productId": 1219180376135,
"regularPrice": 124.99,
"salePrice": 99.99,
"sku": 5998225,
"source": "bestbuy",
"startDate": "2014-07-06",
"type": "HardGood"
},
{
"active": true,
"activeUpdateDate": "2014-11-03T18:03:02",
"lowPriceGuarantee": false,
"name": "Rocketfish In-Wall HDMI Cable",
"new": false,
"onSale": true,
"productId": 1218343205770,
"regularPrice": 29.99,
"salePrice": 24.99,
"sku": 2634897,
"source": "bestbuy",
"startDate": "2011-08-14",
"type": "HardGood"
}
],
"queryTime": "0.004",
"to": 2,
"total": 2,
"totalPages": 1,
"totalTime": "0.020"
}
';
$json_output = json_decode($json);
$pBB = array("title" => array(), "type" => array());
foreach($json_output->products as $obj){
array_push($pBB['title']," {$obj->name}");
array_push($pBB['type']," {$obj->type}" );
}
echo json_encode($pBB);
上述代码的输出是
{
"title": [
" LG - 1.1 Cu. Ft. Mid-Size Microwave - Black",
" Rocketfish In-Wall HDMI Cable"
],
"type": [
" HardGood",
" HardGood"
]
}
我想按以下方式返回
[
{
"title": "LG - 1.1 Cu. Ft. Mid-Size Microwave - Black",
"type": "HardGood"
},
{
"title": " Rocketfish In-Wall HDMI Cable",
"type": " HardGood"
}
]
有什么想法吗?
答案 0 :(得分:1)
根据my comment on your question,您使用了一种非常规的方法将产品组织为值,您应该将每个产品组织成包含其标题和类型的每个产品的单独数组。请改用:
$json_output = json_decode($json);
$pBB = array();
foreach($json_output->products as $obj){
$pBB[] = array(
'title' => " {$obj->name}", // Not sure why you're using " {$obj->name}" but I preserved it
'type' => " {$obj->type}", // You could just use $obj->type directly
);
}
echo json_encode(array_values($pBB));
答案 1 :(得分:1)
$json_output = json_decode($json);
$pBB = array();
foreach($json_output->products as $obj){
array_push($pBB, array(
'title' => " {$obj->name}",
'type' => " {$obj->type}"
));
}
echo json_encode(array_values($pBB));
答案 2 :(得分:1)
试试这个!你需要创建对象并推入数组。
$json = ' { "from": 1, "to": 2, "total": 2, "currentPage": 1, "totalPages": 1, "queryTime": "0.004", "totalTime": "0.020", "partial": false, "canonicalUrl": "/v1/products(offers.type=deal_of_the_day)?format=json&apiKey=946n9vuhdkgeyz2qx2dpxd54","products": [ { "sku": 5998225, "productId": 1219180376135, "name": "LG - 1.1 Cu. Ft. Mid-Size Microwave - Black", "source": "bestbuy", "type": "HardGood", "startDate": "2014-07-06","new": false, "active": true, "lowPriceGuarantee": true, "activeUpdateDate": "2014-11-03T19:43:46", "regularPrice": 124.99, "salePrice": 99.99, "onSale": true},{ "sku": 2634897, "productId": 1218343205770, "name": "Rocketfish In-Wall HDMI Cable", "source": "bestbuy", "type": "HardGood", "startDate": "2011-08-14", "new": false, "active": true,"lowPriceGuarantee": false, "activeUpdateDate": "2014-11-03T18:03:02", "regularPrice": 29.99, "salePrice": 24.99, "onSale": true } ] }';
$json_output = json_decode($json);
foreach($json_output->products as $obj){
$new_obj = new stdClass();
$new_obj->title = $obj->name;
$new_obj->type = $obj->type;
$pBB[]= $new_obj;
}
echo json_encode($pBB);
答案 3 :(得分:0)
构建JSON对象时出错。实际上你在json_decode函数中传递的字符串是可以的,但是当你从数组构建一个JSON对象时会出现问题。
不要使用
$ pBB = array(" title" => array()," type" => array());
而不是尝试
$ pBB = array(array(" title" => $ title," type" => $ type));
当您将$ pBB编码为Json时,我猜它会为您提供准确的输出。
快乐编码。 阿图尔
答案 4 :(得分:0)
echo json_encode($pBB);
你需要确保它似乎可以成为JS的数组(即顺序编号)
用
替换上面的行echo json_encode(array_values($pBB));
使其按预期工作
数组值返回顺序整数作为键