Need to split single array to multiple array using key word.
This is the input array.
Array
(
[0] => "handrail-18"
[1] => "handrail-17"
[2] => "handrail-16"
[3] => "safetyLine-26"
[4] => "safetyLine-25"
)
I have try this below code but still I didn’t getting proper solution
$components_nodetype = array();
foreach($components_parenttype as $item) {
if (isset($item) && $item === "safetyLine") {
$components_nodetype[] = $item;
}
}
I need output like below. This array based on handrail and safetyLine it should get separate by two array
$Array1 = Array
(
[0] => "handrail-18"
[1] => "handrail-17"
[2] => "handrail-16"
)
$Array2 = Array
(
[0] => "safetyLine-26"
[1] => "safetyLine-25"
)
2
Answers
You can solve this by iterating through the array and checking if each element contains the keyword ("handrail" or "safetyLine") by using strpos. If it does, you can add it to the corresponding output array.
It’s more proper to save then in a array aggregated by the key, instead of each an array.
And the output: