0

I am creating the following object from the bigCommerce api as follows:

[
{
    "id": 412,
    "option_id": 37,
    "display_name": "testSteveMemory",
    "sort_order": 0,
    "is_required": true
},
{
    "id": 413,
    "option_id": 34,
    "display_name": "Hard Drive (desktop)",
    "sort_order": 1,
    "is_required": true
},
{
    "id": 414,
    "option_id": 24,
    "display_name": "Include Keyboard & Mouse",
    "sort_order": 2,
    "is_required": true
},
{
    "id": 415,
    "option_id": 33,
    "display_name": "Memory",
    "sort_order": 3,
    "is_required": true
}
]

I convert this to a PHP array using :

$curlProductOptions = json_decode($curlProductOptions, true);

I then loop through the array and get the option for that option_id

$allOptions = array();  
foreach($curlProductOptions as $value){
    //echo $value['option_id'].'<br>';
    $option_id = $value['option_id'];


$product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $product_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlProductData = curl_exec($curl);
array_push($allOptions,$curlProductData);
curl_close($curl);
}

I am then using echo to output the array $allOptions and I consume this in a mobile applications as json. if I use

echo $curlProductData.'<br>' 

I get the following:

[{"id":112,"option_id":37,"label":"1 gig ram","sort_order":0,"value":"1 gig ram"},{"id":113,"option_id":37,"label":"2 gig ram","sort_order":1,"value":"2 gig ram"},{"id":114,"option_id":37,"label":"4 gig ram","sort_order":2,"value":"4 gig ram"}]
[{"id":104,"option_id":34,"label":"80GB SATA","sort_order":0,"value":"80GB SATA"}]
[{"id":90,"option_id":24,"label":"Yes","sort_order":0,"value":"Yes"},{"id":91,"option_id":24,"label":"No","sort_order":1,"value":"No"}]
[{"id":102,"option_id":33,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":103,"option_id":33,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]

If I use echo any of the following:

echo $allOptions;
json_encode($allOptions);
var_dump($allOptions);

I get a parse error when the data is returned to the mobile app. ie it is not recognising the result as a json array.

I am presuming:

  1. That $allOptions = array() declares $allOptions as an array

  2. I am populating $allOptions with each $curlProductData in the loop.

What am I doing wrong here ?

I am now using :

echo "[".implode(",\n",$allOptions)."]";

I am now getting the following returned :

[[{"id":112,"option_id":37,"label":"1 gig ram","sort_order":0,"value":"1 gig ram"},{"id":113,"option_id":37,"label":"2 gig ram","sort_order":1,"value":"2 gig ram"},{"id":114,"option_id":37,"label":"4 gig ram","sort_order":2,"value":"4 gig ram"}], [{"id":104,"option_id":34,"label":"80GB SATA","sort_order":0,"value":"80GB SATA"}], [{"id":90,"option_id":24,"label":"Yes","sort_order":0,"value":"Yes"},{"id":91,"option_id":24,"label":"No","sort_order":1,"value":"No"}], [{"id":102,"option_id":33,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":103,"option_id":33,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]]

but I cannot get the mapping correct ?

MrWarby

4
  • What is throwing the parse error, is it the PHP, or the language that is receiving the output from your PHP (such as JavaScript)? Commented Apr 2, 2014 at 17:42
  • The language that is receiving the output from your PHP. Whenever I had done this before (simpler scenarios ) I can map the data to a mobile list. eg var map2 = { label: 'display_name', count: 'option_id', // this is the ID for the option value: 'id' // this is the id of the optionSet }; But now I have to use label: ["0", "label"], aside: ["1", "label"] but this is showing label from each array section and not a list. Am I making sense ??? Commented Apr 2, 2014 at 17:59
  • Kind of, what language receives the echo json_encode($allOptions)? Commented Apr 2, 2014 at 18:01
  • I have got it working thanks. See below. Commented Apr 2, 2014 at 20:55

4 Answers 4

1

The following solution puts each of the items in a single array:

foreach($curlProductOptions as $value){
    //echo $value['option_id'].'<br>';
    $option_id = $value['option_id'];


    $product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $product_url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_ENCODING, "");
    $curlProductData = curl_exec($curl);

    foreach(json_decode($curlProductData, true) as $key => $value) {
        $allOptions[] = $value; // [] is quicker than array_push() for single items
    }

    curl_close($curl);
}

If you need your four groupings as in the question then:

The following solution puts each of the items in a single array:

foreach($curlProductOptions as $value){
    //echo $value['option_id'].'<br>';
    $option_id = $value['option_id'];


    $product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $product_url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_ENCODING, "");
    $curlProductData = curl_exec($curl);

    $allOptions[] = json_decode($curlProductData, true);

    curl_close($curl);
}

Now json_encode($allOptions) will give you a valid json String. The problem was you were trying to pass 4 json strings when only 1 was supposed to be read.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @akshey2598 The first section above gave me the correct result. Not sure why but I'll get my head around it soon....
1

You are outputting several arrays after each other. You can only have one JSON object at once.

Best would be to stick to arrays and use echo json_encode($curlProductData) to output the data.

Comments

0

If you're getting a parser error, it's probably because there's a missing semicolon after echo $curlProductData.'<br>'

Comments

0
$curlProductData = curl_exec($curl);
array_push($allOptions,$curlProductData);

The problem is that curl_exec is returning a string - that string may be a JSON string, but it's still a string.

Your final echo json_encode($allOptions) would be an array of strings, not a multidimensional array as you might be expecting.

What you can try is echo "[".implode(",\n",$allOptions)."]"; as this will result in a multidimensional array.

However keep in mind that this must be done after the loop. If you do it while you're still inside the loop, you will end up with multiple outputs, which causes a problem.

1 Comment

I have used your code and update my question with the results at the bottom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.