Return Array Elements that contain...

Posted on 2006-05-21
Last Modified: 2008-03-03
array (

[1] => 'uy87yh879sf s9d08fusdf 980dsf98 21/05/2006jidosfj89sd9 89uds9'

[2] => 'uy8/05/2006jidosfj89sd9 89uds9'

[3] => 'uy87yh879sf s9d08fusdf 980dsf98 21/089uds9'

[4] => 'uy87yh 21/05/2006jidosfj89sd9 89uds9'

[5] => '21/05/20069'

[6] => ' 21/05/2006jido'


Returns 1,5,6

That is what I would like.

How would I go about accomplishing this?
Question by:mnb93
    LVL 49

    Accepted Solution


    I think array_filter is the best way to go:

    You would define a filter function which says wether or not a certain element should be included or exclued from the resulting array:

    function myFilter($data) {
      return strstr($data, '21/05/2006')

    Then you call the array_filter function to get the filtered array:

    $keys = array_keys(array_filter($array, 'myFilter'));

    LVL 5

    Expert Comment


    $match = preg_grep('/21\/05\/2006/', $array);

    If you want the keys, you can add:

    $keys = array_keys($match);

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    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 dynamically set the form action using jQuery.
    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.

    755 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

    21 Experts available now in Live!

    Get 1:1 Help Now