Question

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.

Était-ce utile?

La solution

Currently, you have a multi-dimensional array. You can simpify this approach and flatten your array to one dimension. I suggest you modify your foreach loop as below:

foreach($first_slice as $key => $value)
{
    $arr = explode(' ', trim($value));
    $second_slice[$arr[1]] = $arr[0];
}

print_r($second_slice);

Produces the output:

Array
(
    [milk] => 5ml
    [water] => 5ml
    [carrots] => 3pcs
)

Now, to get the quantity for any item, you can directly do echo $second_slice['item_name'].

And if you wanted to find the name of the quantity from the amount, you can use array_search():

$searchFor = '3pcs';
echo array_search($searchFor, $second_slice);

Outputs:

carrots

If there are multiple quantities with the same amount, the above method only returns the first. If you want to get all of them, you can use array_filter():

$result = array_filter($second_slice, function($elem) use ($searchFor){
  return ($elem == $searchFor);
});

print_r($result);

Output:

Array
(
    [milk] => 5ml
    [water] => 5ml
)

Autres conseils

You just want to output 5ml milk? Because if so, simply do the following:

echo $first_slice[0];

There is a hidden whitespace problem, but you can easily fix it with the following code by using trim:

$sample = '5ml milk, 5ml water, 3pcs carrots';

echo $sample."</br>";

$first_slice = explode(',',$sample);

foreach($first_slice as $key => $value)
{
    $second_slice[$key] = explode(' ',trim($value));
}

print_r($second_slice);

which produces the following array:

Array
(
    [0] => Array
        (
            [0] => 5ml
            [1] => milk
        )
    [1] => Array
        (
            [0] => 5ml
            [1] => water
        )
    [2] => Array
        (
            [0] => 3pcs
            [1] => carrots
        )
)

there are a few ways you can achieve this. Here is one way:

$sample = '5ml milk, 5ml water, 3pcs carrots';

$items = explode(",", $sample);

$final = array();

foreach($items as $i){
    $value = explode(" ", trim($i));
    $final[$value[1]] = $value[0];  
}

echo "<pre>";
print_r($final);
echo "</pre>";

If you want to echo "5ml milk":

echo $second_slice[0][0] . " " . $second_slice[0][1];

echo "5ml water";

echo $second_slice[1][0] . " " . $second_slice[1][1];

echo "3pcs carrots";

echo $second_slice[2][0] . " " . $second_slice[2][1];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top