How to echo an array in PHP - php

How can I in an API echo results in a new array?
example:
echo implode('[]',$resultArray);
result:
$resultArray
requested result:
[$resultArray]

Since you're implementing an API the best pracrice will be to output your results in a format that's readable and understandable by multiple programming languages. JSON is the way to go. Read about it here and here
To echo an array, or anything not just an array, in PHP in a JSON format use:
echo json_encode($data);
where $data holds the output

echo json_encode($resultArray);
Assuming you want to keep the data in the response an array to the requester.
If not, your example will work but I'd suggest something easy to parse on the requester's side like a CSV value using commas: echo implode(',', $resultArray);
if you're looking to output the string '[$resultArray]', it'll be something like echo '[$'.print_var_name($resultArray).']'. Not sure why you'd need it like this, but your question is a little bit vague/confusing on exactly what you're looking for.

You could use:
echo "<pre>"; var_dump($array); echo "</pre>";
This way you will get a nice preformatted result of var_dump.

try this
$array = json_decode(json_encode($resultArray), true);
or
Print_r();

Related

Can not echo data from MYSQL database

Hello! The data in table is like that, and I cannot echo these entries individually. I want to show this data in HTML table like for example, Subject: Math, Correct: 34, Wrong: 6.
I am new in php, please help me with this case.
The data in the table seems to be in JSON format. You can convert it to a PHP object using json_decode() then print that object using print_r().
When put all together, you get
echo print_r(json_decode($data), true);
Note, I have to edit this response back to what it is now because #fenil-shah had changed it some something else that was not suitable. Please send your own response instead of making others say what they didn't intend to.
#fenil-shah, my answer was written the way it was on purpose. Here are the explanations:
In general, if you want to stay in control of the result of the print_r() function, you need to return its result as a string. That's why I have passed true as second parameter for the print_r() function. You wouldn't using print_r() without returning its result as a string in a middle of another script because it will output some text in an uncontrolled manner that can break an add. Once you have the return string, you can send it to a log file or do whatever you want with it.
json_decode works beautifully without passing it a true as second parameter. The OP's example is showing a object. You edit was purposely changing the decoded object to an array with associative array. Why?
You have removed the space after the comma which you had moved in from the print_r() to the json_decode() to make it look like json_decode($data,true). You need to know that the space was there for two reasons: Readability and Compliance with the PSR-2 standard (read more about that PHP coding standard at https://www.php-fig.org/psr/psr-2/#46-method-and-function-calls).
There are echo "",print_r() and print() function available in php to print data.If query is returning array then use print_r() function but if query is not returning anything or invalid fetch method in php then data is not getting display by any of these functions.
I think this is what you want as a output.
You can decode the JSON received from database using json_decode() function and get your result transformed in associative array.
Later on passing the array in foreach() loop will do your work accordingly
<?php
$result = '{
"30": {
"subject_id":343,
"correct_answers":34,
"wrong _answers":61,
"not_answered":0,
"time_spent":3801,
"time_to_spend":3680,
"time_spent_correct_ answers":3286,
"time_spent_wrong_answers":515
},
"52": {
"subject_id":52,
"correct_answers":7,
"wrong_answers":3,
"not_answered":0,
"time_spent":883 ,
"time_to_spend":94343,
"time_spent_correct_ans wers":352,
"time_spent wrong_answers":441
},
"53": {
"subject_id":53,
"correct_answers":3,
"wrong_answers":7,
"not_answered":43,
"time_spent":584 ,
"time_to_spend":900,
"time_spent_correct_ans wers":154,
"time_spent wrong_answers":430
}
}';
$json_decoded_data = json_decode($result,true);
?>
<table>
<tbody>
<tr>
<th>subject_id</th>
<th>correct_answers</th>
<th>wrong_answers</th>
</tr>
<?php
foreach($json_decoded_data as $row){
echo "<tr>";
echo "<td>".$row['subject_id']."</td>";
echo "<td>".$row['correct_answers']."</td>";
echo "<td>".$row['correct_answers']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>

How to echo the post value received in post

I'm using image uploader plugin which is posting data in multi dimension array like shown below
Array
(
[file] => {"input":{"name":"KE6Cc2ea2b584.jpg",
"type":"image/jpeg",
"size":61224,
"width":800,
"height":643},
"output":{"width":320,"height":180,
"image"
:"data:image/jpeg;base64,/9j/just an example of base 64 encoded image"},
"actions":{"crop":{"x":0,"y":96.5,"height":450,"width":800 },
"size":{"width":320,"height":320}}}
)
Now i tried lot to echo these post values like shown below
echo $_POST['file']; worked but brought all the data
echo $_POST['file']['output']; didn't worked
echo $_POST['file']['output']['image']; didn't worked
How can i get the value of these post
When trying to echo out any sort of values while debugging, it's a good idea to see exactly what you're getting via var_dump. For example, instead of your echo lines, I'd simply put:
echo "<pre>";
var_dump($_POST['file']);
Check your output there and it should show you exactly what keys you have to work with.
Also, depending on how your plugin is sending the data, you may not get any actual keys. According to your comment about $_POST['file'] printing all the output, it seems as though it's still a string. Try putting the JSON into an object, like so:
$test = json_decode($_POST['file']);
Or, if you'd prefer an associative array as mentioned:
$test = json_decode($_POST['file'], true);
Then get the output of $test:
echo "<pre>";
var_dump($test);

How to print Array READABLE [duplicate]

This question already has answers here:
Is there a pretty print for PHP?
(31 answers)
Closed 7 months ago.
How do i print an Array readable? print_r($array); and var_dump($array); both produce some very ugly clutter of that $array. Ok it is what it says, it prints that array, but i'd like to have some well formated print of that array, how do i do that?
echo '<pre>';
var_dump($array);
echo '</pre>';
Or to a better yet performance, use xDebug with
html_colors = on.
html_colors can be found on php.ini file.
xdebug is from http://xdebug.org/download.php
With xDebug and Colors you don't need to use
echo '<pre>';
echo '</pre>';
it's beautiful as-is.
Actually, it is well-formatted but HTML ignores line breaks and double spaces.
You just have to view the page's source code (CTRL+U or right-click > view source code)
You can wrap your var_dump() output in <pre> to preserve the monospacing:
echo "<pre>" . var_dump($array) . "</pre>";
echo "<pre>".print_r($array, true)."</pre>";
try this code
$myArray=array("a","b","c");
foreach($myArray as $A)
{
echo $A."<br/>"; // you can use any style here like table, div span etc...
}
I like:
highlight_string(var_export($array, true));
var_export() is usable (copy/paste) PHP code as well.
if you want to see it well formatted, you can encode it to json object and print it pretty :)
You have to do is:
echo json_encode($array,JSON_PRETTY_PRINT); //This needs PHP 5.3 at least.

Sorting out raw data from an API link in PHP that is in JSON format

Hopefully this will be quite an easy one for you here.
Anyway, I have been having alot of trouble with fread() so have decided to use an api link that I have been able to locate.
The script is simply to convert extracted game server data into a readable format.
The link for the "Serialized" data is : http://api.typefish.co.uk/monitor/sa-mp/80.86.81.14:6969/?info&masterlist&rules&players
Simply put I have very little in the way of ideas on what I can do with this data.
So far this is what I have although I am probably miles off:
$content = file_get_contents("http://api.typefish.co.uk/monitor/sa-mp/80.86.81.14:6969/?info&masterlist&rules&players");
trim($content, "{");
trim($content, "}");
foreach(exlpode(",", $content) as $pieces){
explode(":", $pieces);
}
And now I am stuck. Can someone either tell me a way thats miles better or help me finish off the script with some ideas?
Thanks
This is the code I have now after 2 answers:
$newarray = json_decode($content);
var_dump($newarray);
echo "<br><br>";
echo $newarray->{"mapname"};
The problem is that I am unable to display the variable in this format, how can I rebuild that so I can view the variables.
I have changed this:
echo $newarray->{"mapname"};
to this:
echo $newarray->data->rules->mapname;
This data is in json format. All you need to use to convert it into a php Array is json_decode($content)
Reference: http://us3.php.net/json_decode
And read more about json here: http://www.json.org/
You ar doing it wrong, it's not PHP serialized string, its json output you can use json_decode to get string into PHP array.
Like:
var_dump(json_decode($content, true));

PHP Echo shows full JSON line data not only what i required

recently i used some JSON data in JQuery. now i want the same thing in PHP.
For Example:
MySQL query returns data like
$result='{"username":"john","age":"18", "birthdate":"19880221"}';
but when i echo this data in PHP, like:
echo $result.username;
echo $result.age;
it shows output:
{"username":"john","age":"18", "birthdate":"19880221"}.username
{"username":"john","age":"18", "birthdate":"19880221"}.age
but i want output this:
John
18
PHP is not Javascript, the . operator is for string concatenation and PHP has no native support for JSON syntax. You need to convert the JSON formatted data into an array and use array syntax:
$result = json_decode($result, true);
echo $result['username'];
echo $result['age'];
php is not js. you have to decode data first, using json_decode
if $result really contains the data you posted
Your result is actually just a string, formated like JSON. It's not an array in PHP. If U want to handle JSON output, you can use json_decode() first, and then get access to data via PHP array syntax like this:
$result = json_decode($result,true);
echo $result['some_key'];

Categories