Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to loop a json object and push selected values into array using keys.

$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);

and below is the error i am getting

<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}

If i push the values without keys it works and i get below output

[" LG - 1.1 Cu. Ft. Mid-Size Microwave - Black"," Rocketfish In-Wall HDMI Cable"]

Any thoughts , Thanks in advance!

share|improve this question

1 Answer 1

up vote 5 down vote accepted

$pBB['title'] is null because you haven't defined it yet.

Change

$pBB = array();

to

$pBB = array("title" => array(), "type" => array());

Update:

$pBB = array();
foreach($json_output->products as $obj){
    $pBB[] = array(
        "title" => $obj->name,
        "type" => $obj->type
    );
}
echo json_encode($pBB);
share|improve this answer
    
Thanks a lot , it worked :-) –  Nish Nov 6 '14 at 16:57
1  
if it worked, then accepts his answer ;) –  Ares Draguna Nov 6 '14 at 16:58
    
I have to wait 7 more mins to accept it as answer, hold tight :) –  Nish Nov 6 '14 at 17:00
    
@Halcyon followup question what changes should i make to code so it returns array as below format [{"type":"HardGood","title":"title1"},{"type":"HardGood","title":"title2"}] right out its returning like {"title":[" LG - 1.1 Cu. Ft. Mid-Size Microwave - Black"," Rocketfish In-Wall HDMI Cable"],"type":[" HardGood"," HardGood"]} –  Nish Nov 6 '14 at 18:51
1  
@Nish see the update. –  Halcyon Nov 6 '14 at 23:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.