jenkins Posted August 31, 2013 Share Posted August 31, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/ Share on other sites More sharing options...
requinix Posted August 31, 2013 Share Posted August 31, 2013 What's your source XML? It might be easier to read from that than from the array you built. Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447531 Share on other sites More sharing options...
jenkins Posted August 31, 2013 Author Share Posted August 31, 2013 Hi requinix, Thanks for your reply. I'm getting the data like this: $xml = new SimpleXMLElement($res->GetCourseInfo->any); $result = $xml->xpath('//Students'); Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447539 Share on other sites More sharing options...
Andy-H Posted August 31, 2013 Share Posted August 31, 2013 (edited) 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 August 31, 2013 by Andy-H Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447541 Share on other sites More sharing options...
Andy-H Posted August 31, 2013 Share Posted August 31, 2013 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. array_search 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. Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447547 Share on other sites More sharing options...
jenkins Posted August 31, 2013 Author Share Posted August 31, 2013 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 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447549 Share on other sites More sharing options...
Andy-H Posted August 31, 2013 Share Posted August 31, 2013 (edited) 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 August 31, 2013 by Andy-H Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447550 Share on other sites More sharing options...
jenkins Posted August 31, 2013 Author Share Posted August 31, 2013 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447557 Share on other sites More sharing options...
Andy-H Posted August 31, 2013 Share Posted August 31, 2013 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447559 Share on other sites More sharing options...
jenkins Posted August 31, 2013 Author Share Posted August 31, 2013 Yes I added the square brackets after the $courses variable. The output is: Array ( [0] => SimpleXMLElement Object ( [0] => 1101 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447563 Share on other sites More sharing options...
Solution Andy-H Posted August 31, 2013 Solution Share Posted August 31, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447564 Share on other sites More sharing options...
Barand Posted August 31, 2013 Share Posted August 31, 2013 What's your source XML? It might be easier to read from that than from the array you built. Do you have problem with posting the xml source? Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1447568 Share on other sites More sharing options...
jenkins Posted October 5, 2013 Author Share Posted October 5, 2013 Hi Andy, Apologies for the late reply. I did get this working Thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/281724-foreach-loop-with-php-xml-simplexml/#findComment-1452715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.