Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 1 down vote accepted

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
)
share|improve this answer
    
I've looked through all the answer. TBH im not so confident in dealing with multi dimensional array since i have a DB with multiple tables(ERD) so i really need specific values to go inside specific tables. Your approach i believe is the best, flatten it out to a single array that way its much easier to parse. –  user3205047 Jan 20 at 15:54
    
A question though, hope you notice this. with this line of code. $second_slice[$arr[1]] = $arr[0]; were you assigning to arr[key] = arr[value] –  user3205047 Jan 20 at 17:13
    
@user3205047: Sure. Ask away! –  Amal Murali Jan 20 at 17:13
    
edited the question. –  user3205047 Jan 20 at 17:14
    
@user3205047: That is correct. You can do a print_r($arr); after the $arr = ... statement to see the contents of the exploded array. In this case, the first part of the string is the amount and second is the item name, so the array needs to be built as $second_slice[$arr[1]] = $arr[0];. Hope that answers your question! –  Amal Murali Jan 20 at 17:21

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

echo $first_slice[0];

share|improve this answer
    
This would actually produce an error, but thanks anyway :) –  user3205047 Jan 20 at 15:53

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
        )
)
share|improve this answer

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>";
share|improve this answer

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];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.