Yes, this has been answered countless times by using different methods, in most cases it works but not on my case, please allow me to explain.
I'm building an array from a CSV file, that was the easy part, the difficult part is, I have is that I have to build another array from the first array grouping the results based on one key value, the problem is that this value is not an array, nor a simple string...
This is a sample from the array of the CSV file
[0] => Array
(
[key_1] => FOO
[cats] => /30/
[key_2] => FTU-1
)
[1] => Array
(
[key_1] => FOO
[cats] => /30/
[key_2] => FTU-2
)
[2] => Array
(
[key_1] => FOO
[cats] => /30/10/
[key_2] => FTU-3
)
[3] => Array
(
[key_1] => FOO
[cats] => /15/
[key_2] => FTU-4
)
[4] => Array
(
[key_1] => FOO
[cats] => /10/
[key_2] => FTU-5
)
[0] => Array
(
[key_1] => FOO
[cats] => /15/
[key_2] => FTU-6
)
The final array has to look like this based on the column cats
:
[30] => Array
(
[0] => Array
(
[key_1] => FOO
[cats] => /30/
[key_2] => FTU-1
)
[1] => Array
(
[key_1] => FOO
[cats] => /30/
[key_2] => FTU-2
)
[1] => Array
(
[key_1] => FOO
[cats] => /30/10/
[key_2] => FTU-3
)
[15] => Array
(
[0] => Array
(
[key_1] => FOO
[cats] => /15/
[key_2] => FTU-4
)
[1] => Array
(
[key_1] => FOO
[cats] => /15/
[key_2] => FTU-6
)
[10] => Array
(
[0] => Array
(
[key_1] => FOO
[cats] => /30/10/
[key_2] => FTU-3
)
[1] => Array
(
[key_1] => FOO
[cats] => /10/
[key_2] => FTU-5
)
I was looking in to this This answer which is the closest to what I need, but didn't work, that's why I'm asking for help.
Update: I think I just solved it...
foreach($firstarr as $k => $v) {
$cats = array_filter(explode('/', $v['cats']));
foreach($cats as $ks=>$vs) {
if(stripos($v['cats'], $vs)){
$pp[$vs][] = $v;
}
}
}
looking good.