How to traverse an associative array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
monindra1984
Forum Newbie
Posts: 14
Joined: Wed Mar 18, 2009 10:35 am

How to traverse an associative array

Post 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
Last edited by Benjamin on Sat Jul 18, 2009 11:30 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to traverse an associative array

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 3:24 pm, edited 1 time in total.
monindra1984
Forum Newbie
Posts: 14
Joined: Wed Mar 18, 2009 10:35 am

Re: How to traverse an associative array

Post by monindra1984 »

Can you tell me some alternative... what should i do??

i m clueless..

thanks with regards....

monindra masia
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to traverse an associative array

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 3:24 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to traverse an associative array

Post 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]
Post Reply