?
Solved

PHP Array of Objects acting strange in ForEach Loop

Posted on 2013-11-13
10
Medium Priority
?
503 Views
Last Modified: 2013-12-07
I have this code

foreach($entity->licenses as $key => $license){
		print_r($license->class_list);
		foreach($license->class_list as $key2 => $class){
			echo("<br/>" . $key2 . "<br/>");
			print_r($class);
			echo("<br/>" . $license->class_list[$key2]->class_name . "<br/>");
			echo("<hr/>");
		}
		
		die();
	}

Open in new window


that is spitting out this information

Array (
[0] => classroom Object ( [id] => 3107 [teacher_id] => 2505 [class_name] => period 1 mathematics [last_modified] => [licenses] => Array ( ) [internal_school_id] => 25 [student_count:classroom:private] => 501 [teacher_name:classroom:private] => rogers, jane [teacher_username:classroom:private] => jrogers [students] => Array ( ) )

 [1] => classroom Object ( [id] => 3108 [teacher_id] => 2505 [class_name] => period 2 mathematics [last_modified] => [licenses] => Array ( ) [internal_school_id] => 29 [student_count:classroom:private] => 499 [teacher_name:classroom:private] => rogers, jane [teacher_username:classroom:private] => jrogers [students] => Array ( ) )

[2] => classroom Object ( [id] => 3109 [teacher_id] => 2506 [class_name] => period 3 math [last_modified] => [licenses] => Array ( ) [internal_school_id] => 9FFVA [student_count:classroom:private] => 500 [teacher_name:classroom:private] => beamis, tim [teacher_username:classroom:private] => tbeamis [students] => Array ( ) ) )

0
classroom Object ( [id] => 3107 [teacher_id] => 2505 [class_name] => period 1 mathematics [last_modified] => [licenses] => Array ( ) [internal_school_id] => 25 [student_count:classroom:private] => 501 [teacher_name:classroom:private] => rogers, jane [teacher_username:classroom:private] => jrogers [students] => Array ( ) )
period 1 mathematics

1
classroom Object ( [id] => 3108 [teacher_id] => 2505 [class_name] => period 2 mathematics [last_modified] => [licenses] => Array ( ) [internal_school_id] => 29 [student_count:classroom:private] => 499 [teacher_name:classroom:private] => rogers, jane [teacher_username:classroom:private] => jrogers [students] => Array ( ) )
period 2 mathematics

2
classroom Object ( [id] => 3108 [teacher_id] => 2505 [class_name] => period 2 mathematics [last_modified] => [licenses] => Array ( ) [internal_school_id] => 29 [student_count:classroom:private] => 499 [teacher_name:classroom:private] => rogers, jane [teacher_username:classroom:private] => jrogers [students] => Array ( ) )
period 2 mathematics



If you look at the first object dump it shows that there are 3 classes
Period 1 Mathematics
Period 2 Mathematics
Period 3 Math

but when I get into the second foreach loop that is iterating that class_list it is not getting to Period 3 Math, but duplication Period 2 Mathematics.

I am having one of those days where I just can't *see* what could be wrong.
0
Comment
Question by:jrm213jrm213
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39645631
Is there any way we could get this data set in something like XML or JSON so we could build a code sample that contained test data?
0
 
LVL 17

Author Comment

by:jrm213jrm213
ID: 39645672
This is the entity object json_encoded
{"id":"1428","name":"hidden","description":"hidden","date_created":"2013-09-10 13:07:05","date_removed":null,"address":"1","licenses":[{"id":"4191","licensing_entity_id":"1428","license_count":"1500","start_date":"2012-08-01 00:00:00","end_date":"2019-07-31 23:59:59","program_id":"58","grade_level_id":"7.00","grade_level":"Course1\/Sixth Grade","book_level":"6","class_list":[{"id":"3107","teacher_id":"2505","class_name":"period 1 mathematics","last_modified":"","licenses":[],"internal_school_id":"25","students":[]},{"id":"3108","teacher_id":"2505","class_name":"period 2 mathematics","last_modified":"","licenses":[],"internal_school_id":"29","students":[]},{"id":"3109","teacher_id":"2506","class_name":"period 3 math","last_modified":"","licenses":[],"internal_school_id":"9FFVA","students":[]}],"student_list":[]}],"teachers":[],"users":[],"students":[],"access_code":null,"tickets":[],"management_version":0,"default_program":"58","classes":[],"contact_name":"<hidden>","contact_email":"<hidden>","student_list":[]}

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39645752
Thanks... Back in a moment, ~Ray
0
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39645763
This works as expected for me:

$dataStr = '{"id":"1428","name":"hidden","description":"hidden","date_created":"2013-09-10 13:07:05","date_removed":null,"address":"1","licenses":[{"id":"4191","licensing_entity_id":"1428","license_count":"1500","start_date":"2012-08-01 00:00:00","end_date":"2019-07-31 23:59:59","program_id":"58","grade_level_id":"7.00","grade_level":"Course1\/Sixth Grade","book_level":"6","class_list":[{"id":"3107","teacher_id":"2505","class_name":"period 1 mathematics","last_modified":"","licenses":[],"internal_school_id":"25","students":[]},{"id":"3108","teacher_id":"2505","class_name":"period 2 mathematics","last_modified":"","licenses":[],"internal_school_id":"29","students":[]},{"id":"3109","teacher_id":"2506","class_name":"period 3 math","last_modified":"","licenses":[],"internal_school_id":"9FFVA","students":[]}],"student_list":[]}],"teachers":[],"users":[],"students":[],"access_code":null,"tickets":[],"management_version":0,"default_program":"58","classes":[],"contact_name":"<hidden>","contact_email":"<hidden>","student_list":[]}';
$entity = json_decode($dataStr); 

foreach($entity->licenses as $key => $license){
		print_r($license->class_list);
		foreach($license->class_list as $key2 => $class){
			echo("<br/>" . $key2 . "<br/>");
			print_r($class);
			echo("<br/>" . $license->class_list[$key2]->class_name . "<br/>");
			echo("<hr/>");
		}
		
		die();
	}

Open in new window

That said, if you only want the class names out, then you don't need 2 loops.

This will do it:

foreach ($entity->licenses[0]->class_list as $key => $class):
	var_dump($key);
	var_dump($class->class_name);
endforeach;

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39645776
Please see: http://www.laprbass.com/RAY_temp_jrm213jrm213.php

<?php // RAY_temp_jrm213jrm213.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
echo '<pre>';


// PROBLEM: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28293221.html#a39645672

$jso = <<<EOD
{"id":"1428","name":"hidden","description":"hidden","date_created":"2013-09-10 13:07:05","date_removed":null,"address":"1","licenses":[{"id":"4191","licensing_entity_id":"1428","license_count":"1500","start_date":"2012-08-01 00:00:00","end_date":"2019-07-31 23:59:59","program_id":"58","grade_level_id":"7.00","grade_level":"Course1\/Sixth Grade","book_level":"6","class_list":[{"id":"3107","teacher_id":"2505","class_name":"period 1 mathematics","last_modified":"","licenses":[],"internal_school_id":"25","students":[]},{"id":"3108","teacher_id":"2505","class_name":"period 2 mathematics","last_modified":"","licenses":[],"internal_school_id":"29","students":[]},{"id":"3109","teacher_id":"2506","class_name":"period 3 math","last_modified":"","licenses":[],"internal_school_id":"9FFVA","students":[]}],"student_list":[]}],"teachers":[],"users":[],"students":[],"access_code":null,"tickets":[],"management_version":0,"default_program":"58","classes":[],"contact_name":"<hidden>","contact_email":"<hidden>","student_list":[]}
EOD;

// MAKE AN OBJECT
$obj = json_decode($jso);

// ITERATE OVER THE LICENSES
foreach ($obj->licenses as $lic)
{
    // SHOW DATA FROM THE LICENSES
    $gl = $lic->grade_level;
    echo PHP_EOL . $gl;

    // ITERAATE OVER THE CLASS LISTS
    foreach ($lic->class_list as $cl)
    {
        // SHOW DATA FROM THE CLASS LISTS
        $tid = $cl->teacher_id;
        $cnm = $cl->class_name;
        echo PHP_EOL . "$tid $cnm";
    }
}
echo PHP_EOL;

// ACTIVATE THIS TO SEE THE ENTIRE OBJECT
// var_dump($obj);

Open in new window

HTH, ~Ray
0
 
LVL 17

Author Comment

by:jrm213jrm213
ID: 39645799
That is really bizarre that it works as expected for you and differently for me.
Actually what I need to do is have the a further loop around that like this
foreach($class_assignments as $assignment){
	foreach($entity->licenses as &$license2){
		foreach($license2->class_list as $class){
			if($class->get_internal_school_id() == $assignment->school_class_id){
				array_push($license2->student_list,$assignment);
			}
		}
	}
}

Open in new window



Basically I am dealing with a large file upload that specifies an updated class roster list. But before inserting those roster assignments into the database I need to make sure the new roster list will not violate their licensing seats.

But what was happening is I was getting double the student assignments because that class "Period 2 Mathematics" ends up in the class list twice somehow once you get inside that last loop... anytime before it if I print_r it, it is correct.

I only added the $keys in later because I wanted to see it was hitting all the indexes, which it is, but the last one "Period 3 Math" isn't in the array anymore, and "Period 2 Mathematics" is in the array twice.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39645892
What's the purpose of & $license2?  Or maybe a better question is, "What are you trying to get out of the JSON string?
0
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 0 total points
ID: 39657035
&$license2 is just a variable I was using to make sure there wasn't some problem with using $license. As the result I was getting was unexpected, I didn't know what causing the duplication so I was changing up everything.

I really don't know how to resolve this question, when I came today after being away for the weekend, this issue is no longer presenting. Which is disturbing in it's own right.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39657164
Yeah, I hate it when problems go away on their own.  Possibly something was buffered by the browser.  

This article might help shed some light on how foreach() works differently when processing arrays and objects.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12310-PHP-Variables-and-References.html
0
 
LVL 17

Author Closing Comment

by:jrm213jrm213
ID: 39702888
The problem just stopped happening. I don't know what caused it.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question