I have a project where I have an encoded json array and need to format it so the elements inside the array print underneath each other like so in the index.php file.
this is my desired output for the elements inside the array, to be printed out underneath each other with a space for each new index
here is my code so far:
allNames.php
<?php
// Get the string from the URL
$json = file_get_contents('http://5dd559d3ce4c300014402cb8.mockapi.io/onboard/posts');
printAll($json);
$array= array();
function printAll($json){
// Decode the JSON string into an object
$obj = json_decode($json,true);
$elementCount = count($obj);
// In the case of this input, do key and array lookups to get the values
for($i=0; $i<$elementCount; $i++){
#delimit based on added * after each attribute to ensure all elemets are added and not cut short
$array[]=explode("*",$obj[$i]["id"]."*".$obj[$i]["createdAt"]."*".$obj[$i]["name"]
."*".$obj[$i]["avatar"]."*".$obj[$i]["jobDescription"]."*".$obj[$i]["userEmail"]."*"
.$obj[$i]["userIpAddress"]."*".$obj[$i]["userAgent"]);
}
echo json_encode($array);
}
?>
index.php
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
function ajax_post(){
$.post("allNames.php", {
}, function(data) {
var returnedData = JSON.parse(data);
$("#data").html(returnedData + "<br>");
});
};
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
<input name="myBtn" type="submit" value="Check" onclick="ajax_post()"> <br><br>
<center> <p id="data" style="color:black"></p><br><br> </center>
</body>
</html>
';` at the end of your `for` loop and that will add a blank row in the browser where you're echoing your results. You can echo variables inside an actual HTML page if you'd like to format it any way you want. Can you be more specific in your question please? – Paulo Hgo Nov 27 '19 at 19:09
'` for example? Repeat that for each element in the array? – Paulo Hgo Nov 27 '19 at 19:12