Jump to content

searching array.


RuleBritannia

Recommended Posts

Hello

 

If I have a multidimensional array

<?php

Array
(
    [0] => Array
        (
            [id] => 40000
            [type] => automatic
            [status] => error
            [colour] => o
        )
    [1] => Array
        (
            [id] => 50000
            [type] => automatic
            [status] => good
            [colour] => e
)
)
?>

I want to search for the status 'error', and if it exists within the array, return true(thats it(dont need keys and such to locate it))

 

I am fully aware I can just make a loop, But I want to know if its possible using a core function.

 

I have tried continuosly using functions like array_search, in_array, array_keys,array_values, And I cannot acheive it, But I am sure its possible.

 

Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/284179-searching-array/
Share on other sites

I want to know if its possible using a core function.

No

 

 

But I am sure its possible.

It's not (at least not with a single built-in function)

 

 

 . . dont need keys and such to locate it.

If you are going to go to the trouble of building something, might as well build it to be extensible.

Link to comment
https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459600
Share on other sites


<?php

function subarray_search($searchArray, $searchParam)
{
    $searchKey = key($searchParam);
    $searchValue = current($searchParam);
    foreach($searchArray as $index => $subArray)
    {
        if(isset($subArray[$searchKey]) && $subArray[$searchKey]=$searchValue)
        {
            return $index;
        }
    }
    return false;
}


$findErrorStatus = subarray_search($myArray, array('status'=>'error'))

if($findErrorStatus !== false) {
    echo "A status is in error";
} else {
    echo "None of the statses are in error";
}

?>
Link to comment
https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459602
Share on other sites

No

 

 

It's not (at least not with a single built-in function)

 

 

If you are going to go to the trouble of building something, might as well build it to be extensible.

 

Thanks for the function below, But I will continue trying for a while as I believe there maybe someway of acheiving this.

As for building something to be extensible, I want to make my code as lightweight(resourcefully and both lines of code) as possible

Link to comment
https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459623
Share on other sites

Ok So I did manage to find a core function to do this

For those interested, array_column is the function.

 

In what I was trying to acheive here, We can use it like this.

<?php

Array
(
[0] => Array
(
[id] => 40000
[type] => automatic
[status] => error
[colour] => o
)
[1] => Array
(
[id] => 50000
[type] => automatic
[status] => good
[colour] => e
)
)

$status = array_column($array,'status');

//returns

[0] = error
[1] = good

?>

With this result in a nice array, we can just use another simple function like in_array to test if the array contains a error string.

 

I believe this is a much more elegant and simple way than to use big created functions or loops. :thumb-up:

 

EDIT, 1 last thought.

 

If anybody has any other alternatives, please post, I have also found another, but its not so elegant as it consists of using 5 functions chained, But there is proberly even more ways to acheive this result.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/284179-searching-array/#findComment-1459631
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.