homepage Welcome to WebmasterWorld Guest from 54.211.80.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP array within a loop
breham




msg:3215963
 2:17 pm on Jan 11, 2007 (gmt 0)

Hello,

Hopefully this'll make sense to someone - I'm getting more than a little confused with it!

I have an XML parser which provides some variables.

This XML parser is within a loop so retrieves multiple occurences of the same variables.

For instance:

Pass 1:
Brett 22.99
Peter 19.99
Sarah 33.21

Pass 2:
Brett 19.99
Peter 11.99

Pass 3:
Josie 22.99
Sarah 11.99
Peter 12.99

the two variables are called $name and $amount

What I need to do is total the $amounts for each $name at the end of the loop.

What makes this trickier (for me!) is that the names are unknown until the loop has run.

I'm guessing that I need to create an array for each name and then add the values into that array. I then need to display this at the end so it will show each of the names once and their total amounts.

Hope this makes sense and someone can help!

Many thanks
Brett

 

jatar_k




msg:3216013
 2:51 pm on Jan 11, 2007 (gmt 0)

you could add something like this

initialize at the top
$namearr = array();

then this in your loop

if (!isset($namearr[$name])) { 
$namearr[$name] = $amount;
} else {
$namearr[$name] += $amount;
}

then echo at the end
echo '<pre>namearr<br>';
print_r($namearr);
echo '</pre>';

I tested it using this

<? 
$mystuff = array();
$mystuff[0] = array("Brett",22.99);
$mystuff[1] = array("Peter",19.99);
$mystuff[2] = array("Sarah",33.21);
$mystuff[3] = array("Brett",19.99);
$mystuff[4] = array("Peter",11.99);
$mystuff[5] = array("Josie",22.99);
$mystuff[6] = array("Sarah",11.99);
$mystuff[7] = array("Peter",12.99);
$namearr = array();
$counter = 0;
$name = '';
$amount = 0;
while (is_array($mystuff[$counter])) {
$name = $mystuff[$counter][0];
$amount = $mystuff[$counter][1];
echo '<br>name: ',$name,' - $amount: ',$amount;
if (!isset($namearr[$name])) {
$namearr[$name] = $amount;
} else {
$namearr[$name] += $amount;
}
$counter++;
}
echo '<pre>namearr<br>';
print_r($namearr);
echo '</pre>';
?>

breham




msg:3217274
 2:27 pm on Jan 12, 2007 (gmt 0)

Hi Jatar_k,

Thanks for replying. That worked with your script but when I put it my page it comes undone a little.

I do this bit:
$mystuff = array();
$mystuff[0] = array("Brett",22.99);
$mystuff[1] = array("Peter",19.99);
$mystuff[2] = array("Sarah",33.21);
$mystuff[3] = array("Brett",19.99);
$mystuff[4] = array("Peter",11.99);
$mystuff[5] = array("Josie",22.99);
$mystuff[6] = array("Sarah",11.99);
$mystuff[7] = array("Peter",12.99);

in a function called by a while loop and it creates the arrays. I know this because when I echo "<pre>echo print_r($mystuff[$x]);</pre>"

from within the loop it displays:

Array
(
[0] => Sarah
[1] => 33.21
) etc.

However when I do this bit (outside of the function):

$namearr = array();
$counter = 0;
$name = '';
$amount = 0;
while (is_array($mystuff[$counter])) {
$name = $mystuff[$counter][0];
$amount = $mystuff[$counter][1];
echo '<br>name: ',$name,' - $amount: ',$amount;
if (!isset($namearr[$name])) {
$namearr[$name] = $amount;
} else {
$namearr[$name] += $amount;
}
$counter++;
}
echo '<pre>namearr<br>';
print_r($namearr);
echo '</pre>';
?>

All that's displayed is:

namearr
Array
(
)

Do arrays keep their state outside of the original function?

Thanks again.
Brett

jatar_k




msg:3217278
 2:34 pm on Jan 12, 2007 (gmt 0)

you really should only need this

$namearr = array();

if (!isset($namearr[$name])) {
$namearr[$name] = $amount;
} else {
$namearr[$name] += $amount;
}

you just want the var initialized before your loop so you don't reinitialize it on every iteration, I'm sure we've all done similar before ;)

the if can go inside your loop, it is all you need to do what you want, the rest of the junk I posted was just for me to be able to construct a proper test since I don't have a parser etc.

You don't really have to create an array for this little if to work on. Whatever you already do in your parser to output your values, just add it in there somewhere.

breham




msg:3217365
 3:44 pm on Jan 12, 2007 (gmt 0)

Thanks again.

No matter what I do though - I can get the array to form within the loop but as soon as I try to display outside of the loop the array is empty?

Any ideas?

Thanks
Brett

jatar_k




msg:3217386
 3:54 pm on Jan 12, 2007 (gmt 0)

ah, just a scope problem than

how about this thread for a ton of info
Understanding Custom Functions and Variable Scope in PHP [webmasterworld.com]

breham




msg:3219252
 10:26 pm on Jan 14, 2007 (gmt 0)

Thanks again Jatar_k.

That has helped greatly! I've now managed to fill the array, pass it out of the function (thanks to your responses), order the array now the last part I'm struggling with is this (and I imagine the answer is straightforward enough - I just can't get it!):

I would like to list how many times each name has returned a result i.e.

namearr
Array
(
[Brett] => 42.98 (3 amounts)
[Peter] => 44.97 (3 amounts)
[Sarah] => 45.2 (2 amounts)
[Josie] => 22.99 (2 amounts)
)

I imagine it's something to do with the count command in PHP but I can't figure out how to get it to display correctly. Any ideas?

Thanks again
Brett

jatar_k




msg:3219734
 1:08 pm on Jan 15, 2007 (gmt 0)

you could add another dimension to your array in you if statement

if (!isset($namearr[$name])) {
$namearr[$name][0] = $amount;
$namearr[$name][1] = 1;
} else {
$namearr[$name][0] += $amount;
$namearr[$name][1]++;
}

tested, looks like it works

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved