0

i'm using the php simple dom parser and found a little issue:

when the selector returns only one element, i also need to run a foreach loop for it. is there a simpler way to do it?

thanks

4
  • like in jQuery - when u get only one element you can directly access it by default. Commented Apr 9, 2011 at 11:02
  • You need to post your use case with code if we are to be any help to you Commented Apr 9, 2011 at 11:02
  • all jQuery functions work on a multiset, meaning they don't care if it's one element or every single dom element. Give us a test case of what it is you are trying to achieve. Commented Apr 9, 2011 at 11:03
  • very simple: <div class=widget><input type=text value=bla></div> my code is: $txt = $ctl->find("input"); it's only one element but i can't access it like in jQuery with: echo $txt->value .. so i need to run a foreach loop everytime? Commented Apr 9, 2011 at 11:06

1 Answer 1

2

The manual says find() has a second parameter for exactly that:

mixed find ( string $selector [, int $index] )

Find elements by the CSS selector. Returns the Nth element object if index is set, otherwise return an array of object.

So in your case,

$txt = $ctl->find("input", 0); 

should do the job.

SimpleHTMLDOM API reference

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.