in a document of MongoDB, i have a key/value such that value is an association array (inserted using php); how can iterate

StackOverflow https://stackoverflow.com/questions/7709726

Question

in a document of MongoDB, i have a key/value such that value is an association array (inserted using php); how can iterate throuth this array

array(4) {
  ["_id"]=>
  object(MongoId)#8 (0) {
  }
  ["tabid"]=>
  string(6) "123456"
  ["type"]=>
  string(6) "design"
  ["data"]=>
  array(2) {
    ["article"]=>
    string(57) "Bla bla"
    ["comts"]=>
    string(57) "Bla2 bla2"
  }
}

i did i var_dump to get this.

here is how what i did, but its not working

 foreach($arr['data'] as $k => $v){

    $txt+=sprintf("%s<br><i>%s</i><br><br>",$k,(string)$v); }

any help please?

Was it helpful?

Solution

why $txt+ ? + is operator for addition in PHP. $txt will be equal to a number in the end.

For concatenation two strings use .

foreach($arr['data'] as $k => $v){
    $txt .= sprintf("%s<br><i>%s</i><br><br>",$k,(string)$v); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top