1

I have a problem that's born of being a dyed in the wool procedural programmer who's here forced into using some OOP constructs in order to make use of a library I need to use. I am stuck unable to access variables- other than print them out. Let me illustrate:

foreach($html->find('span[class="given-name"]') as $e)
    echo $e->innertext . '<br>';

foreach($html->find('span[class="family-name"]') as $e)
    echo $e->innertext . '<br>';

The above will print out a long list of first names, followed by a long list of surnames. However I want to be able to access them together. For example I want to be able to say something like $myarray["firstname"] = (*whatever is in $e->innertext*) and also $myarray["surnamename"] = (*whatever is in the next $e->innertext*)

When I try the seemingly obvious:

$x = $e->innertext;

it crashes. I assume that's because I am passing a pointer to $x instead of a value, but how on earth do I tell it I want the value - in this case a part of a person's name, to be assigned to $x, or my array, or whatever the variable might be?

I am almost a complete neophyte when it comes to OOP concepts and constructs, so please bear that in mind. Thank you for your assistance!

13
  • 1
    Are you saying you want to take each name and store it in an array as you go through the foreach loop? What is the rest of the code when you do $x=$e->innertext;? Is it just preceded by foreach($html->find('span[class="family-name"]') as $e)? Thanks Commented Jun 17, 2014 at 17:49
  • 1
    Can you provide the HTML? Commented Jun 17, 2014 at 17:50
  • innerText is just a string, there's nothing special about it. Commented Jun 17, 2014 at 17:52
  • There is nothing after the $x=$e->innertext; because adding that line made it crash. All the rest of it is is a long list of foreach/echo's on lots of related data items, like address, town, state, country etc. There is no HTML to speak of. Commented Jun 17, 2014 at 17:53
  • What was the error message when it crashed? Commented Jun 17, 2014 at 17:53

1 Answer 1

3

If your document is well structured and in order, this should do the trick:

$name=array();
$surname=array();

foreach($html->find('span[class="given-name"]') as $e){
    $name[]=$e->innertext;
}

foreach($html->find('span[class="family-name"]') as $e){
    $surname[]=$e->innertext;
}

foreach($name as $key=>$value){
  echo $name[$key] . " " . $surname[$key] . "<br>";
}
Sign up to request clarification or add additional context in comments.

8 Comments

Please put braces around your loop bodies, don't promote bad programming style.
I guess we should consider the case #surnames != #names as well ?
Huummm. I didnt follow you birgire. Why we need to check that?
I'm just wondering about the case when count( $name ) is not the same as count( $surname ).
Now I see what you mean. If so, check if they are equal will not fix it. If the elements are not even we will mismatch the names and surnames. Thats why I wrote the solution only will work if the document is well structured.
|

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.