1

enter image description here

I want to create a json object using php like below as I need to create a stacked bar chart using d3.js, hence I am using data from oracle table and need the json data as above image.
My table has these 3 columns. (ID, NAME, Layers) like:-

ID, NAME, Layers
1   ABC   a:8,b:10
2   WWW   c:8,d:7

When I am fetching data from oracle, my php code is:-

while (($row = oci_fetch_array($stid, OCI_ASSOC))) {
          $streamArr[] = $row;
}    
header("Access-Control-Allow-Origin: *");
echo json_encode($streamArr);

I am getting this error:-

Warning: [json] (php_json_encode) type is unsupported, encoded as null in line 48
json output as:-
[["1","ABC",{"descriptor":null}],["2","WWW",{"descriptor":null}]]
can you please help me in rectifying with this issue?
2
  • Could you please provide this data not as a picture. Commented Dec 13, 2017 at 8:33
  • 1
    Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. Don't post images of code or error messages. Read why Instead copy and paste or type the actual data into the post directly. Commented Dec 13, 2017 at 8:37

2 Answers 2

1

The error message refers to a field you didn't mention in the example. Maybe just don't select unneeded fields?

If you fix that, you still won't get the desired JSON structure. To get layers as an object in every element of data in the resulting JSON, you need to convert the string values stored in the database to PHP arrays before encoding.

You could do that with similar code:

while (($row = oci_fetch_array($stid, OCI_ASSOC))) 
{
  $row['layers'] = array_map(function($el) {
    return explode(':', $el);
  }, explode(',', $row['layers']));

  $streamArr[] = $row;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice solution! Bravo!
1

@marekful answer is really nice, not easiest to understand but efficient and short!

Here you have a longer and not so efficient, but maybe easier to get.

$streamArr = array();
while (($row = oci_fetch_array($stid, OCI_ASSOC))) 
{
    // Get ID and NAME
    $data = array(
        'id' => $row['ID'],
        'name' => $row['NAME'],
        'layers' => array()
    );
    // Get Layers by split by comma
    $layers = explode(',', $row['layers']);
    foreach ($layers as $layer) {
        // Get layer parts by slit with double-point
        $layersPart = explode(':', $layer);
        foreach ($layersPart as $key => $value) {
            $data['layers'][$key] = $value;
        }
    }
    // Add to global array
    $streamArr[] = $data;
}
echo json_encode($streamArr);

1 Comment

Thanks Camille, your solution is indeed more readable but quite a bit longer. You will want to be short when you work a lot with code, plus it becomes easier to read, too. explode does its own "foreach" internally though, but it uses a tokeniser on the string and iterates over the tokens to create an array. These kind of PHP functions are handy to hide complexity.

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.