1
我想循環一個JSON對象,並使用鍵將選定的值推入數組。array_push()期望參數1是陣列與array_push使用鍵
$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);
$pBB = array();
foreach($json_output->products as $obj){
array_push($pBB['title']," {$obj->name}");
array_push($pBB['type']," {$obj->type}");
//array_push($pBB," {$obj->name}"); without key works fine
}
echo json_encode($pBB);
以下是我收到
<br />
<b>Warning</b>: array_push() expects parameter 1 to be array, null given on line <b>6</b><br />
<br />
<b>Warning</b>: array_push() expects parameter 1 to be array, null given on line <b>6</b><br />
{"title":null}
如果我推的值,而它的工作原理按鍵錯誤,我得到下面的輸出
[" LG - 1.1 Cu. Ft. Mid-Size Microwave - Black"," Rocketfish In-Wall HDMI Cable"]
任何想法,在此先感謝!
非常感謝,它的工作:-) – Nish 2014-11-06 16:57:09
如果它的工作,然後接受他的答案; ) – 2014-11-06 16:58:39
我得等7更多分鐘接受它作爲答案,堅持:) – Nish 2014-11-06 17:00:17