简体   繁体   中英

how to do a foreach loop in php?

I have a function that has a query and a foreach loop:

$sql = "SELECT * FROM explore WHERE id = $id";
$object = $this->db->select($sql);

foreach($object as $val){
    $results = array('id'=>$val->id, 'from_id'=>$val->from_id);
    $this->result[] = $this->notify($results);  
    }

return $results;

The issue here is that if I return $object I get 2 records:

Array
(
[0] => stdClass Object
    (
        [from_id] => 6
        [id] => 3
    )

[1] => stdClass Object
    (
        [from_id] => 6
        [id] => 1
    )

)

and return $results has 1 record:

Array
(
    [id] => 1
    [from_id] => 6
)

Also if I return $this->result; , $this->result[] = $this->notify($results); does run twice but uses the same record twice returned by $results instead of using the 2 records from $object

Hope you guys can understand my issue.

ps: I am using the zend framework

Any ideas?

Edit : notify is a function in another class

$getResults isn't being set anywhere in your code, visibly. It looks like you ought to be returning $this->result instead, as that's where the results are being stored. That's my best guess given the amount of code you've given us. If you can provide more code, I can further update my answer if it doesn't work for you.

Given your comment, update your code to this:

$sql = "SELECT * FROM explore WHERE id = $id";
$object = $this->db->select($sql);

foreach($object as $val){
    $results = array('id'=>$val->id, 'from_id'=>$val->from_id);
    $this->result[] = $this->notify($results);  
    }

return $this->result;

If you're only returning $results , it'll be filled with the last item, not every item.

The reason you have a problem is that you redefine the $results array on each iteration of the foreach loop, instead of adding an element to it.

This is where the problem is:

//...
foreach($object as $val){
    $results = array('id'=>$val->id, 'from_id'=>$val->from_id);
//...        ^ you are reassigning the whole value of $results

Do this instead:

//...
$results = array();
foreach($object as $val){
    $results[] = array('id'=>$val->id, 'from_id'=>$val->from_id);
//...       ^^ note the array push instead of complete reassign

EDIT as @Cyclone has rightly pointed out, the above answer is in fact wrong. You need to be doing one of two things:

  • returning $this->result instead of $results
  • populating the $results variable with the processed data, instead of $this->result .

Essentially, you either need to change:

return $results;

to:

return $this->result;

Or, change the loop to this:

$results = array();
foreach($object as $val){
    $results[] = $this->notify(array('id'=>$val->id, 'from_id'=>$val->from_id));  
}

Which one you want to do depends on whether you actually need $this->result - ie whether you need to keep the results in the object after this code has executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM