There is an array

my_array ( [delimiter_classification] => Array ( [row_title] => Классификация ) [item_cat_name] => Array ( [row_title] => Тип [31] => Умные часы и браслеты [15] => Умные часы и браслеты [14] => Умные часы и браслеты [16] => Умные часы и браслеты ) [item_sub_cat_name] => Array ( [row_title] => Подтип [31] => Фитнес-браслет [15] => Умные часы [14] => Умные часы [16] => Умные часы ) ) 

You need to go through the array in this way:

  foreach($my_array as $key){ if(strpos($key_name, 'delimiter') === 0){ ... }else{ ... } } 

Only $key also an array and strpos() does not work with it. How to check this key for the presence of "delimiter"? Or some alternative.

    2 answers 2

     foreach($my_array as $key=>$value){ if(strpos($key, 'delimiter') !== false){ foreach($my_array[$key] as $inner_value){ Выводим массив под delimiter } }else{ foreach($my_array[$key] as $inner_value){ Выводим массив сравнения } } 

    option 2

     foreach(array_keys($my_array) as $key){ 
    • Thanks, but the first option does not fit, as I need to go through the $key array in a loop inside the loop. And the second option didn’t exactly understand how we’ll search for “delimiter” there - Torawhite
    • @Torawhite So you need to recursively? - rjhdby
    • @Torawhite Even better, describe the algorithm that you need to implement, because from the question I realized that you just need to start the keys of the first nesting having in the name 'delimiter' - rjhdby
    • The bottom line is this - take a look at the array above, this is a table of comparison of goods. If the name of the key is 'delimiter', then this is a common separator and I display it in one way, otherwise I loop through the array: I output the name of the series, and then I output the parameters of each product (they are indices in numbers). And so on - Torawhite
    • @Torawhite then everything is great, and the first and second options. Slightly corrected the answer, so it was clearer. - rjhdby

    Use recursion

     function doSomething($arr) { foreach ($arr as $key => $val) { if (is_array($val)) { doSomething($val); } elseif (strpos($key, 'delimiter') === 0) { // ... } else { // ... } } } doSomething($my_array); 
    • Thanks, and can I walk through the $key array in a loop? - Torawhite
    • @Torawhite $key cannot be an array - it is a key. And the keys are in php numeric and string. - Gedweb