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
194 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 110

Expert Comment

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

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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…

734 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