I have this code:
$sample = '5ml milk, 5ml water, 3pcs carrots';
echo $sample."</br>";
$first_slice = explode(',',$sample);
foreach($first_slice as $key => $value)
{
$second_slice[$key] = explode(' ',$value);
}
print_r($second_slice);
It does what i want, i need to separate 5ml milk from 5ml water and 3pcs carrots from there i need to separate again 5ml from milk.
My question is, how do i select/echo only 5ml and milk as a single data.
The above code produces the result:
Array ( [0] => Array ( [0] => 5ml [1] => milk ) [1] => Array ( [0] => [1] => 5ml [2] => water ) [2] => Array ( [0] => [1] => 3pcs [2] => carrots ) )
Im quite confused on how do i select/echo since i exploded it twice means its an array inside an array. Please correct me if my understanding is wrong.
Additional question: I have to explode it thrice. Since my table has name,quantity,unit columns. name for the ingredient name, quantity for the quantity, unit for the unit of measurement.
respectively [milk] [5] [ml]
I have done the same to make another array for separating measurement and quantity. But it cancels out the redundancy.
foreach($second_slice as $key=>$value)
{
$arr = explode('-',$value);
$third_slice[$arr[1]] = $arr[0];
}
The output is:
Array ( [ml] => 5 [pcs] => 3 )
There are 2 5ml's on the string. How do i not avoid it being taken out? since they are separate ingredients.