When I dump the variable it has a data:
var_dump($result);
object(stdClass)#2 (5) { ["user_id"]=> string(1) "1" ["username"]=> string(6) "user_name" ["email"]=> string(14) "test#mail.com" ["password"]=> string(32) "password" ["test"]=> string(1) "1" }
But when I use foreach no value has return except if I just echo $data; but I want to be specific to the value I want to get.
foreach($result as $data){
echo $data->use_id;
echo $data->username;
}
Why there is no returned value?
Try this : json_decode converts json string to array. Ref: http://php.net/manual/en/function.json-decode.php
$array = json_decode($result, true);
echo "<pre>";
print_r($array);
You can use foreach to $array and echo the result.
You don't need to iterate over the object. Just do this.
echo $result->user_id;
echo $result->username;
With the sample you provided you are treating each property in $result as an object and looking for user_id and user_name (which don't exist).
As you wrote:
var_dump($result);
object(stdClass)#2 (5) { ["user_id"]=> string(1) "1" ["username"]=> string(6) "user_name" ["email"]=> string(14) "test#mail.com" ["password"]=> string(32) "password" ["test"]=> string(1) "1" }
So the result is an object itself. so if you want specify and fetch exact variable, there's no need to iterate it!
Related
I'm a little confused I have the following;
array(4) { ["id"]=> string(1) "2" ["result"]=> array(5) { ["account"]=> string(34) "rf2DKsfbZuCa2WwQAg49cRXqYuRCTszUTp" ["assets"]=> array(1) { ["rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ"]=> array(1) { [0]=> array(2) { ["currency"]=> string(3) "FLX" ["value"]=> string(9) "999999999" } } } ["ledger_hash"]=> string(64) "E44084AAC1F1AE2C7147675BF8F763ED7620DC82E1B1A5BD6DC6F640652F1B63" ["ledger_index"]=> int(17773072) ["validated"]=> bool(true) } ["status"]=> string(7) "success" ["type"]=> string(8) "response" }
I am trying to echo the currency;
I have the following but it's not working.
$response = websocket_read($sp,$errstr);
$decode = json_decode($response, true);
echo'<br><br>';
echo $decode['result']['assets']['currency'];
echo'<br><br>';
echo "Server responed with: '" . $response ."'\n";
The line that is not working is echo $decode['result']['assets']['currency'];
Thankyou
Your rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ has the value as an array, either remove the square brackets from this part to treat it as value
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":[{"currency":"FLX","value":"999999999"}]
to
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":{"currency":"FLX","value":"999999999"}
or use index to get the value
echo $decode['result']['assets']['rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ'][0]['currency'];
Im new to php and developing. please find below the code i used.
$data['ques']=$this->questions_model->displayquestionbyid($questionid);
var_dump($data);
The out put prints as
array(1) { ["ques"]=> array(1) { [0]=> object(stdClass)#19 (6) { ["questionid"]=> string(1) "1" ["votecount"]=> string(1) "0" ["username"]=> string(3) "hip" ["catogoryid"]=> string(1) "2" ["questionTitle"]=> string(4) "e2e2" ["description"]=> string(6) " deded" } } }
How do i take value of username and save it under php variable.Thank you
It's hard to tell from the output format you gave, but you probably want to do:
$data = $this->questions_model->displayquestionbyid($questionid);
$username = $data[0]->username;
$data = $this->questions_model->displayquestionbyid($questionid);
$username = $data['ques'][0]->username;
check if this works and please debug using print_r() instead.
array(2) {
["success"]=>
string(1) "1"
["return"]=>
array(125) {
[0]=>
array(11) {
["marketid"]=>
string(3) "141"
["label"]=>
string(6) "42/BTC"
["primary_currency_code"]=>
string(2) "42"
["primary_currency_name"]=>
string(6) "42Coin"
["secondary_currency_code"]=>
string(3) "BTC"
["secondary_currency_name"]=>
string(7) "BitCoin"
["current_volume"]=>
string(10) "0.11628537"
["last_trade"]=>
string(12) "223.00000000"
["high_trade"]=>
string(12) "256.88999999"
["low_trade"]=>
string(12) "205.00000000"
["created"]=>
string(19) "2014-01-12 19:35:49"
}
}
I was trying to process this in PHP
$result = api_query("getmarkets");
$json = json_decode($result);
var_dump($result);
I tried many times to start processing this data but how would I start this
I was thinking something like $json['return'][0]['marketid'] would grab the market id.
Looks like it is already php object, so no need to parse as json.
try...
$result['return'][0]['marketid']
... and delete ...
$json = json_decode($result);
I returned an array of JSON data type from javascript to PHP, I used json_decode($data, true) to convert it to an associative array, but when I try to use it using the associative index, I get the error "Undefined index" The returned data looks like this
array(14) { [0]=> array(4) { ["id"]=> string(3) "597" ["c_name"]=> string(4) "John" ["next_of_kin"]=> string(10) "5874594793" ["seat_no"]=> string(1) "4" }
[1]=> array(4) { ["id"]=> string(3) "599" ["c_name"]=> string(6) "George" ["next_of_kin"]=> string(7) "6544539" ["seat_no"]=> string(1) "2" }
[2]=> array(4) { ["id"]=> string(3) "601" ["c_name"]=> string(5) "Emeka" ["next_of_kin"]=> string(10) "5457394839" ["seat_no"]=> string(1) "9" }
[3]=> array(4) { ["id"]=> string(3) "603" ["c_name"]=> string(8) "Chijioke" ["next_of_kin"]=> string(9) "653487309" ["seat_no"]=> string(1) "1" }
Please, how do I access such array in PHP? Thanks for any suggestion.
As you're passing true as the second parameter to json_decode, in the above example you can retrieve data doing something similar to:
$myArray = json_decode($data, true);
echo $myArray[0]['id']; // Fetches the first ID
echo $myArray[0]['c_name']; // Fetches the first c_name
// ...
echo $myArray[2]['id']; // Fetches the third ID
// etc..
If you do NOT pass true as the second parameter to json_decode it would instead return it as an object:
echo $myArray[0]->id;
$data = json_decode($json, true);
echo $data[0]["c_name"]; // "John"
$data = json_decode($json);
echo $data[0]->c_name; // "John"
$data = json_decode(...);
$firstId = $data[0]["id"];
$secondSeatNo = $data[1]["seat_no"];
Just like this :)
As you're passing true as the second parameter to json_decode, in the above example you can retrieve data doing something similar to:
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
This may help you!
$latlng='{"lat":29.5345741,"lng":75.0342196}';
$latlng=json_decode($latlng,TRUE); // array
echo "Lat=".$latlng['lat'];
echo '<br/>';
echo "Lng=".$latlng['lng'];
echo '<br/>';
$latlng2='{"lat":29.5345741,"lng":75.0342196}';
$latlng2=json_decode($latlng2); // object
echo "Lat=".$latlng2->lat;
echo '<br/>';
echo "Lng=".$latlng2->lng;
echo '<br/>';
When you want to loop into a multiple dimensions array, you can use foreach like this:
foreach($data as $users){
foreach($users as $user){
echo $user['id'].' '.$user['c_name'].' '.$user['seat_no'].'<br/>';
}
}
I want to create a JSON object from my MySQL results with PHP so I can pass it to JavaScript. I don't quite understand the difference between JSON array and JSON object.
This is how I do it. But is there a better way? This is the array way I believe?
$json = array();
while($r=mysql_fetch_array($res)){
$json['firstname'] = $r['firstname'];
$json['lastname'] = $r['lastname'];
}
echo json_encode($json);
I want to be able to get the info from JavaScript, by selecting all first names only If I wish etc..
you can try this, fetch data and push to array, then echo that array
$info=array();
while($row = mysql_fetch_array($res,MYSQL_ASSOC)){
array_push($info,$row);
}
echo json_encode($info);
would return
array(2) { [0]=> array(3) { ["id"]=> string(1) "1" ["firstname"]=> string(3) "foo" ["lastname"]=> string(3) "bar" } [1]=> array(3) { ["id"]=> string(1) "2" ["firstname"]=> string(3) "foo" ["lastname"]=> string(3) "bar" } }
json
[{"id":"1","firstname":"foo","lastname":"bar"},{"id":"2","firstname":"foo","lastname":"bar"}]
Well this would encode every row, with each row being the JSON Object:
$json = array();
while($r=mysql_fetch_array($res)){
$json[] = $r;
}
echo json_encode($json);