0

I am using simple html dom parser. I want to put an array in my $ret['ingredients'] array.

This is the HTML code I want to parse

<div class="ingredients">
<p>1 dl vatten</p>
<p>30 g smör</p>
<p>¾ dl vetemjöl</p>
<p>1 stort ägg</p>
</div>

and want the result look like this

Array 
( 
    [ingredients] => Array 
        ( 
            [0] => '1 dl vatten' 
            [1] => '30 g smör'
            [2] => '¾ dl vetemjöl'
            [3] => '1 stort ägg' 
        ) 
) 

But when I try this code

foreach($html->find('div[class="ingredients"] p') as $element) {
    $ret['ingredients'] = array($element->innertext);
        }

I get this result

Array 
( 
    [ingredients] => Array 
        ( 
            [0] => '1 dl vatten 30 g smör ¾ dl vetemjöl 1 stort ägg' 
        ) 
) 

1 Answer 1

1

Should be rather:

$ret['ingredients'] = array();
foreach($html->find('div[class="ingredients"] p') as $element) {
   $ret['ingredients'][] = $element->innertext;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.