I'm trying to parse the result in PHP from a SOAP request.
Sometimes the response can be in a single object, but sometimes it can be multiple array results, and then my PHP breaks.
If it's only a single result (Object), then I have no problem to print it out:
<?php
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fv12->FV12_Agare_fr_o_m;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg01->FG01_Personnummer;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg01->FG01_Namn;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg02->FG02_Adress;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg02->FG02_Postnummer;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg02->FG02_Postort;
?>
But when it's multiple responses (Array), I have to use something like:
<?php
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare[0]->fg01->FG01_Namn;
?>
Is it possible to make loop of some kind instead to always show the results?
Are you looking for this simple solution:
foreach($nuvarandetidigareagarefraga_output->return->foregaendeAgare as $element){
print $element->fg01->FG01_Namn;
}
?
If yes then please consult php manual: http://php.net/manual/en/language.types.array.php and http://php.net/manual/en/language.control-structures.php section about loops.
Related
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>
I'm trying to retrieve a URL string from some json code.
Here is the json code
{"files":["www.example1.com"],"previews":["www.example1preview.com"],"meta":{},"userId":"guest","product":{"id":"2335","name":"standard"},"type":"u"}
Looking at what I've seen in the PHP manual I'm trying to retrieve previews like this.
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->{'previews'};
?>
This doesn't seem to output the value however.
By experimenting with php -a command on terminal, I've put your json into json_decode and managed to get your link by just doing:
print $meta_value_json->previews[0];
The only reason to use print $meta_value_json->{'previews'}; at least according to php documentation is if you want an object as output and the key trying to retrieve is numerical or of a type that is not supported by php.
By experimenting a bit further, the reason that print $meta_value_json->{'previews'}; fails is because print expects a string, in our case here previews is an array. Therefore if you do print $meta_value_json->{'previews'}[0]; it will also work as expected.
You need to get the value from your decoded json like this: $class->parameter.
Knowing that previews is an array, you will also need to choose a specific element from it to print ( I got the first one ):
<?php
ob_start();
include('getjson.php');
$meta_value_json = ob_get_clean();
echo $meta_value_json;
$meta_value_json = json_decode($meta_value_json);
print $meta_value_json->previews[0]; /// get the specific value
?>
I'm trying to learn JSON and PHP using API.
My problem is that I can't get out the data of example Array 0, the output as can seen at bottom if you visit the link, just Array googled as mutch as I can but kinda new on the json array.
$json_data=json_decode($json_array,true);
print_r($json_data);
echo "<br>";
echo $json_data['total'],"<br>";
echo $json_data['skipped'],"<br>";
echo $json_data['count'],"<br>";
echo $json_data['0'],['name'],"<br>";
Based on your comment, I think I see what you want to do. You're trying to access the second level of your array incorrectly. You don't put anything between the two bracket sets. You also don't need to put the 0 in quotes, as it is not a string. I think you want this:
$json_data=json_decode($json_array,true);
print_r($json_data);
echo "<br>";
echo $json_data['total'],"<br>";
echo $json_data['skipped'],"<br>";
echo $json_data['count'],"<br>";
echo $json_data[0]['name'],"<br>"; // Line changed, removed comma and quotes.
EDIT: Note that the [0]['name'] is inside the hits value, so you'll probably want this instead:
echo $json_data['hits'][0]['name'];
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();
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.