Page 1 of 1

How to traverse an associative array

Posted: Sat Jul 18, 2009 2:47 pm
by monindra1984
When i run the given below PHP code i get the follwing error:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\LOGISTICS&MOB\trial\untitled.php on line 31

THe Code is given below

Code: Select all

 
        while ($rows[] = mysql_fetch_assoc($ManpowerResult)) { } 
        $i=0;
        print_r($rows);
        echo"-----------------------";
        foreach($rows as $arrays) {   [i][u][b] //this is the line number 31 [/b][/u][/i]               $i=0;
                foreach ($arrays as $field => $value) {
                    if($i==0)
                    { 
                        $obj[$field][] = $value;
                    }
                    else
                    {
                        $obj[$field][] = $value*$contact_rate[$field];
                    }
                        $i++;
                    } 
                    //echo "<br />";
            }
        } 
 
pls help me.. I getting stucked.. SOS


Thanking in advance.

Monindra Masia

Re: How to traverse an associative array

Posted: Sat Jul 18, 2009 3:18 pm
by McInfo
Use BBCodes when posting code.

Code: Select all

[code=php]
[/code]
This line

Code: Select all

while ($rows[] = mysql_fetch_assoc($ManpowerResult)) { }
will produce an array whose last element is false.

Edit: This post was recovered from search engine cache.

Re: How to traverse an associative array

Posted: Sat Jul 18, 2009 4:11 pm
by monindra1984
Can you tell me some alternative... what should i do??

i m clueless..

thanks with regards....

monindra masia

Re: How to traverse an associative array

Posted: Sat Jul 18, 2009 6:05 pm
by McInfo
For that line, the proper code would be

Code: Select all

$rows = array();
while ($row = mysql_fetch_assoc($ManpowerResult)) {
    $rows[] = $row;
}
I'm a little confused by the rest of your script. You'll have to explain what you are trying to do if you want more help.

Edit: This post was recovered from search engine cache.

Re: How to traverse an associative array

Posted: Sat Jul 18, 2009 11:31 pm
by Benjamin
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums. Functions in your code will be automatically linked to manual entries and your code will be syntax highlighted making it much easier for everyone to read. You will most likely receive more answers as well.

[quote="Forum Rules"]
11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.
[/quote]

You may also want to read:
[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url]
[*][url=http://php.net/]PHP Manual[/url]
[*][url=http://www.google.com/search?client=opera&rls=en&q=php+tutorials&sourceid=opera&ie=utf-8&oe=utf-8]PHP Tutorials[/url][/list]