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