Jump to content

foreach loop with PHP & XML (simplexml)


jenkins
Go to solution Solved by Andy-H,

Recommended Posts

Hi all,

 

I have a wsdl (xml) file that provides course number, name and textbook.

I am using foreach ($result as $course) to get courseID so that I can get the courseName and textBook for each course.

Some courses have more than one textBook so the course is listed multiple times. 

For example, if a course has 3 textBooks, it is displayed 3 times, like so:

 

CourseID   CourseName   TextBook

1101       Biology101   biology textBook title 1

1101       Biology101   biology textBook title 2

1101       Biology101   biology textBook title 3

 

 

 

I would like to list the course once and then check for multiple textBooks and display all textBooks at the same time, as follows:

 

CourseID   CourseName   TextBook

1101       Biology101   1. biology textBook title 1

                        2. biology textBook title 2

                        3. biology textBook title 3

 

I have put the information into an array which displays like this:  

 

Array
(
   [0] => 
1101 , Biology101 , biology textBook title 1
   [1] => 1101 Biology101 , biology textBook title 2
   [2] => 1101 , Biology101 , biology textBook title 3
   [3] => 1106 , Math106 , math textBook title 1
   [4] => 1106 , Math106 , math textBook title 3
)

 

What is the best way of accomplishing this?  Should I use an array to add all the information or try another approach?

 

Thanks for any suggestions.

 

Link to comment
Share on other sites

You would be better using a multi-dimensional array, as requinix said, it would be easier if we could see your source XML, but you will probably need something along the lines of:

 

$courses     = array();
$course_keys = array();
foreach($result as $course) {
   $course_key = array_search($course->id, $course_keys);
   if ( $course_key === false ) {
      $courses = array(
         'id'         => $course->id,
         'name'       => $course->name,
         'text_books' => array($course->text_book)
      );
      $course_keys[] = $course->id;
   } else {
      $courses[$course_key]['text_books'][] = $course->text_book;
   }
}

Edited by Andy-H
Link to comment
Share on other sites

That should have been:

 

$courses     = array();$course_keys = array();foreach($result as $course) {   $course_key = array_search($course->id, $course_keys);   if ( $course_key === false ) {      $courses[] = array(         'id'         => $course->id,         'name'       => $course->name,         'text_books' => array($course->text_book)      );      $course_keys[] = $course->id;   } else {      $courses[$course_key]['text_books'][] = $course->text_book;   }}

 
Missed the array brackets after $courses.
 
 
This basically checks if the course ID exists, if it does it returns the index (key) for that ID and inserts a new text book, otherwise it adds a new array into the courses multi-dimensional array (containing the course id, name and first text book) and inserts the course ID as a new entry into the course_keys array, this way the course id's in both arrays have the same key, so array search on the course keys array will also return the key for the courses array.
Link to comment
Share on other sites

Thanks for helping me Andy.
 
I've modified my code as you suggested.  My output now looks like this:
 

[0] => Array
   (
     [id] => SimpleXMLElement Object
         (
            [0] => 1101
         )

     [name] => Biology101
     [text_books] => Array
         (
            [0] => SimpleXMLElement Object
               (
                   [0] => biology textBook title 1
               )

         )
)
Link to comment
Share on other sites

Try casting the id to integer and the title to string, like so:

 

$courses     = array();$course_keys = array();foreach($result as $course) {   $course_key = array_search($course->id, $course_keys);   if ( $course_key === false ) {      $courses[] = array(         'id'         => (int)$course->id,         'name'       => $course->name,         'text_books' => array((string)$course->text_book)      );      $course_keys[] = $course->id;   } else {      $courses[$course_key]['text_books'][] = (string)$course->text_book;   }}

 

//EDIT, I've had the same problem before, I can't remember exactly how I got around it but if the casting doesn't work, try casting to array and getting element 0, i.e.

 

$id = (array)$course->id;
$id = $id[0];

Edited by Andy-H
Link to comment
Share on other sites

Thanks Andy.  It's a little better   :)

But it is still not getting the all the textbooks 

 

I'll keep working on it.  The output is now:

[0] => Array
    (
       [id] => 1101
       [name] => Biology101
       [text_books] => Array
           (
              [0] => biology textBook title 1
           )
)
Link to comment
Share on other sites

Did you add the square brackets after the $courses variable?

 

   if ( $course_key === false ) {      $courses[] = array(

 
This part?
 
Also, can you show me the output from
echo '<pre>'. print_r($course_keys, 1) .'</pre>';

Link to comment
Share on other sites

  • Solution

Ahh ok, try var_dump'ing that value that you're inserting until you get an integer (with the different casting techniques), then change all references to your $course->id variable to the working solution (the one that returns an integer).

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.