Jason Jason - 2 months ago 10
MySQL Question

php iterate through array of objects inside an array

I am trying to loop through all posts in my database and grab their id. Here is what I have so far:

$posts = $db->select("posts","*",array());

foreach ($posts as $value) {
$response['postvalue'] = $value;
}


When I run the code above I get this in my console.

enter image description here

inside those objects I have a property 'id'.

When I run this:

$posts = $db->select("posts","*",array());

foreach ($posts as $value) {
$response['postvalue'] = $value->id;
}


I get a null value:

enter image description here

What am I doing wrong?

UPDATE:

enter image description here

Answer

Update your code to add a var dump and post the data structure of the posts array. Currently it would be anyones best / lucky guess without knowing how that data looks.. this way people will be able to help you

$posts = $db->select("posts","*",array());
//var_dump($posts); 
foreach ($posts->data as $value) {
    $response['postvalue'][] = $value['id'];
}