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

 

 

Link to comment
Share on other sites

$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);

Link to comment
Share on other sites

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

 

Array

(

    [Amos] => Array

        (

            [new] => 1

            [id] => 196

        )

 

    [Diahnne] => Array

        (

            [new] => 1

            [id] => 197

        )

 

)

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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....)

Link to comment
Share on other sites

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.