?
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
?
196 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

765 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