1
$val_array = json_decode($json_data, true);

I decoded a JSON array & stored it into a variable. JSON has data from server side.

When I var dump this array var_dump($val_array); I get:

{
    "groups": {
        "connect": "yes",
        "Name": "admin",
        "id": "1",
        "Groups": "1",
        "manage": "1",
        "Users": "1",
        "show": "1",
        "log": "1"
    }
}

And I check size of array.

$x=count($val_array); gives 1 as output
$x=count($val_array['groups']); gives 8 as output

And when I have more data in my database, JSON looks like this

{
    "groups": [{
        "connect": "yes",
        "Name": "admin",
        "id": "1",
        "Groups": "1",
        "manage": "1",
        "Users": "1",
        "show": "1",
        "log": "1"
    },
              {
        "connect": "no",
        "Name": "noadmin",
        "id": "2",
        "Groups": "2",
        "manage": "2",
        "Users": "2",
        "show": "2",
        "log": "2"
              }

] }

I want to show the values to an html table. I use a simple for loop.

$val_array = $val_array['groups'];

for($i=0; $i<$x; $i++)
{

$a= $val_array['connect'];
$b= $val_array['Name'];
$c= $val_array['id'];
$d= $val_array['Groups'];
$e= $val_array['manage'];
$f= $val_array['Users'];
$g= $val_array['show'];
$h= $val_array['log'];
}

What is the value of $x that I should use?

When I use $x=count($val_array); and if JSON has more than 1 data, error comes up. Undefined offset or something, if data is only 1, then it works.

When I use this $x=count($val_array['groups']); and if JSON data has 1 data, size is returned as 8.

When I used

$x=count($val_array['groups']);

and

var_dump($val_array);

I got: Notice: Undefined offset: 0 in my page

Size as 8.

array (size=1)
  'groups' => 
    array (size=8)
      'connect' => string '1' (length=1)
      'Name' => string 'admin' (length=10)
      'id' => string '1' (length=1)
      'Groups' => string '1' (length=1)
      'manage' => string '1' (length=1)
      'Users' => string '1' (length=1)
      'show' => string '1' (length=1)
      'log' => string '1' (length=1)

as var_dump output.

7
  • your JSON in the second case is malformed. You need a comma after the closing } after groups Commented May 29, 2014 at 6:36
  • Can you show us var_dump($val_array); for second json string just after decoding it. Commented May 29, 2014 at 6:36
  • Use foreach instead of for. Commented May 29, 2014 at 6:37
  • Since when does var_dump() display associative arrays like that? It always puts the type of the data first, and shows arrays with Array(size) at the beginning, and => between the keys and values, not :. That looks more like the original JSON than the output of var_dump. Commented May 29, 2014 at 6:43
  • 1
    Even with the comma added, it's still not valid JSON. { } surrounds objects, every element has to be in the form key: value. You have no key for the second value. Are you sure it isn't groups: [ { ... }, { ...} ]? Commented May 29, 2014 at 6:46

3 Answers 3

4

If groups is supposed to be an array of group opbjects, the JSON should be:

{
   "groups":[
      {
         "connect":"yes",
         "Name":"admin",
         "id":"1",
         "Groups":"1",
         "manage":"1",
         "Users":"1",
         "show":"1",
         "log":"1"
      },
      {
         "connect":"no",
         "Name":"noadmin",
         "id":"2",
         "Groups":"2",
         "manage":"2",
         "Users":"2",
         "show":"2",
         "log":"2"
      }
   ]
}

You need [ ] around the array. With this JSON, count($val_array['groups']) should return 2, not 8. See this demo.

If the web service sometimes returns the groups as an array, and other times as a single element, you can do this:

if (!isset($val_array['groups'][0])) {
    $val_array['groups'] = array($val_array['groups']);
}

This checks whether groups is an array. If not, it makes an array containing the single element. Then you can process it consistently as an array in the rest of your code.

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

8 Comments

All I need is to know about the size. My JSON is corrected (I corrected it). @Barmar
This works fine. But thing is, I am getting this JSON when I make a GET request to a web service, so when there is only one element, there are no []. So I ain't able to change that manually. @Barmar
Don't use the true option to json_decode. Then you can test whether the type of of $val_array->groups is an array or an object.
If it's an array, you need to iterate over it, if it's an object you can access it directly.
Fatal error: Cannot use object of type stdClass as array @ Barmar when I didn't use true option to json_decode.
|
1

As @barmar pointed out, your JSON Data is malformed. If you have already the correct JSON format, you could just use it and loop it thru foreach to output it to a table. Consider this example:

<?php

$raw = '{ "groups":[ { "connect":"yes", "Name":"admin", "id":"1", "Groups":"1", "manage":"1", "Users":"1", "show":"1", "log":"1" }, { "connect":"no", "Name":"noadmin", "id":"2", "Groups":"2", "manage":"2", "Users":"2", "show":"2", "log":"2" } ]}';
$data = json_decode($raw, true);
$data = reset($data); // flatten
$headers = array_keys(reset($data)); // get the header and use it as your headings/headers

?>

<!-- simple foreach loop -->
<table border="1" cellpadding="10">
<thead>
    <tr>
    <!-- loop the headers -->
    <?php foreach($headers as $value): ?>
        <td><?php echo $value; ?></td>
    <?php endforeach; ?>
    </tr>
</thead>
<tbody>
<?php foreach($data as $value): ?>
    <tr>
        <!-- then loop the values -->
        <?php foreach($value as $element):?>
            <td><?php echo $element; ?></td>
        <?php endforeach; ?>
    </tr>
<?php endforeach; ?>
</tbody>
</table>

Sample Output

Comments

-1

I found the issue. change $val_array['Log'] to $val_array['log'].

$json_data = '{
   "groups": [{
   "connect": "yes",
   "Name": "admin",
   "id": "1",
   "Groups": "1",
   "manage": "1",
   "Users": "1",
   "show": "1",
   "log": "1"
},
 {
   "connect": "no",
   "Name": "noadmin",
   "id": "2",
   "Groups": "2",
   "manage": "2",
   "Users": "2",
   "show": "2",
   "log": "2"
}] }';

$val_array = json_decode($json_data, true);

foreach($val_array["groups"] as $key=>$value){

$a= $value['connect'];
$b= $value['Name'];
$c= $value['id'];
$d= $value['Groups'];
$e= $value['manage'];
$f= $value['Users'];
$g= $value['show'];
$h= $value['log'];
echo $h;
}

Comments

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.