Check value at multiple array indices in PHP

I would like to check the array of image file names to see how many consecutive images have the same orientation using PHP.

In the following example, I would like to know that indices 1 through 4 have the same orientation, or that there are four consecutive images with the same orientation in the first index.

For reference, the β€œorientation” values ​​are β€œV” for vertical and β€œH” for horizontal.

eg.

Array ( [0] => Array ( [filename] => image0.jpg [orientation] => V ) [1] => Array ( [filename] => image1.jpg [orientation] => H ) [2] => Array ( [filename] => image2.jpg [orientation] => H ) [3] => Array ( [filename] => image3.jpg [orientation] => H ) [4] => Array ( [filename] => image4.jpg [orientation] => H ) [5] => Array ( [filename] => image5.jpg [orientation] => V ) [...] [n] } 

There must be a better way than

 if ([i]['orientation'] == [i+1]['orientation']) if ([i]['orientation'] == [i+1]['orientation'] == [i+2]['orientation']) if ([i]['orientation'] == [i+1]['orientation'] == [i+2]['orientation'] == [i+3]['orientation']) if ([i]['orientation'] == [i+1]['orientation'] == [i+2]['orientation'] == [i+3]['orientation'] == [i+4]['orientation']) 
+4
source share
1 answer

If I understand the logic that you are trying to apply, SplQueue provides all the possibilities for a thorough and accurate solution to your problem.

I wrote this and it tests me perfectly based on the use case you use.

 // your data array $array = array( array("filename"=>"image0.jpg","orientation"=>"V"), array("filename"=>"image1.jpg","orientation"=>"H"), array("filename"=>"image2.jpg","orientation"=>"H"), array("filename"=>"image3.jpg","orientation"=>"H"), array("filename"=>"image4.jpg","orientation"=>"H")); function queue($array) { // grab a new SqlQueue object -- http://php.net/manual/en/class.splqueue.php $q = new SplQueue; foreach($array as $key=>$val) { // if this is the first iteration or the queue object was emptied out if ($q->isEmpty()) { $q->enqueue($val); } else { if ($val['orientation'] == $array[$key--]['orientation']) { $q->enqueue($val); if (($q->count() % 4) == 0) { return $q; } } else { // Dequeue the whole group on interrupt while ($q->valid()) { $q->dequeue(); } // ... and start filling the queue, the mismatch as the new pattern $q->enqueue($val); } } } } $q = queue($array); echo $q->count(); 

The properties of the enqueued() sets property are private, so you have to make them visible in your class.

If you are using PHP 5.4+, you can replace this index call with a reduced array by dereferencing an array of functions, for example:

  if ($val['orientation'] == prev($array[$key])['orientation'] { //... 

Everything else is pretty standard. A module test returns a queue object as soon as it receives 4 matches in a row, since SplQueue objects provide a sequence of indexed FIFOs and cannot be unchanged. Finally, if the interrupt matches before the queue has 4 matches in the row, the Split iterators empties the queue to begin with - starting with the mismatch (first new pattern.)

This should cover everything ...

Hth :)

+7
source

All Articles