Solved

Iterating through an array, check if a a value and if it meets conditions continue iterating while ignoring keys with the same value

Posted on 2011-09-28
2
193 Views
Last Modified: 2012-05-12
Hi,

I have an an array, which I reversed to give the following result:

Array
(
    [0] => nameOrder=1
    [1] => nameOrder=0
    [2] => priceOrder=1
    [3] => http://lsomedomain/somecategory
)

The values I want to check for are nameOrder,priceOrder

I want to iterate through it and record (in another array) the key for the first time I find an occurence of nameOrder and priceOrder. When I find a match, I ingore any other occurences - in this case [1]. So in this example the resulting array would be

array {
    [0] => 0
    [1] => 2
}

Had a look at using array_walk but not having much luck.

Any help much appreciated!

Cheers,

Daisy
0
Comment
Question by:daisydoos
  • 2
2 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36718631
You can use foreach() to iterate over arrays.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36718716
http://www.laprbass.com/RAY_temp_daisydoos.php
<?php // RAY_temp_daisydoos.php
error_reporting(E_ALL);

// FIND THINGS IN AN ARRAY AND SHOW THE KEYS

// CREATE THE TEST DATA
$arr = array
( 'nameOrder=1'
, 'nameOrder=0'
, 'priceOrder=1'
, 'http://lsomedomain/somecategory'
)
;

// THE THINGS WE WANT TO FIND
$xyz = array
( 'nameOrder'
, 'priceOrder'
)
;

// A PLACE TO KEEP WHAT WE FIND
$out = array();

// USE THE ITERATOR TO ACCESS THE TEST DATA
foreach ($arr as $num => $thing)
{
    // USE ANOTHER ITERATOR TO ACCESS THE SEARCH STRINGS
    foreach ($xyz as $ptr => $key)
    {
        // IF WE FIND A SEARCH STRING
        if (strpos($thing, $key) !== FALSE)
        {
            // SAVE THE KEY NUMBER, AND STOP LOOKING FOR THIS SEARCH STRING
            $out[] = $num;
            unset($xyz[$ptr]);
            if (empty($xyz)) break;
        }
    }
}

// SHOW THE WORK PRODUCT
echo "<pre>";
print_r($arr);
echo PHP_EOL;
print_r($out);

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question