-2

I can successfully output data from a JSON array to a web page, but I'm trying to reverse the output.

I've tried altering the foreach string to out $result['calls']

<?php
$getfile = file_get_contents('call.json');
$jsonfile = json_decode($getfile);
$result['calls'] = array_reverse($result['calls']);
?>
<a href="call_add.php">Nieuwe call toevoegen</a><br><br>
<table align="center">
<tbody>
    <?php foreach ($jsonfile->calls as $index => $obj): ?>
 ...

I've got something to output, but without the actual 'calls' data OR the PHP page just stays blank. (no results I guess?)

My JSON data looks like this:

{  
   "calls":[  
      {  
         "wie":"Person",
         "waar":"Place",
         "computer":"nvt",
         "impact":"1 gebruiker",
         "vraag":"Kan geen A3 afdrukken",
         "notities":"Ligt aan de printer",
         "status":"Informatie gegeven"
      }, etc

As a newbie I'd expect something like this to work also, but I can't get it to work either.

$getfile = file_get_contents('call.json');
$arr = json_decode($getfile);
$arrr = array_reverse($arr);
foreach($arrr->calls as $item);

The solution is in this code: reverse a json object in foreach loop php in the html

But I still can't get it to work. I'm also unclear of the function of the : (semi-colon) at the end of the line calls as $index => $obj): ?>

I'm expecting a ; here?

** Update 2-8-2019 ** I've now updated the code. Seems fine, except no results are show on page.

<?php
$json = file_get_contents('call.json');
$json = array_reverse(json_decode($json, true));


?>
<a href="call_add.php">Nieuwe call toevoegen</a>&nbsp;&nbsp;<a 
href="call_e.php">Zoek</a><br><br>
<table align="center">
    <tbody> 
        <?php 
foreach ($json as $obj): { 
echo "<p>" . $obj['wie'] . "</p>";
echo "<p>" . $obj['waar'] . "</p>";
}
?>
<tr>
<? endforeach; ?>
Arne
  • 7
  • 1
  • 2
    Where does `$result['calls']` come from? – Nigel Ren Aug 01 '19 at 08:44
  • No, a _completely_ blank page is rather likely to mean that you introduced a syntax error (otherwise, you should at least get to see the static parts, such as the link, which gets output in any case whether there’s data to loop over or not) - so go configure your PHP error reporting correctly first of all. https://stackoverflow.com/questions/1475297/phps-white-screen-of-death – misorude Aug 01 '19 at 09:13
  • Hi Nigel, I'm not using that variable yet, put you have to use the array_reverse on a variable. So I'm using it on the decoded JSON. My JSON data is named: calls – Arne Aug 01 '19 at 11:14

1 Answers1

0

$jsonfile->calls is an array of object, so if you do:

foreach($jsonfile->calls as $key => $value) {
    // ...
}

$key will be the numeric index of the array (0, 1, 2 ...) and $value will be a \stdClass with the data you are trying to access. I think what you want to achieve is this:

foreach($jsonfile->calls as $object) {
    foreach($object as $key => $value) {
        // $key: 'wie'
        // $value: 'Person'
        // and so on...
    }
}

Does this answer your question?

Aranarth
  • 68
  • 6
  • I understand this code and learnt the numeric index of the array. I'm just trying to reverse that numeric index and output in reverse order, So my data is now represented: 1 2 3 but I want 3 2 1 based on their numeric values. p.s. my base code is based on: https://stackoverflow.com/questions/38301457/php-crud-json-file-instead-of-a-database-like-mysql for reference. I just want to reverse the output. – Arne Aug 02 '19 at 06:52
  • @Arne on a second look you are looping `$jsonFile`, while you actually reversed `$result['calls']`. Check it out – Aranarth Aug 02 '19 at 07:13