这一行正在覆盖数组

时间:2010-09-06 22:33:34

标签: php loops multidimensional-array

好吧所以我正在创建一个多元数组,这一行只允许一个元素进入它。我如何检查是否

$related[$row_r['Category_name']][$row_r['name']]

大于0,如果是这样,不要覆盖该值,只是附加到它上面

while($row_r = mysql_fetch_assoc($result)){
  $related[$row_r['Category_name']][$row_r['name']] = $row_r; //this line

2 个答案:

答案 0 :(得分:3)

也许你正在寻找这个?

while($row_r = mysql_fetch_assoc($result)){
    $related[$row_r['Category_name']][$row_r['name']][] = $row_r;
}

答案 1 :(得分:1)

$value=$related[$row_r['Category_name']][$row_r['name']];
if(is_int($value) && $value>0) {//if current value is a >0 integer
  $related[$row_r['Category_name']][$row_r['name']] = array($value, $new_value);//we combine the new value too, together with the previous one, into an array
} elseif(is_array($value)) {//if  it was already an array, we append the new element
  $related[$row_r['Category_name']][$row_r['name']][] = $new_value;
} else {//other wise (a 0 integer), we would assign the value.
  $related[$row_r['Category_name']][$row_r['name']] = $new_value;
}