-2

I have this array

    Array
            (
            [0] => Array
            (
            [completed_system_products_id] => 15
            [completed_systems_id] => 9
            [step_number] => 8
            [product_id] => 230

    [1] => Array
            (
            [completed_system_products_id] => 14
            [completed_systems_id] => 9
            [step_number] => 5
            [product_id] => 127

    [2] => Array
            (
            [completed_system_products_id] => 13
            [completed_systems_id] => 9
            [step_number] => 4

How do i find the array with step_number = 4

any ideas

i tried this

$something = array_search(4, $array);

but not what i expected

Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
  • Try this function listed here: http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php – Ben Nov 22 '11 at 19:27

2 Answers2

3

It's not functionally equivalent (you'll get an array instead of a key value back), but you could use array_filter with a callback.

$itemsOfInterest = array_filter ($source, function ($elem)
{
    return ((isset ($elem ['step_number'])) && ($elem ['step_number'] == 4));
});

$itemsOfInterest should contain an array with only the elements that meet your requirements.

GordonM
  • 31,179
  • 15
  • 87
  • 129
2

You could get the ID through a function such as this one:

function GetIdWithStep4() {
    foreach ($array as $key => $value) {
        if ($value['step_number'] == 4) {
            return $key;
        }
    }
}

$something = GetIdWithStep4();
David
  • 209
  • 1
  • 6