1

I have a status report database table named dsr

and it has some indexes named as nlanestatus1, nlanestatus2 ... so on and slanestatus1, slanestatus2 ... and so on ..

I ve been trying to filter these values by

$array = array_filter($dsr, function($key) {
    return strpos($key, 'lanestatus') === 0;
}, ARRAY_FILTER_USE_KEY);

I took this code from another question on stackoverflow, and understood that by changing the value of 0 to 1 in function($key), I can get the required keys, after experimentation with this code however, i found out that this function does not work. I tried changing the 3 equals to 2 equals and it just displays the whole $dsr array. Any help in php coding would be helpful.

Another method to solve it would be to change the mysql indexes but the system is already in working and changing them can break it so its not an option.

3 Answers 3

1

strpos finds the position of the key in a string. In this case, it searches for 'lanestates' in f.e. 'nlanestatus2', this position will never be 0, as 0 would be the first position.

This would work out if you want to use strpos

return strpos($key, 'lanestatus');

But in fact you won't need the position of the $key, so I would suggest using strstr

return strstr($key, 'lanestatus');
Sign up to request clarification or add additional context in comments.

1 Comment

this is also a good method, infact more simpler than the u_mulder's solution, u_mulder's one was best if you know where the position of string is ... gotta read up on strstr() thanks
1

Modify your function as:

$array = array_filter($dsr, function($key) {
    return strpos($key, 'lanestatus') !== false;
}, ARRAY_FILTER_USE_KEY);

In your case

strpos($key, 'lanestatus') === 0;

means that you check that your key starts with lanestatus. But as your keys are nlanestatus1 and slanestatus1 they never start with lanestatus.

If you're 100% sure that in front of lanestatus is one symbol you can use:

$array = array_filter($dsr, function($key) {
    // check that position of `lanestatus` starts from position 1 in `$key`
    return strpos($key, 'lanestatus') === 1;
}, ARRAY_FILTER_USE_KEY);

6 Comments

i tried both the cases, but it returns back an empty array.. !!
Here's a demo - 3v4l.org/DAFAQ Compare it with your code and data.
my php version is 7.3.7, and its not working .. I am absolutely sure that lanestatus has 1 letter s and n behind it .. but its greeting me with an empty array
I advise you to create a fiddle with data and your code. And update your question with it.
Maybe you shoud check values instead of indexes/keys? And remove ARRAY_FILTER_USE_KEY?
|
-1

How about:

$array = [];
foreach($dsr as $index) {
    if(strpos($index, 'lanestatus') !== false) {
        $array[] = $index;
    }
}

5 Comments

Keyword is index. Over what iterates your loop?
Over the $dsr array, filled with indexes. I really can't see a problem with it, for the life of me: 3v4l.org/Pgvri
ARRAY_FILTER_USE_KEY uses the keys of array, not values, Also index means key. Now it is clear?
True, but in the question it states I took this code from another question on stackoverflow which would mean it's just an attempt to solve the problem, not the actual accurate solution. We don't know the structure of array, and again - I see nothing wrong with my answer.
That can be true too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.