Hi everyone I have a json named rushlist.json using this format:
{"rushlist":[{"Char":"Whipthemout","Pass":1,"Fail":1,"Status":"Free"}]}
I figured out how to display the whole array with this:
<?php
$Rushlist = json_decode(file_get_contents("rushlist.json"), true);
print_r($Rushlist);
?>
It displays :
Array ( [rushlist] => Array ( [0] => Array ( [Char] => Whipthemout [Pass] => 1 [Fail] => 1 [Status] => Free ) ) )
However I want to display just the element/value not this:
Array ( [rushlist] => Array ( [0] => Array ( [Char] => Whipthemout [Pass] => 1 [Fail] => 1 [Status] => Free ) ) )
Something like this
Rushlist
[Char] = Whipthemout
[Pass] = 1
[Fail] = 1
[Status] = Free
Any help would be great!
Thanks.
EDIT
<?php
$Rushlist = json_decode(file_get_contents("rushlist.json"), true);
print_r($Rushlist);
foreach($Rushlist as $arr_name=>$arr) {
print $arr_name . "\n";
foreach($arr as $key=>$value) {
print "[" . $key . "] = " . $value . "\n";
}
}
?>
Displays this:
Array ( [rushlist] => Array ( [0] => Array ( [Char] => Whipthemout [Pass] => 1 [Fail] => 1 [Status] => Free ) ) ) rushlist
Notice: Array to string conversion in F:\Share\test\Dropbox\test\test3.php on line 9
[0] = Array
foreach($Rushlist as $arr_name=>$arr) {
print $arr_name . "\n";
foreach($arr as $arr2 ) {
foreach($arr2 as $key=>$value) {
print "[" . $key . "] = " . $value . "\n";
}
}
}
That should do it.
UPDATED
Related
Hey guys i am new of the laravel, how can i parse this output array? This is my array is coming from repeater using jquery.
Array
(
[tour_baslik] => 1. Day
[tour_icerik] => content here....
[lunch] => Array
(
[0] => 2
)
[dinner] => Array
(
[0] => 1
[1] => 2
)
)
Array
(
[tour_baslik] => 2.Day
[tour_icerik] => content 2 here...
[lunch] => Array
(
[0] => 1
[1] => 2
)
[dinner] => Array
(
[0] => 2
)
)
I need parse like that but i'm stuck:
foreach($myarray as $key => $data){
echo $key . '-' . $data; }
Output must be:
tour_baslik - 1.day
tour_icerik - content here..
lunch - 2
dinner - 1,2
If you need to iterate through all items of your input, you could use a recursive function like the following:
function iterator($key, $value) {
if (is_array($value)) {
foreach ($value as $key => $value) {
iterator($key, $value);
}
} else {
echo !empty($key) ? "$key - " : "";
echo $value."\n";
}
}
iterator(null, $input);
How to parse below array returned by sphinx in php
php code
if ( $result === false ) {
echo "Query failed: " . $cl->GetLastError() . ".\n";
}
else {
if ( $cl->GetLastWarning() ) {
echo "WARNING: " . $cl->GetLastWarning() . "<br>";
}
if ($result['0'] > 0) {
// print_r($result['0']['attrs']);
$words[] = $result['0']['matches'];
foreach ($words as $val) {
echo "=> " . $val['keyword'] . $val['ref'];
}
} else {
echo 'No results found';
}
}
Array
(
[0] => Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => keyword
[1] => ref
)
[attrs] => Array
(
[keyword] => 7
[ref] => 7
)
[matches] => Array
(
[25367434949034415] => Array
(
[weight] => 1643
[attrs] => Array
(
[keyword] => hunting
[ref] => activity
)
)
)
[total] => 1
[total_found] => 1
[time] => 0.000
[words] => Array
(
[hunt] => Array
(
[docs] => 1
[hits] => 1
)
)
)
)
I want to parse 'matches' array and it's subarray values like keyword, ref etc.
Lets concentrate on this bit
if ($result['0'] > 0) {
$words[] = $result['0']['matches'];
foreach ($words as $val) {
echo "=> " . $val['keyword'] . $val['ref'];
}
}
Firstly, the result isnt an integer, so shouldnt really be compared as one (although guess it might work)
Personally would recommend checking the actual matches - and using empty, which works fine on arrays...
if (!empty($result['0']['matches'])) {
Then you for some reason add the matches to an array (thats what $arry[] = does, its effectively 'push')
... but also no point assigned to a variable, as only use it once (in the foreach)
... Plus generally the document_id is the index of the matches array, so expose that.
foreach ($result['0']['matches'] as $doc_id => $val) {
finally, now have the matches, want the attribute in the attors array. so something like
$a = $val['attrs'];
echo $doc_id." => ".$a['keyword'].", ".$a['ref']."<br>\n";
putting it all together...
if (!empty($result['0']['matches'])) {
foreach ($result['0']['matches'] as $doc_id => $val) {
$a = $val['attrs'];
echo $doc_id." => ".$a['keyword'].", ".$a['ref']."<br>\n";
}
}
This has probably been asked many times but I can't find a solution for my case.
This is my array :
$request=Array (
[0] => Array ( [staName] => Auditorium Stravinsky 2m2c )
[1] => Array ( [staName] => Geneva Arena )
[2] => Array ( [staName] => Les Docks )
[3] => Array ( [staName] => Kheops )
)
And i need an output as follows as JSON:
"Auditorium Stravinsky 2m2c ","Geneva Arena","Les Docks","Kheops"
My current code is as follows:
foreach($request as $value)
{
$names[]=$value;
}
$jsonValue = json_encode(array_values($names));
print_r($jsonValue);
And my current output is as follows in JSON format:
[{"staName":"Auditorium Stravinsky 2m2c "},{"staName":"Geneva Arena"},{"staName":"Les Docks"},{"staName":"Kheops"}]
How can i stop "staName " from being outputed?
Many thanks in advance and please be considerate of my post as this is only the second one I make on this site.
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ) ,
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$newArray=array();
for($i=0;$i<count($request);$i++){
$newArray[$i]=$request[$i]['staName'];
}
$newArray=json_encode($newArray,true);
print_r($newArray);
And the output is a merged json:
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
You can achieve this from,
Code
$a = array();
foreach($request as $key =>$val){
foreach($val as $k => $v){
$a[] = $v;
}
}
print_r(json_encode($a));
Check this Demo link
Output
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
fist of all your array definition are not correct. and your output is simple string not a JSON
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ),
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$name = '';
foreach($request as $value)
{
foreach($value as $value2)
{
$name = $name . ' ' . $value2;
}
}
echo $name;
Output
Auditorium Stravinsky 2m2c Geneva Arena Les Docks Kheops
Here's my PHP code:
<?php
$json_url = "REDACTED";
$json = file_get_contents($json_url);
$stats = json_decode($json, true);
foreach ($stats as $row) {
echo $row['results']['result']['conversions'] . "<br />";
}
?>
Here's the JSON:
{
"metadata":{
"iserror":"false",
"responsetime":"0.07s"
},
"results":{
"first":1,
"last":99,
"total":99,
"result":[
{
"total_visitors":"3",
"visitors":"3",
"conversions":"0"
},
{
"total_visitors":"26",
"visitors":"26",
"conversions":"0"
},
{
"total_visitors":"13",
"visitors":"13",
"conversions":"0"
},
{
"total_visitors":"1",
"visitors":"1",
"conversions":"0"
},
{
"total_visitors":"1",
"visitors":"1",
"conversions":"0"
}
]
}
}
Essentially I'm just trying to echo the "conversions" from each section in the json file.
Never worked with a JSON file using PHP for, so I'm not really sure where I'm going wrong with this.
Need small correction in accessing array like below
foreach ($stats['results']['result'] as $row) {
echo $row['conversions'] . "<br />";
}
Because when you do json_decode, you will get array like below
Array
(
[metadata] => Array
(
[iserror] => false
[responsetime] => 0.07s
)
[results] => Array
(
[first] => 1
[last] => 99
[total] => 99
[result] => Array
(
[0] => Array
(
[total_visitors] => 3
[visitors] => 3
[conversions] => 0
)
[1] => Array
(
[total_visitors] => 26
[visitors] => 26
[conversions] => 0
)
[2] => Array
(
[total_visitors] => 13
[visitors] => 13
[conversions] => 0
)
[3] => Array
(
[total_visitors] => 1
[visitors] => 1
[conversions] => 0
)
[4] => Array
(
[total_visitors] => 1
[visitors] => 1
[conversions] => 0
)
)
)
)
It should be like following:
<?php
$json_url = "REDACTED";
$json = file_get_contents($json_url);
$stats = json_decode($json, true);
if ($stats && isset($stats['results']) && isset($stats['results']['result'])) {
foreach ($stats['results']['result'] as $row) {
echo $row['conversions'] . "<br />";
}
}
?>
So check if needed fields are set in JSON, and then loop for every result record to get the conversions.
$stats = json_decode($json, true);
foreach ($stats['results']['result'] as $row) {
echo $row['conversions'] . "<br />";
}
I have this JSON-File here
{
"id" : "bf75b277-169b-49da-8ab1-b78b8dfg1b43-e25c7f28b3",
"ts" : "1372751172664",
"connected" : {
"ssid" : "eduroam",
"bssid" : "00:0f:f9:eb:08:81",
"rssi" : "-62",
"speed" : "53"
},
"configured" : [
{
"ssid" : "eduroam",
"bssid" : "null",
"keyMgmnt" : ["2", "3"],
"grCiphers" : ["0","1","2","3"]
},
{
"ssid" : "foobar",
"bssid" : "null",
"keyMgmnt" : ["0"],
"grCiphers" : ["0","1","2","3"]
}
],
"location" : {
"prov" : "network",
"lat" : "52.3793203",
"lon" : "9.7231332",
"acc" : "22.777"
}
}
and I'm trying to get the key-value-pairs out into a file (and later into a mysql-database).
I am having trouble to go along the nested structure. Maybe I do not understand it correctly?
$LOGPATH = "/var/www/test/";
$out = fopen($LOGPATH."testlog.log", "a");
$result = file_get_contents('php://input');
$data = json_decode($result, true);
$value = $data;
$test = array();
This line beneath causes me headaches, how can I say get the key-value-pairs of "connected"?
Trying $test = $data['connected'] did not work, as the output simply is a "{" and nothing more...
$test = $data;
fwrite($out, "test \n");
fwrite($out, $test);
fwrite($out, "\n");
foreach ($test as $entry){
fwrite($out, $entry);
fwrite($out, "\n");
}
Any idea how to extract the key-value-pairs and/or help me understand the structure?
This should get you on the right track
foreach($data['connected'] as $key => $value){
echo 'Key: '.$key.' Value:'.$value.'<br>';
}
json_decode() will dump JSON into regular array. You can do print_r() or var_dump() on it, to see the structure of the array. So to get your connected leaf you do:
$connected = $data['connected'];
You can then iterate over it:
foreach( $connected as $key=>$val ) {
echo $key . ": " . $val;
}
Got it, thanks to you guys!
// Get a request from i.e. a webpage (this is a webservice)
$jsonArray = file_get_contents('php://input');
// put the JSON-Data into an array
$jsonData = json_decode($jsonArray, true);
// You can simply choose the entry-points. As they do not change for me, they are hardcoded in here, else you had to use something like a for-loop to extract them
$jsonConnect = $jsonData['connected'];
$jsonLocation = $jsonData['location'];
$jsonConfigured = $jsonData['configured'];
// walk through the JSON-Data and extract the values, although the array has more than 2 levels, you can select your entry-point and walk from there on, which makes this quite easy
for($i = 0; $i < count($jsonConfigured); $i++){
// keyMgmnt itself is an array and I want to concatenate all of its values and put it into a single field in my MySQL-DB, so implode() is used
$keyMgmnt = implode(",", $jsonConfigured[$i]['keyMgmnt']);
$grCiphers = implode(",", $jsonConfigured[$i]['grCiphers']);
$ssid = $jsonConfigured[$i]['ssid'];
$bssid = $jsonConfigured[$i]['bssid'];
// from here on I simply put the variables into my MySQL-DB
$sql_configured = "INSERT INTO configured (keyMgmnt, grCiphers, ssid, bssid) VALUES ('$keyMgmnt', '$grCiphers', '$ssid', '$bssid')";
mysql_query($sql_configured) or die ("Failure in configured");
}
$sql_connected = "INSERT INTO connected (rssi, speed, ssid, bssid) VALUES ('$jsonConnect[rssi]', '$jsonConnect[speed]', '$jsonConnect[ssid]', '$jsonConnect[bssid]')";
$enterConnected = mysql_query($sql_connected) or die("Failure in connection!");
$sql_location = "INSERT INTO location (lat, prov, lon, acc) VALUES ('$jsonLocation[lat]', '$jsonLocation[prov]', '$jsonLocation[lon]', '$jsonLocation[acc]')";
$enterLocation = mysql_query($sql_location) or die("Failure in location!");
Thanks again!
Case closed.
PS: The structure of the JSON-Data. You get it by using "print_r()" or "var_dump()".
(
[id] => bf75b277-169b-49da-8ab1-b78b80f51b43-e25c7f28b3
[ts] => 1372751172664
[connected] => Array
(
[ssid] => eduroam
[bssid] => 00:0f:f7:eb:08:81
[rssi] => -62
[speed] => 54
)
[configured] => Array
(
[0] => Array
(
[ssid] => eduroam
[bssid] => null
[keyMgmnt] => Array
(
[0] => 2
[1] => 3
)
[grCiphers] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
[1] => Array
(
[ssid] => foobar
[bssid] => null
[keyMgmnt] => Array
(
[0] => 0
)
[grCiphers] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
)
[location] => Array
(
[prov] => network
[lat] => 52.3793203
[lon] => 9.7231332
[acc] => 22.777
)
)
(
[id] => bf75b277-169b-49da-8ab1-b78b80f51b43-e25c7f28b3
[ts] => 1372751172664
[connected] => Array
(
[ssid] => eduroam
[bssid] => 00:0f:f7:eb:08:81
[rssi] => -62
[speed] => 54
)
[configured] => Array
(
[0] => Array
(
[ssid] => eduroam
[bssid] => null
[keyMgmnt] => Array
(
[0] => 2
[1] => 3
)
[grCiphers] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
[1] => Array
(
[ssid] => foobar
[bssid] => null
[keyMgmnt] => Array
(
[0] => 0
)
[grCiphers] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
)
[location] => Array
(
[prov] => network
[lat] => 52.3793203
[lon] => 9.7231332
[acc] => 22.777
)
)