digital_priest Posted August 8, 2011 Share Posted August 8, 2011 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 Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 where does $instructor_code come from ? and why are you doing this: $info['role'] = $instructor_code; ?? Quote Link to comment Share on other sites More sharing options...
digital_priest Posted August 8, 2011 Author Share Posted August 8, 2011 $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); Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 could you please just do this: echo '<pre>'; print_r($other_instructors_test); echo '</pre>'; and post the result here so we can see the array's structure? thanks Quote Link to comment Share on other sites More sharing options...
digital_priest Posted August 8, 2011 Author Share Posted August 8, 2011 echo '<pre>'; print_r($other_instructors_test); echo '</pre>'; Array ( [Amos] => Array ( [new] => 1 [id] => 196 ) [Diahnne] => Array ( [new] => 1 [id] => 197 ) ) Quote Link to comment Share on other sites More sharing options...
digital_priest Posted August 8, 2011 Author Share Posted August 8, 2011 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 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 8, 2011 Share Posted August 8, 2011 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; Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 ...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. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 8, 2011 Share Posted August 8, 2011 ...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. Quote Link to comment Share on other sites More sharing options...
digital_priest Posted August 8, 2011 Author Share Posted August 8, 2011 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....) Quote Link to comment 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.