Good day. Can not catch up with how to do. An array is formed dynamically. He may be so

one.

[f_i] => Array ( [1] => Array ( [1] => fld_img [2] => page [3] => article ) [2] => 0 ) 

2

 [f_i] => Array ( [1] => 0 [2] => 0 

3

  [f_i] => Array ( [1] => 0 [2] => Array ( [1] => fld_img2 [2] => page [3] => article ) ) 

Took only two keys and 4 states. I need to convert an array to such a view.

If value == 0, then do not touch. If the value is an array, then the value is equal to the key. That is, it should work, so in the first case.

one.

 [f_i] => Array ( [1] => 1 [2] => 0 ) 

Thank you in advance.

    1 answer 1

    You can pass an array with a foreach loop and check the values ​​of its elements with the is_array function. If it is an array, replace it with a key. Suppose $ arr is your array, then the loop will look like this:

     foreach ($arr as $key => $value) { if(is_array($value)) $arr[$key] = $key; } 
    • Thank you very much, everything works. I did not expect to answer so quickly. I can't add plus yet, rating is less than 15. I forgot about is_array completely. Engaged in garbage in general. Thanks again, the beer would have put down :) - Alexander
    • Fine! I am glad to help. The author of the question may mark the answer as correct without any restrictions on rating;) - 5f0f5
    • [link] prntscr.com/c4sst9 I did the right thing? Sorry, the first time I ask questions. - Alexander
    • Yes, that's right, thank you :) - 5f0f5
    • And thank you. Very rescued, sat tupil. - Alexander