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];
}
Related
I am working on a project to get the names of an array.
The arrays seem to be multidimensional, with the added bonus of being a stdclass Object. I am trying to select a key from the provided array but seem to have no luck selecting them.
echo($response->array[shoecompany]->array[1]->name);
from the information here
stdClass Object
(
[shoe] => shoemaker
[shoecompany] => Array
(
[0] => stdClass Object
(
[shoenumber] => 1
[name] => Blank
[1] => stdClass Object
(
[shoenumber] => 2
[name] => demo
)
[2] => stdClass Object
(
[shoenumber] => certificate
[name] => certofsale
)
)
)
Nothing i do seems to pull the information i need out of this. Any ways to go about pulling, said information.
The arrays seem to be multidimensional, with the added bonus of being a stdclass Object.
Arrays and objects aren't the same things.
I let you learn more about the specifics of both if you are curious.
Regarding access, yous use brackets - '[]' - when you want to access something in an array and an arrow - '->' - when you want to access an object's property :
$array['key'];
$object->property;
In your case, since only $response and the entries in the entry showcompany - I assume it's a typo - are objects, what you should write is :
$response->shoecompany[1]->name;
Which gives you in practical use :
foreach ($response->shoecompany as $val) {
echo $val->shoenumber, ' : ', $val->name, '<br>'; // Or whatever you want to print, that's for the sake of providing an example
}
If it is more convenient for you to handle exclusively arrays, you can also use get_object_vars() to convert an object properties to an array :
$response = get_object_vars($response);
Code should be like:
echo $response->shoecomapny[1]->name;
In short, to select key inside an object you need to use "->" operator and to select key inside array use "[]".
This is a common question, but I've not been able use solutions to what I'm working on. I'm having trouble understanding how echoing the values in an array works, any help is appreciated. The following code;
$sql = 'someQuery';
$dev='someString';
$device = $client->executeSQLQuery(array("sql"=>$sql));
echo "<pre>";
print_r($device);
echo "</pre>";
Will yield the following output;
stdClass Object
(
[return] => stdClass Object
(
[row] => Array
(
[0] => stdClass Object
(
[name] => someString
[userid] => user1
)
[1] => stdClass Object
(
[name] => someString
[userid] => user2
)
)
)
)
This is very easy to understand. I also understand that in order to echo the contents of this output I need to use a foreach loop. I'm trying to echo each instance of userid (which occurs twice in the output).
foreach($device as $first)
{
if( is_array($first->row) )
{
$userid = $first->userid;
foreach($userid as $second)
{
echo "<b>UserID</b><br> " . $device->return->row->userid . $
}
}
}
So, this does not work and likely because I didn't learn it the proper way the first time. In this case, as I understand it, the if statement doesn't even need to be there because we don't need to evaluate if the content is an array (it's not). Can someone explain the logic of how to go through the array and echo each instance of the object userid?
Pay attention on the print_r output. $device is stdClass not a array, it has a property called return which also is a stdClass. Once again has a property called row this property is a array.
So as #derek-pollard said, you need to do, something like this:
foreach($device->return->row as $info) {
echo "<b>UserID</b><br> " . $info->userid;
echo "<b>UserName</b><br> " . $info->name;
}
It'll work because $device->return->row will return the array of users. For beginners, the foreach will iterate once for each item (inside the array) and $info will have the content of the current item.
Finally it will print:
<b>UserID</b><br> user1
<b>UserName</b><br> someString
<b>UserID</b><br> user2
<b>UserName</b><br> someString
In your example row is an array of objects. userid is just an object's property which is assigned with primitive value. You can't iterate through such kind of properties:
foreach($device->return->row as $obj)
{
echo $obj->userid; // this gives 'userid' value
}
Your assumption will be reasonable if you would store some array in userid property
I am very basic new php learner, i having difficulty to get nested array value, here is my json result:
stdClass Object
(
[title] => Aao Raja - Gabbar Is Back | Chitrangada Singh
[link] => stdClass Object
(
[22] => Array
(
[0] => http://r8---sn-aigllnsk.c.docs.google.com/videoplayback?mime=video%2Fmp4&id=o-AExJcTxRDvCYsfgA1cIvQDs1v-pvLhKjTPdDh67X19vz&dur=145.542&itag=22&pl=48&ip=2a03:b0c0:1:d0::2f6:c001&sparams=dur,expire,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,nh,pl,ratebypass,source,upn&key=cms1&sver=3&expire=1437035009&upn=9lTw9Popb18&ratebypass=yes&source=youtube&lmt=1432539432699196&fexp=901816%2C9407809%2C9408142%2C9408420%2C9408710%2C9409172%2C9412774%2C9412846%2C9413149%2C9415664%2C9415958%2C9416126%2C9416370%2C9416656&ipbits=0&signature=3547894526817B37774A7838F8B68493CDD62101.3F143C74D76E8705800445A4CD4476C4F8BCD988&cms_redirect=yes&mm=31&mn=sn-aigllnsk&ms=au&mt=1437013301&mv=m&nh=IgpwcjAzLmxocjE0KgkxMjcuMC4wLjE&utmg=ytap1
[1] =>
[2] => hd720
)
[43] => Array
(
[0] => http://r8---sn-aigllnsk.c.docs.google.com/videoplayback?mime=video%2Fwebm&id=o-AExJcTxRDvCYsfgA1cIvQDs1v-pvLhKjTPdDh67X19vz&dur=0.000&itag=43&pl=48&ip=2a03:b0c0:1:d0::2f6:c001&sparams=dur,expire,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,nh,pl,ratebypass,source,upn&key=cms1&sver=3&expire=1437035009&upn=9lTw9Popb18&ratebypass=yes&source=youtube&lmt=1428933984759484&fexp=901816%2C9407809%2C9408142%2C9408420%2C9408710%2C9409172%2C9412774%2C9412846%2C9413149%2C9415664%2C9415958%2C9416126%2C9416370%2C9416656&ipbits=0&signature=266C126464ECDB4CC0FF076CD41F07BCC4DA7E34.08D9F13B7BF7D92FD1E1963336CC7FB8F19FE899&cms_redirect=yes&mm=31&mn=sn-aigllnsk&ms=au&mt=1437013301&mv=m&nh=IgpwcjAzLmxocjE0KgkxMjcuMC4wLjE&utmg=ytap1
[1] =>
[2] => medium
)
I can access the Title, but can't access the Link urls:
echo $title = $json->{'title'};
echo $link = $json->{'link'}->{'22'}->{'0'};
How can access the specific link array 22
This echo $title = $json->{'title'}; works because you are accessing an object's property and using -> is the correct way.
In this case $json->{'link'}->{'22'}->{'0'} you are trying to access an array item instead an object's property, because $json->{'link'}->{'22'} is an array and not an object. In this case, you should access it in this way: $json->{'link'}->{'22'}[0]. In order to avoid this kind of issues and, when you decode your JSON to a PHP object, you can pass true as a second parameter to the function json_decode and that will convert the whole object into an array. That way, you don't need to worry about accessing elements as object's attributes, you can access them, always, as array items. So, in this case, it would be: $json["link"]["22"][0].
You're confusing the way you access objects and arrays.
Getting the title is correct via $json->title, but the link should be $json->link->{'22'}[0] - a mixture of objects and arrays.
FYI the {'name'} notation is the same as name - only required when you are including variables in your object name e.g. {$someVar . 'name'}
I suppose you use json_decode() function. Do you know that you can get an array instead of StdClass Object? So, you can use.
<?php
$php_array = json_decode($json_string, true);
I'm getting stumped by simply trying to get the key/value of a certain tier inside an array I have created from an XML file. The part of the array from Print_R() is:
SimpleXMLElement Object
(
Array
(
[category] => SimpleXMLElement Object
(
[#attributes] => Array
(
[settings] => maximum
)
[cat_1] => 5.21
[cat_2] => 5.05
[cat_3] => 19.36
[cat_4] => 21.97
[cat_5] => 12.17
)
)
)
I am trying to get the "cat_1, cat_2, cat_3 .." keys so that I can put them in their own array and use them for other things. I can do print_r($array) and it works, but when I try and do this:
foreach ($array->category as $key => $val) {
$new_array[$key]= "$val";
}
$array->category doesn't seem to target that list. The "SimpleXMLElement Object" from the XML file seems to be in the way of how I normally use arrays. Does anyone know how I can get to those cat_1 ets. lists?
Notice that $array->category is an object, not an array and cat_* are properties. Since they are all public just use:
$new_array = get_object_vars($array->category);
You should convert simpleXML object to array using
$array = json_decode(json_encode((array) $simplexmlob)), 1);
Then use $array['category'] for other things. There is no need to use foreach loop.
I have an array called $results, when I use function:
print_r($results);
I get the following.
Array
(
[0] => ProfileElement Object
(
[name] => John thomson
[email] => johnt#gmail.com
[Bio] => 20 years of engineering expertise
[url] => http://twitter.com
)
)
My goal is to echo [name] [email] [Bio] [url] values separately. But when I write the following code in php I don't get any values?
echo $results[0]["ProfileElement Objects"]["Bio"];
Does anyone know why? Isn't this an array inside an array?
It appears that the array element contains an object, not another array. To access the object property, use the -> operator:
echo $results[0]->Bio;
You were close.
echo $results[0]->bio;
Is probably what you want. $result[0] is an object.
Also, depending on visibility, you may need to use a getter method.
remove ["ProfileElement Objects"]
echo $results[0]->Bio;
Try doing:
$results[0]->name;
ProfileElement Object is the object type.
It's an object inside an array. It looks like you should be able to access it as $results[0]->name, $results[0]->email, etc.
It is an object. You can get the 'Bio' value using:
echo $results[0]->Bio;