Jump to content

foreach on 2-D array, seems simple, not working


digital_priest

Recommended Posts

I'm feeling very thick because I should know why this isn't working, but I've been staring at this for too long—so I'm looking for help.

 

I'm trying to add a key/value to the second set of arrays in a 2D array. It looks like it works within the foreach loop, but then doesn't show up later.

 

Here's the PHP code:

echo("OTHER_INSTRUCTORS_TEST IS... <br />");
print_r($other_instructors_test);
foreach ($other_instructors_test AS $instructor => $info) {
echo("<p>INSTRUCTOR: ");
print_r($instructor);
echo("<br />INFO: ");
print_r($info);
echo("<br />ROLE CODE: ");
print_r($instructor_code);

$info['role'] = $instructor_code;
echo("<br />INFO IS NOW: ");
print_r($info);
}
echo("<p>OTHER_INSTRUCTORS_TEST (POST ROLE CODES): <br />");
print_r($other_instructors_test);

 

 

 

And here's the results:

OTHER_INSTRUCTORS_TEST IS...

Array ( [Amos] => Array ( [new] => 1 [id] => 187 ) [Diahnne] => Array ( [new] => 1 [id] => 188 ) )

INSTRUCTOR: Amos

INFO: Array ( [new] => 1 [id] => 187 )

ROLE CODE: 2

INFO IS NOW: Array ( [new] => 1 [id] => 187 [role] => 2 )

 

INSTRUCTOR: Diahnne

INFO: Array ( [new] => 1 [id] => 188 )

ROLE CODE: 2

INFO IS NOW: Array ( [new] => 1 [id] => 188 [role] => 2 )

 

OTHER_INSTRUCTORS_TEST (POST ROLE CODES):

Array ( [Amos] => Array ( [new] => 1 [id] => 187 ) [Diahnne] => Array ( [new] => 1 [id] => 188 ) )

 

Why isn't the ['role'] value showing up in the $other_instructors_test array?

 

Thanks for your help,

Amos

 

 

$instructor_code comes from a function that finds ID codes from a table of role names (head instructor, instructor, volunteer, etc.). The function call actually appears in the previous line of code.

 

$instructor_code = get_role_code('instructor');
echo("OTHER_INSTRUCTORS_TEST IS... <br />");
print_r($other_instructors_test);
foreach ($other_instructors_test AS $instructor => $info) {
	echo("<p>INSTRUCTOR: ");
	print_r($instructor);
	echo("<br />INFO: ");
	print_r($info);
	echo("<br />ROLE CODE: ");
	print_r($instructor_code);

	$info['role'] = $instructor_code;
	echo("<br />INFO IS NOW: ");
	print_r($info);
}
echo("<p>OTHER_INSTRUCTORS_TEST (POST ROLE CODES): <br />");
print_r($other_instructors_test);

	
echo '<pre>';
print_r($other_instructors_test);
echo '</pre>';

 

Array

(

    [Amos] => Array

        (

            [new] => 1

            [id] => 196

        )

 

    [Diahnne] => Array

        (

            [new] => 1

            [id] => 197

        )

 

)

 

And I'm doing this:

$info['role'] = $instructor_code;

to add the key=>value to the array represented by $info in:

foreach($other_instructors_test AS $instructor => $info)

Adding the role code to the $other_instructors_test array is the real goal, all the printing is just attempts to debug

 

You can't change the array using $info unless you reference it.  Try this (notice the &):

foreach($other_instructors_test AS $instructor => &$info)

Or an alternate way without using the reference.

 

Instead of:

$info['role'] = $instructor_code;

Do this:

$other_instructors_test[$instructor]['role'] =  $instructor_code;

...and both the arrays come from a database right?

 

You can probably build the entire array from a single mysql query, using a JOIN, eliminating the need for all this, but I don't have enough information about your database tables to give you an example.

...and both the arrays come from a database right?

 

You can probably build the entire array from a single mysql query, using a JOIN, eliminating the need for all this, but I don't have enough information about your database tables to give you an example.

 

I agree.  There is no reason to loop through an array and add one static value to each row.  You can just do this in your query, either with a join or a pseudo-column with a value.

Oh, yea! Don't know why I didn't think of that.

 

I was using the same function for all of the variables, some of which are names of employees with roles, but some are names of locations or contacts, etc. A second function was finding the ID codes for the roles. That should have jumped out at me.

 

So I've added a third argument to the function, $role, and use it to create a joined table when it has an appropriate value.

 

Works like a charm!

 

Thanks!

(still don't know why that didn't work, though, which bugs me, but I've got other things to do than worry over code I'm not using....)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.