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>
