I have some data saved in mongodb and i am fetching the rows like this
public function actionDbTheory()
{
$id = "60e16c3a8ceac239fc5d06b2";
$collection = Yii::$app->mongodb->getCollection('properties');
$current_timestamp = Yii::$app->formatter->asTimestamp(date('Y-d-m h:i:s'));
//Create Properties List Object
$properties_query = new Query;
$properties_query->select(['basic_info','property_setup','amenities','photos','pricing','finalize'])
->from('properties')
->where(['_id' => $id]);
$rows = $properties_query->one();
echo "<pre>";
//echo $rows['property_setup']['numberOfGuests'];
foreach ($rows['property_setup'] as $key => $value) {
print_r($value);
}
echo "</pre>";
}
The object from mongodb looks like this
"property_setup" : {
"stayRooms" : [
{
"value" : "",
"singleBed" : "6",
"doublebed" : "5",
"largeBed" : "5",
"extraLargeBed" : "4",
"bunkBed" : "4",
"sofaBed" : "5",
"futonMat" : "4",
"roomTypes" : "apartment",
"bathroomType" : "private",
"numberofRoomsofThisType" : "77",
"room_name" : "KIng Room",
"price_per_night" : "0"
},
{
"value" : "",
"singleBed" : "8",
"doublebed" : "7",
"largeBed" : "9",
"extraLargeBed" : "6",
"bunkBed" : "4",
"sofaBed" : "4",
"futonMat" : "2",
"roomTypes" : "family",
"bathroomType" : "private",
"numberofRoomsofThisType" : "77",
"room_name" : "Queen Room Deluxe",
"price_per_night" : "0"
}
],
"numberOfGuests" : "33",
"apartmentsSize" : "5",
"numberOfBathrooms" : "44",
"numberOfLivingRooms" : "33",
"numberOfSofaBeds" : "76",
"otherSingleBed" : "3",
"otherDoublebed" : "4",
"otherLargeBed" : "2",
"otherExtraLargeBed" : "7",
"otherBunkBed" : "9",
"otherSofaBed" : "6",
"otherFutonMat" : "6",
"userid" : "60d3a4a9cb50d874614d67d3",
"recordid" : "662626277272"
},
To read the array i am using this code
foreach ($rows['property_setup'] as $key => $value) {
print_r($value);
}
Output
Array
(
[0] => Array
(
[value] =>
[singleBed] => 6
[doublebed] => 5
[largeBed] => 5
[extraLargeBed] => 4
[bunkBed] => 4
[sofaBed] => 5
[futonMat] => 4
[roomTypes] => apartment
[bathroomType] => private
[numberofRoomsofThisType] => 77
[room_name] => KIng Room
[price_per_night] => 0
)
[1] => Array
(
[value] =>
[singleBed] => 8
[doublebed] => 7
[largeBed] => 9
[extraLargeBed] => 6
[bunkBed] => 4
[sofaBed] => 4
[futonMat] => 2
[roomTypes] => family
[bathroomType] => private
[numberofRoomsofThisType] => 77
[room_name] => Queen Room Deluxe
[price_per_night] => 0
)
)
and this displays the arrays. If i put an index print_r($value[0]);
this displays the first array. However, if i print_r($value[0]['futonMat']);
i get an illegal offset error whileas i have a key named futonMat
. How can i read the values of each array?