'How to get an Array value inside an object in an array Php [duplicate]
I have this array below at the last code part. To get ["db"] and ["size"] object in array through a loop I use
<?php
foreach ($result->cpanelresult->data as $value){
<b>DB Name:</b> <?php echo $value->db; ?> | <b>Size:</b> <?php echo $value->size; ?>
}
?>
It works as expected, but the problem is I am trying to access user string in userlist array but its giving me difficulty, I tried this
<?php echo $value->userlist->user; ?>
It did not work, I get error Trying to get property 'user' of non-object in C:\xampp\htdocs\auth\ajax.php on line 582 because userlist is an Array. Please what am I doing wrong.
object(stdClass)#65 (1) {
["cpanelresult"]=> object(stdClass)#4 (7) {
["preevent"]=> object(stdClass)#2 (1) {
["result"]=> int(1)
}
["module"]=> string(7) "MysqlFE"
["data"]=> array(29) {
[0]=> object(stdClass)#5 (5) {
["db"]=> string(20) "saska_example"
["size"]=> string(4) "8192"
["userlist"]=> array(1) {
[0]=> object(stdClass)#6 (2) {
["user"]=> string(20) "saska_example"
["db"]=> string(20) "saska_example"
}
}
["usercount"]=> int(1)
["sizemeg"]=> string(4) "0.01"
}
[1]=> object(stdClass)#7 (5) {
["size"]=> string(4) "8192"
["userlist"]=> array(1) {
[0]=> object(stdClass)#8 (2) {
["db"]=> string(13) "saska_exampleone"
["user"]=> string(13) "saska_exampleone"
}
}
["usercount"]=> int(1)
["sizemeg"]=> string(4) "0.01"
["db"]=> string(13) "saska_exampleone"
}
[2]=> object(stdClass)#10 (5) {
["userlist"]=> array(1) {
[0]=> object(stdClass)#9 (2) {
["db"]=> string(16) "saska_exampletwo"
["user"]=> string(16) "saska_exampletwo"
}
}
["size"]=> string(4) "8192"
["usercount"]=> int(1)
["sizemeg"]=> string(4) "0.01"
["db"]=> string(16) "saska_exampletwo"
}
[3]=> object(stdClass)#11 (5) {
["db"]=> string(13) "saska_demo"
["sizemeg"]=> string(4) "5.63"
["size"]=> string(7) "5906432"
["userlist"]=> array(1) {
[0]=> object(stdClass)#12 (2) {
["user"]=> string(13) "saska_root"
["db"]=> string(13) "saska_demo"
}
}
["usercount"]=> int(1)
}
["event"]=> object(stdClass)#63 (1) {
["result"]=> int(1)
}
["func"]=> string(7) "listdbs"
["apiversion"]=> int(2)
["postevent"]=> object(stdClass)#64 (1) {
["result"]=> int(1)
}
Solution 1:[1]
So I got the answer from u_mulder in the comment section as
<?php echo $value->userlist[0]->user; ?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shasha |