ementalis Posted March 13, 2013 Share Posted March 13, 2013 Is it posible to make a foreach loop and use where something equals somemething condition in PHP? E.g. I have a code like: <?php foreach($parents as $parent) : ?> echo $parent->title; <?php endforeach; ?> And I would like to have something like: <?php foreach($parents as $parent, where $parent->type = 'submenu1') : ?> echo $parent->title; <?php endforeach; ?> So, it will filter stuff on the fly. The reason behind it is that I can do only one mysql query and grab all submenus stuff and then in php code loop just one object instead of e.g. 20 or more. how to do such thing and implement where filter in foreach loop? Quote Link to comment Share on other sites More sharing options...
haku Posted March 13, 2013 Share Posted March 13, 2013 <?php foreach($parents as $parent) { if($parent->type == 'submenu1') { // do something } } ?> Quote Link to comment Share on other sites More sharing options...
yomanny Posted March 13, 2013 Share Posted March 13, 2013 (edited) EDIT: oops, haku is fast, what he said. - W Edited March 13, 2013 by yomanny Quote Link to comment Share on other sites More sharing options...
ementalis Posted March 13, 2013 Author Share Posted March 13, 2013 Thanks, but it's ugly and messy I need something more "advanced". But thanks anyway. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 13, 2013 Share Posted March 13, 2013 If the original parents array is the result of a SQL query, do the filtering in the query Quote Link to comment Share on other sites More sharing options...
ementalis Posted March 13, 2013 Author Share Posted March 13, 2013 (edited) If the original parents array is the result of a SQL query, do the filtering in the query I cannot do 20 or 50 queries and send them to view. thats crazy I can of ourse do the if condition inside foreach loop but I prefer some more elegant and playfuly-clever solution Edited March 13, 2013 by ementalis Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 13, 2013 Share Posted March 13, 2013 What? Quote Link to comment Share on other sites More sharing options...
kicken Posted March 14, 2013 Share Posted March 14, 2013 (edited) I can of ourse do the if condition inside foreach loop but I prefer some more elegant and playfuly-clever solution <?php class ConditionalIterator implements Iterator { private $mList; private $mKeys; private $mKeyIdx; private $mFieldToCheck; private $mFieldValue; public function __construct($list, $field, $value){ $this->mList = $list; $this->mKeys = array_keys($list); $this->mFieldToCheck = $field; $this->mFieldValue = $value; $this->rewind(); } private function getNextMatchingKeyIndex($s){ for (; $s<count($this->mKeys); $s++){ $k = $this->mKeys[$s]; $v = $this->mList[$k]; if (is_array($v) && isset($v[$this->mFieldToCheck])){ if ($v[$this->mFieldToCheck] == $this->mFieldValue){ return $s; } } else if (is_object($v) && isset($v->{$this->mFieldToCheck})){ if ($v->{$this->mFieldToCheck} == $this->mFieldValue){ return $s; } } } return false; } public function rewind(){ $this->mKeyIdx = $this->getNextMatchingKeyIndex(0); } public function current(){ $key = $this->key(); return $this->mList[$key]; } public function key(){ return $this->mKeys[$this->mKeyIdx]; } public function next(){ $this->mKeyIdx = $this->getNextMatchingKeyIndex($this->mKeyIdx+1); } public function valid(){ return $this->mKeyIdx !== false; } } $a = new stdclass(); $a->type = 'a'; $a->name = 'one'; $b = new stdclass(); $b->type = 'b'; $b->name = 'two'; $c = new stdclass(); $c->type = 'c'; $c->name = 'three'; $ap = new stdclass(); $ap->type = 'a'; $ap->name = 'four'; $bp = new stdclass(); $bp->type = 'b'; $bp->name = 'five'; $cp = new stdclass(); $cp->type = 'c'; $cp->name = 'six'; $list = array($a, $b, $c, $ap, $bp, $cp); foreach (new ConditionalIterator($list, 'type', 'b') as $k=>$v){ var_dump($k, $v); } echo 'Done!'; There ya go. Complex, hard to understand, and in complete violation of the KISS principal, just like you ordered. Edited March 14, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.