1

I have a multi-line array value setup in existing code (written by an former team member) as follows:

$array1['Alpha'] = "New York,Los Angeles,Washington";
$array1['Beta'] = "New York,Los Angeles,Honolulu";
$array1['Gamma'] = "New York,Los Angeles,Washington";
....
....
and so on....

My need is that I have the value of a city (say "Washington"), and I need to get all the array index values which have Washington in their CSV list. So in the above case, the answer would be "Alpha" and "Gamma" (but not "Beta" as it does not contain Washington)

I tried array_search function, like

echo array_search("Washington",$array1);

but it is not yielding the desired result.

What am I missing here please?

Also tried

$cittskarray = explode(',', $array1["Washington"]);

foreach ($array1 as $cityvalue)
{
 ;//search one by one
}

but that is lengthy method and does deep search.

3
  • "What am I missing here please?" - how array_search actually works ...? Quote manual: "Searches the array for a given value" - but none of your array values actually is Washington Commented Jan 19, 2022 at 13:37
  • @CBroe - got it. I was also trying explode function on each array value, but the code was getting lengthier and complex. So looking for an efficient solution. Additional question I had is what really is this multi-line array? Is it associative hash, or what as per PHP standard? Commented Jan 19, 2022 at 14:08
  • 1
    It is a normal, flat array. You don't have more than one dimension here to begin with. All of your values are strings. Commented Jan 19, 2022 at 14:35

3 Answers 3

2

You can use foreach and check as:

  1. You can convert comma separated string to array, and check using in_aray
  2. if that value found, then you can fill in new output array.

See below example

$array1['Alpha']="New York,Los Angeles,Washington";
$array1['Beta']="New York,Los Angeles,Honolulu";
$array1['Gamma']="New York,Los Angeles,Washington";

$returnData = [];
foreach ($array1 as $k => $val)
{
 $datas = explode(",",$val);
    if(in_array('Washington', $datas) ){
        $returnData[] = $k;
    }
}

print_r($returnData);

Output will be: [0] => Alpha [1] => Gamma

Sign up to request clarification or add additional context in comments.

Comments

2

It's because array_search looks for full match.

Simply loop array and check existence of search string:

$array1['Alpha']="New York,Los Angeles,Washington";
$array1['Beta']="New York,Los Angeles,Honolulu";
$array1['Gamma']="New York,Los Angeles,Washington";

$keyword = 'Washington';

foreach ($array1 as $key => $cities)
{
   if (str_contains($cities, $keyword)) {
        echo $key . "\n";
   }
}

Example

3 Comments

Thanks @Justinas, This only works on PHP versions which are at least 8.0.0. My server is unfortunately lower
@Aquaholic So replace str_contains with stripos($cities, $keyword) !== false
Yes. That works. Thanks (+1)
2

I don't know if it's the most efficient way, but you could use array_filter() (doc) to acheive this.

Example :

<?php
$array['Alpha']="New York,Los Angeles,Washington";
$array['Beta']="New York,Los Angeles,Honolulu";
$array['Gamma']="New York,Los Angeles,Washington";
$search = "Washington";

$result = array_keys(array_filter($array, function($a) use ($search) {
    return strpos($a, $search) > -1;
}));

print_r($result);

Where the function is an anonymous function (so could be changed), will display:

Array
(
    [0] => Alpha
    [1] => Gamma
)

1 Comment

Thanks @Frankich. Your code works well (+1), and the one by DevsiOdedra is comparatively more clear and readable.

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.