Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having some trouble adding to an array during a while loop and was wondering if any of you could help me. Firstly, some background. I am looping through some sql results and trying to gather the results while grouping by various ids to make it easier to deal with later. It just seems to be one line of code that isn't working. There is some code below

while($row=mysql_fetch_assoc($res)){
if(!array_key_exists($row['foreign_key_value'],$contacts)){
    $contacts[$row['foreign_key_value']]=array();
}
if(!array_key_exists($row['uid'],$contacts['foreign_key_value'])){
    $contacts[$row['foreign_key_value']][$row['uid']]=array();
}
$contacts[$row['foreign_key_value']][$row['uid']][$row['rating_id']]=$row['rating_value'];  

}

It is the last line I am having trouble with, where I am adding rating_id and rating_value. The data I am looping through is 4 fields - foreign_key_value, uid, rating_id and rating_value. The structure I want to end up with looks like

array(1) {
  [73]=>
  array(2) {
    [9]=>
    array(1) {
      [4]=>
      string(1) "3"
    }
  [1762]=>
    array(1) {
      [1]=>
      string(1) "5"
    }
  }

I just cannot get the rating_id and rating_value to create more than one key value pair in the last array, where I am expecting 5 pairs. The only thing I am getting is the last pair selected. I really have no idea why I'm not getting the data I need, can anyone help?

abc667 - you're spot on. Thank you very much. I've been staring at it so long I missed that and I'm starting to feel like an idiot now

share|improve this question
 
Should $contacts['foreign_key_value'] in your first code section, line 5 be $contacts[$row['foreign_key_value']]? –  Passerby Feb 7 at 15:02
1  
change $contacts['foreign_key_value'] to $contacts[$row['foreign_key_value']] in if(!array_key_exists($row['uid'],$contacts['foreign_key_value'])) –  abc667 Feb 7 at 15:03
 
abc667 - you're spot on. Thank you very much. I've been staring at it so long I missed that and I'm starting to feel like an idiot now –  danny slade Feb 7 at 15:11
 
passerby you're on it as well - I would vote you both up if I could –  danny slade Feb 7 at 15:15
add comment

1 Answer

You need to declare the $contacts array BEFORE the while() loop - so you can use it afterwards

$contacts = array();
while($row=mysql_fetch_assoc($res)){
if(!array_key_exists($row['foreign_key_value'],$contacts)){
    $contacts[$row['foreign_key_value']]=array();
}
if(!array_key_exists($row['uid'],$contacts['foreign_key_value'])){
    $contacts[$row['foreign_key_value']][$row['uid']]=array();
}
$contacts[$row['foreign_key_value']][$row['uid']][$row['rating_id']]=$row['rating_value'];  

}
share|improve this answer
 
The $contacts array has already been declared, i just didn't include it in the code –  danny slade Feb 7 at 15:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.