1

I am creating an array, named $subArray. This array has to be filled with $child->getName() => $child, gotten from a XML document.

$subArray = array(
  foreach($person->children() as $child){
    $child->getName() => $child;
  }
);

But this doesn't seem to work, as I cannot use the => when using array_push, and I cannot use a foreach loop inside an array. How do I solve this?

This is my whole function:

function get_xml_arr($xmlURL){

  $xml=simplexml_load_file($xmlURL) or die ("XML not found");

  $array = array();

  foreach($xml->person as $person){
    $subArray = array(
      foreach($person->children() as $child){
        $child->getName() => $child;
      }
    );
    array_push($array, $subArray);
  }

  return $array;

}

1 Answer 1

2

you can use $child->getName() as index of array in such way

$subArray = array();
foreach($xml->person as $person) {
   foreach($person->children() as $child) 
      $subArray[$child->getName()] = $child;
   $array[] = $subArray;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, very nice! Thank you very much!
don't mention it. Good luck!

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.