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
188 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 108

Expert Comment

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now