Re: [PHP] Array help.

[email protected] (Joshua Kehn)
Newsgroups php.general
Message-ID <[email protected]>
On Jul 30, 2010, at 2:36 PM, Paul Halliday wrote:

> I have a query that may not always return a result for a value, I need
> to reflect this with a "0". I am trying to overcome this by doing this
> (the keys are ID's):
> 
> while ($row = mysql_fetch_row($statusQuery)) {
> 
>        $cat = array(0=>0,1=>0,11=>0,12=>0,13=>0,14=>0,15=>0,16=>0,17=>0,19=>0);
> 
>        switch ($row[1]) {
>            case 0: $cat[0] = $row[0]; break;
>            case 1: $cat[1] = $row[0]; break;
>            case 11: $cat[11] = $row[0]; break;
>            case 12: $cat[12] = $row[0]; break;
>            case 13: $cat[13] = $row[0]; break;
>            case 14: $cat[14] = $row[0]; break;
>            case 15: $cat[15] = $row[0]; break;
>            case 16: $cat[16] = $row[0]; break;
>            case 17: $cat[17] = $row[0]; break;
>            case 19: $cat[19] = $row[0]; break;
>        }
> 
>        print_r($cat);
>    }
> 
> Which gives me this:
> 
> Array ( [0] => 15547 [1] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0
> [15] => 0 [16] => 0 [17] => 0 [19] => 0 )
> Array ( [0] => 0 [1] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15]s
> => 30 [16] => 0 [17] => 0 [19] => 0 )
> 
> The query only return 2 rows:
> 
> 15 | 30
> 0 | 15547
> 
> What am I doing wrong? Is there a more elegant way to achieve what I want?
> 
> Thanks.
> 
> -- 
> Paul Halliday
> Ideation | Individualization | Learner | Achiever | Analytical
> http://www.pintumbler.org
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

Paul-

Why not say:

$cat = array();
if(isset($row[1])
{
    $cat[$row[1]] = $row[0];
}

print_r($cat);

Regards,

-Josh
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.