i am trying to iterate a result set using foreach loop. Below is my array when i do print_r on result set
Array
(
[0] => stdClass Object
(
[xml] =>
[qid] => 406
[title] => Q by Sikander
[description] => test question created by Sikander
)
)
Using following foreach loop
foreach ($xmls_nodes as $value){
echo $value->qid ;
echo $value->xml ;
echo $value->title ;
echo $value->description ;
}
now as a result it prints following
406 Q by Sikander test question created by Sikander
but right after this it also prints following for each of the record and row
A PHP Error was encountered Severity: Notice
Message: Trying to get property 'qid' of non-object
it give same line no in for error where i am printing these values and using die statement after this, please help me to get rid of this issue
You have to convert this object to array and then just use it as array:
$arr = json_decode(json_encode($xmls_nodes), true);
You have to do JSON-encode your object and then decode it back to an array
[0] => stdClass Object
$arr = json_decode(json_encode($xmls_nodes), true);
then loop through it
Related
I'm using PHP SoapClient to call a SOAP service.
I'm using the following code to return the below view
$response = $client->Get($request);
echo '<pre>';
print_r($response);
echo '</pre>';
echo '<hr>';
Here's the response
stdClass Object
(
[TransactionID] => 17ACE7B75CB6SDBX
[ResponseType] => SYNC
[Parameters] => stdClass Object
(
[Param] => stdClass Object
(
[_] => SANDBOX20101001123321125.168.214.72125.168.253.29912003D60E04CC4C1F8
[id] => SessionLogRecord
)
)
)
Here is , showing the elements within it that I want to extract the values of
<onlinesessionrecord><serviceid>SANDBOX</serviceid><datetime>20101001123321</datetime><ipaddress>125.168.214.72</ipaddress><nasipaddress>125.168.253.2</nasipaddress><nasport>9912</nasport><sessionid>003D60E04CC4C1F8</sessionid></onlinesessionrecord>
For example, I am trying to extract from above
I have tried to display it using the following, but I get an error "Attempt to read property "onlinesessionrecord" on string"
$data = $response->Parameters->Param->_->onlinesessionrecord->ipaddress;
print_r($data);
If anyone knows what I'm doing wrong, your advice would be appreciated.
I am sure this is very simple, I just seem to struggle with xml.
--- Next Day, further findings after help from comments ---
So, thanks to the comments below, I have now go to this point. If I use the below:-
$data = simplexml_load_string($response->Parameters->Param->_);
$out = $data->IPAddress; // same if I do the following $data[0]->IPAddress;
print_r($out);
I get this:-
SimpleXMLElement Object ( [0] => 125.168.214.72 ) if I do the below.
But I just want to extract the IPAddress value and assign it to a variable. I have tried 2 ways
$out = $data->IPAddress;
and
$out = $data[0]->IPAddress;
Both give the same output, I just want to actual IPAddress and assign it to a variable.
i am new to JSON i have a json object retrieved from the database in the form of
Array
(
[0] => stdClass Object
(
[id] => 1
[data] => {"vehicle":[{"year":"2000","make":"Ac","model":"Aceca","acquired_year":"2016","acquired_month":"2","use":"Business","distance_driven_to_work_or_school":"2","distance_driven_manually":"10000"}],"first_name":"ADAS","last_name":"DSADSADA","email":"asddsa#sda.com","phone":"dsasasa","postal_code":"","drivers":[{"name":"ssada","birth_year":"2016","birth_month":"2","birth_day":"2","gender":"female","martial_status":"Single","license_number_provided":"yes","license_number":"asddasdas","license_type":"","training_completed":"","years_been_listed_on_auto_policy_in_north_america":"No Previous Experience","license_suspensions":"","accidents":"Select","convictions":"Select","cancellation_reason":"","cancellation_year":"","cancellation_month":"","cancellation_day":""}],"considering_renters_to_reduce_rate":"yes","install_winter_tires":"no","park_in_private_driveway":"yes","willing_to_install_device":"no","years_insured_with_current_company":"4 Years","how_heard_about_us":"asdaa"}
[date] => 2017-11-20 18:17:52
[status] => 0
)
)
now when i try to use json_decode to convert it into an array i am getting Trying to get property of non-object here's my code
<?php
echo "<pre>";
print_r($quotes); //works fine uptil here
$data = json_decode($quotes->data,true);//the error line
echo "<pre>";
print_r($data);
?>
i tried it a couple of ways but it is not working i tried some other solutions as well stil ending up getting errors any help?
It is because $quotes is an array of objects. Try $quotes[0]->data, e.g.:
$data = json_decode($quotes[0]->data,true);
// ------------------------^^^
You're receiving an array containing objects from the database. You're almost there but instead of
$data = json_decode($quotes->data,true);
You should use
$data = json_decode($quotes[0]->data,true);
I want to echo an array_chunk as string, how do I do that ?
here is the code
$rt = $this->db->query("SELECT id_reg_pd FROM 043104_kuliahmhs_20152_2a0dc380_temp");
$_datao = array_chunk($rt->result(), 3);
foreach($_datao as $batman => $robin) {
print_r($robin);
}
I want echo id_reg_pd as string.
I have tried tried :
echo $robin->id_reg_pd;
but get php error like this
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Here the array from print_r($robin);
Array
(
[0] => stdClass Object
(
[id_reg_pd] => 001be76b-4e58-4cea-96cf-fee2d8e0abdc
)
[1] => stdClass Object
(
[id_reg_pd] => 001d4fe5-73f5-4bae-b126-1f787ea0104e
)
[2] => stdClass Object
(
[id_reg_pd] => 002ab28b-e0b9-464a-89fb-12552512a5d0
)
)
Loop over $robin and then check
foreach($robin as $value)
{
echo $value->id_reg_pd;
}
try like this
for($i=0;$i<count($_datao);$i++){
$newarr = (array) $robin[$i];
echo $newarr['id_reg_pd'];
}
Sahil is incorrect. It is not true that you must use a for / foreach loop to achieve your desired result. array_column() works on an array of objects. If you can use a simple implode() call to convert your array to a string, then here is a simple one-liner:
Code (Demo):
$robin=[
(object)['id_reg_pd'=>'001be76b-4e58-4cea-96cf-fee2d8e0abdc'],
(object)['id_reg_pd'=>'001d4fe5-73f5-4bae-b126-1f787ea0104e'],
(object)['id_reg_pd'=>'002ab28b-e0b9-464a-89fb-12552512a5d0']
];
//print_r($robin); // uncomment to see for yourself
//var_export(array_column($robin,'id_reg_pd')); // uncomment to see for yourself
echo implode(', ',array_column($robin,'id_reg_pd')); // implode with whatever glue you wish
Output:
001be76b-4e58-4cea-96cf-fee2d8e0abdc, 001d4fe5-73f5-4bae-b126-1f787ea0104e, 002ab28b-e0b9-464a-89fb-12552512a5d0
I've experienced a problem when developing for a minecraft server: I have an array like this which is received as json and encoded but for processing the output of this I need to know how to access the array and echo one of the values for Example the Version Key.I tried to save the array as variable $json and to access it via echo $json->Version; but this results in the output of this error: Notice: Trying to get property of non-object in /home/bs-web/joomla/api/test2.php on line 12
HereĀ“s the Array:
Array
(
[0] => stdClass Object
(
[GameName] => MINECRAFT
[Version] => 1.8
[Plugins] =>
[Map] => BungeeCord_Proxy
[Players] => 7
[MaxPlayers] => 100
[HostPort] => 25565
[HostIp] => 188.40.97.86
[Software] => Vanilla
)
)
Your variable $json is an array, you cannot get property of an array. Assuming you have only one element in the array as in your example, you should use
echo $json[0]->Version;
to display the version(1.8 in your example).
once you saves the $json variable as an array, you only can access its values passing the key that you want to use, and then, point to the object. In your case, to get the Version property, you will need to use $json[0]->Version.
To access and echo all values of your object you can use a foreach loop to iterate with it.
foreach ($json as $object){
echo $object->GameName;
echo $object->Version;
echo $object->Plugins;
.... // All properties that you want
}
Hope it helps you, hugs.
You could also use json_decode($json, true) then you get an assocative array which you can access like this $json['Version']
I am using $this->db->get_where() to get data from database in codeigniter.
Its returning following which I got using print_r()
Its looks like array of stdClass object. Anyone who how to access values inside this array.
Array ( [0] =>
stdClass Object (
[id] => 1
[password321] => qwerty
[email123] => example#gmail.com
[username123] => xyz
)
)
It shows an array of objects. There is only one object in it.
If:
$var = $this->db->get_where();
Then:
echo $var[0]->id;
Access it like any other object.
echo $array[0]->id //1
echo $array[0]->username123 //xyz
And so on. If you have multiple objects inside the array, run it through a for loop to iterate the array.
For example:
for ($i=0;$i<sizeof($array);$i++) {
echo $array[$i]->[object property];
}