Add elements to an array inside a foreach loop
maybe I am missing something, but why do you even need to have two different arrays $i and $w? second thing, $file_names array should go outside of the foreach loop since this array doesn't depend on the iteration of the first foreach loop.

I am not sure about the exact output which you expect but it seems that you can only iterate through the file names array and when you need to update this line 

'value' =&gt; "" . $ws . ""  , you could just look up the value of $ws in the particular array rather than iterating the entire array every time.