php


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 !

Related Links

WordPress mouse over select option
modify json response in php?
Symfony implementing registration
Move inner array values to parent array, reset keys and change key name to string
Attempt to update users table generates MethodNotAllowedHttpException, Laravel-4
Using HTML form to update Oracle Database - Database Not Updating
How to read json out in php as a string? [duplicate]
PayPal REST API issue in PPModel.php on line 51
Sub-pages always open domain root
codeigniter form validation for checkbox at least one selection
(403) Daily Limit for Unauthenticated Use Exceeded when using google-api-php-client
Reading contents from text/csv document with inconsistencies in data
use regex with array_diff() to exclude files with given extension
Array from mysql [duplicate]
Cannot modify header information - headers already sent by NOT be displayed with E_ALL error_reporting
.htaccess not running mod-rewrite or not being executed

Categories

HOME
python-2.7
opengl
protocol-buffers
mc
keyboard
stacktrace.js
docker-cloud
openflow
repair
eddystone
reportviewer
clish
sonata
jpa-2.0
sonicwall
scala-ide
xsl-fo
multiple-tables
multi-upload
pymongo
device
function-points
web-push
plyr
ipa
uri
android-service
sendmessage
d3.js-v4
windows2012
opnet
counting
cloveretl
polyml
macports
mars-simulator
hidden-markov-models
iscroll
microkernel
grunt-contrib-watch
resampling
yoast
solr-query-syntax
winmerge
asp.net-web-api-routing
connect-direct
case-when
front-camera
nitrousio
s-function
pyaudio
spring-mongo
msbuild-4.0
raw-sockets
android-sdcard
codecharge
polymerfire
openmrs
arbre
screen-orientation
domino-designer-eclipse
android-handler
componentart
unidata
dbmigrate
webproject
mgwt
hp-idol-ondemand
device-admin
zend-mail
quantify
reference-counting
compiler-flags
stty
objective-c-runtime
insertion
web-frameworks
oscilloscope
office-app
instance-variables
kext
fragment-identifier
nsconnection
hungarian-notation
mygeneration
jdownloader
spring-modules
sqlsitemapprovider
server-load
comment-conventions
levels

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App