avoid $ sign on php for print array value from json - php

I have to Simple code retrieve data from json.i got problem with json properties that contains like department$_identifier. 'department$_identifier' properties contains the value.but php count it as a variable.how to escape this.
Here is my code :
foreach ($data as $key => $Attendance) {
if ($Attendance->department$_identifier == 'Administration') {
echo $Attendance->department$_identifier;
echo $Attendance->attendanceDate;
echo $Attendance->status;
echo $Attendance->_entityName;
}
}
How to solve this

use {} like as
echo $Attendance->{'department$_identifier'};
http://php.net/manual/en/function.json-decode.php

Try this
foreach ($data as $key => $Attendance) {
if ($Attendance["department$_identifier"] == 'Administration') {
echo $Attendance["department$_identifier"];
echo $Attendance["attendanceDate"];
echo $Attendance["status"];
echo $Attendance["_entityName"];
}
}

Related

How to get json property name with php

So here's the thing, I get this json from a url ( in my context i get it from a url, but let's say here I write my json in a variable :
$file = '[
{"status": "5.4.1","email": "dddddd#exelcia-it.com"},
{"status": "5.4.1",, "email": "sksksksk#exelcia-it.com"}
]'
Then I do $json = json_decode($file,true);
And I want to get all the emails so I do :
foreach ($json as $key => $value) {
echo $value["email"]. "<br>";
}
But what I also need, is to return something like that from the loop (only for one property):
"email = dddddd#exelcia-it.com".
So I need to also get the name of the property but I can't figure this out.
I tried
foreach($json as $key => $propName){
echo $key.'<br>';
}
But I just get the index (0,1,...), not what I want.
Thanks!
You have to loop each json row, this should works:
$file = '[{"status": "5.4.1","email": "dddddd#exelcia-it.com"},{"status": "5.4.1", "email": "sksksksk#exelcia-it.com"}]';
$json = json_decode($file,true);
foreach($json as $row)
{
foreach($row as $key => $value)
{
echo "<b>".$key."</b>".':'.$value.'<br>';
}
}
Use this loop to get key and value pairs together.
foreach($data as $row)
{
foreach($row as $key => $val)
{
echo $key . ': ' . $val;
echo '<br>';
}
}
Ok thanks that's what I needed !
For me I just need the email properties and values, so I do :
foreach($json as $row)
{
foreach($row as $key => $value)
{
if($key=='email'){
echo "<b>".$key."</b>".':'.$value.'<br>';
}
}
}
Awesome ! thanks !

Read and print json array in php

I have a JSON array like below. I want to print only the values of the name. But I am getting undefined index name and getting value of name.below is my json.
[{"docId":{"id":"57dd70252a896558e573a0c8"},"docProfile":{"name":"gowtham","gender":null,"email":null,"mobile":"7406339908"},"docLocalInfo":{"username":"gowtham","otp":934343,"newPasswordToken":null,"tempMobile":"","adminVerfiy":null},"privateInfo":{"mciNumber":null,"aadharNumber":null,"panNumber":null},"tempHospitals":[],"bankInfo":null,"signupSteps":{"accountCreated":true,"otpValidated":true},"notification":null,"hospitals":[],"address":null}]
my code
foreach($doc_array as $data => $mydata)
{
foreach($mydata as $key=>$val)
{
echo $val['name'];
}
}
How to get the values of name from docProfile? Any help would be greatly appreciated
Inside your foreach you don't need to loop again since docProfile is an index of the json object array
Just simple access it
echo $mydata['docProfile']['name'].'<br>';
so your foreach would be like this
foreach($doc_array as $data => $mydata) {
echo $mydata['docProfile']['name'].'<br>';
}
Demo
Try to something Like this.
<?php
$string = '[{"docId":{"id":"57dd70252a896558e573a0c8"},"docProfile":{"name":"gowtham","gender":null,"email":null,"mobile":"7406339908"},"docLocalInfo":{"username":"gowtham","otp":934343,"newPasswordToken":null,"tempMobile":"","adminVerfiy":null},"privateInfo":{"mciNumber":null,"aadharNumber":null,"panNumber":null},"tempHospitals":[],"bankInfo":null,"signupSteps":{"accountCreated":true,"otpValidated":true},"notification":null,"hospitals":[],"address":null}]';
$arr = json_decode($string, true);
echo $arr[0]['docProfile']['name'];
?>
This array just have one row but if your array have more row you can use it;
you need to decode JSON at first.
$doc_array =json_decode($doc_array ,true);
foreach($doc_array as $key=> $val){
$val['docProfile']['name']
}
<?php
$json_str='[{"docId":{"id":"57dd70252a896558e573a0c8"},"docProfile":{"name":"gowtham","gender":null,"email":null,"mobile":"7406339908"},"docLocalInfo":{"username":"gowtham","otp":934343,"newPasswordToken":null,"tempMobile":"","adminVerfiy":null},"privateInfo":{"mciNumber":null,"aadharNumber":null,"panNumber":null},"tempHospitals":[],"bankInfo":null,"signupSteps":{"accountCreated":true,"otpValidated":true},"notification":null,"hospitals":[],"address":null}]';
$json_arr = (array)json_decode($json_str,true);
foreach($json_arr as $iarr => $ia)
{
foreach($ia["docProfile"] as $doc => $docDetails)
{
if($doc =="name")
{
echo $ia["docProfile"]["name"];
}
}
}
?>
This code gives you the answer

How do I have the keys and values in a php search show up on the same line

How do I have the keys and values in a php search query on JSON data show up on the same line. My code is listed below and has the key show up and then the value show up on the following line.
function funTimes($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>', $key ;
funTimes($item);
echo '</pre>';
} else {
echo '<pre>', $item, '</pre>';
}
}
echo '</ul>';
}
print(funTimes($results));
one of the outputs is an age field and it has age on one line and then the actual age showing up on the next line. When I use the following code per below:
function funTimes($tree)
{
foreach($tree as $key => $item)
{
if (is_array($item))
{
echo "{<br>";
funTimes($item);
echo "}<br>";
}
else
{
echo trim($key).": ".trim($item)."<br>";
}
}
}
print(funTimes($results));
my output displays as when I use print
key: age
value: 30
and when I use print_r my output displays as:
key: age
value: 30
Using the below code:
print "<pre>";
print_r($results);
print "</pre>";
gives me the following output:
[0] => Array
(
[key] => age
[value] => 30
)
Per Webeng's response below I've updated the answer and this code below gives me the solution I was looking for and has the key and value on the same line:
function funTimes($tree)
{
foreach($tree as $key => $item)
{
if (is_array($item))
{
echo "<br>";
funTimes($item);
echo "<br>";
}
else
{
echo $item." ";
}
}
}
print(funTimes($results));
Instead of using <pre>, it would be much easier to get your result of having the $key and $value of each element of the array next to each other by echoing them in a one lined string:
echo $key.": ".$item."<br>";
For example:
function funTimes($tree)
{
foreach($tree as $key => $item)
{
if (is_array($item))
{
echo "{<br>";
funTimes($item);
echo "}<br>";
}
else
{
echo $key.": ".$item."<br>";
}
}
}
Let me know if that worked for you.
Edited:
From the comment you gave me, it seems that what is causing the different lines might be characters inside your string. Replace this code:
echo $key.": ".$item."<br>";
with this code:
echo trim($key).": ".trim($item)."<br>";
What the trim() function does is it strips away any whitespace characters at the beginning and at the end of the string parameter. Let me know if it works now.
Edit 2:
After having read your updated answer and comments, this is the code I came up with:
For example:
function funTimes($tree)
{
foreach($tree as $key => $item)
{
if (is_array($item))
{
echo "{<br>";
funTimes($item);
echo "}<br>";
}
else
{
echo $item." : ";
}
}
echo "<br>";
}

How to loop through json array to convert it into single values and list them?

i have fetched a json output for menus to be displayed in the webpage.
I want to convert it into menus and sub menus. I have tried looping through it but it doesnt works.Please help me with this, thanks in advance.
$output = json_encode(array($menu_main_row['menu_name'] => $sub_menu));
Json Output:
{"Home":"[{\"Create\":\"create.php\"},{\"Edit\":\"edit.php\"}]"}
{"Projects":"[{\"My Projects\":\"myprojects.php\"},{\"My reports\":\"reports.php\"}]"}
This is the foreach loop which i used.
foreach($value as $main_menu_name=>$arr_val_sub)
{
echo "<ul>";
echo $main_menu_name;
$val=json_decode($arr_val_sub);
foreach ($val as $menu_name=>$menu_url)
{
echo $menu_name;
echo "<br>";
echo $menu_url;
}
}
The main menu name is printing, but when i print the sub menu name and url it prints like 0 1 .
You missed another step to dig into the json. $menu_url is another json to parse. Below code fixes it
foreach($value as $main_menu_name=>$arr_val_sub)
{
echo "<ul>";
echo $main_menu_name;
$val=json_decode($arr_val_sub);
foreach ($val as $menu_name=>$menu_url)
{
$submenu=new RecursiveArrayIterator($menu_url);
foreach ($submenu as $submenu_name=>$submenu_url)
{
echo $submenu_name;
echo "<br>";
echo $submenu_url;
echo "<br>";
}
}
}

foreach with two kind of values

<?php
foreach ($array['response']['data']['Offers'] as $arr) {
$gegevens = array(
$arr['Offer']['id'],
$arr['Offer']['name'],
$arr['Advertiser']['company'],
$arr['Offer']['advertiser_id'],
$arr['Offer']['offer_url'],
$arr['Offer']['default_payout'],
$arr['Offer']['expiration_date']
);
echo '<tr>';
foreach ($gegevens as $value) {
echo "<td>{$value}</td>";
}
echo "</tr>\n";
}
?>
This is the code I have.
How can I search for two kind of values inside the foreach($array['response']['data']?
It has to be foreach($array['response']['data'][**Offers**] and also foreach($array['response']['data'][**Advertisers**].
I need this so that I can echo out $arr['**Offer**']['name'], $arr['**Advertiser**'] ['company']
Can someone help me with this?
In its simplest:
foreach($array['response']['data']['Offers'] as $key => $offer_arr){
$advertiser_arr = $array['response']['data']['Advertisers'][$key];
}
You then have offer array and advertiser array of the same index
Doesn't sound like you need to put them all into an array. This should suffice:
<?php
foreach($array['response']['data']['Offers'] as $arr) {
echo '<tr>';
echo "<td>{$arr['Offer']['name']}</td>";
echo "<td>{$arr['Advertiser']['company']}</td>";
echo "</tr>\n";
}
?>

Categories