Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 111

Expert Comment

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

Accepted Solution

by:
Ray Paseur earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

610 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