Page 1 of 1
PHP & Array
Posted: Tue Aug 16, 2011 5:03 am
by circuitdouble
Hye all,
Does anyone can help a little bit to explain what this code is all about? I'm kinda confuse when it regards to array.
Thanks a lot guys.
Code: Select all
foreach ($item_array as $ia_key => $ia_value) {
$theitems = explode('->',$ia_key);
for($x = 0; $x < count($theitems); $x++) {
$item_name = $theitems[$x];
$item_total = $total_per_item[$item_name];
$in_float = $ia_value / $item_total;
$in_percent = round($in_float * 100, 2);
$alt_item = $theitems[ ($x + 1) % count($theitems)];
echo "[+] $ia_key($ia_value) / $item_name($item_total) = ". $in_float ."\r\n";
Re: PHP & MySQL: Problem in Array
Posted: Thu Aug 18, 2011 7:39 am
by phphelpme
Hi again,
I think if you simplify what you are asking for and the information you require then you stand a better chance of getting a reply because just looking at it gives people a headache buddy.
Sometimes you dont need to give all the code and its like you have your homework assignment to complete and you wants us to complete it for you. I think you need to reword it and you might get some answers buddy.
Best of luck.
Best wishes
Re: PHP & Array
Posted: Thu Aug 18, 2011 4:17 pm
by social_experiment
A brief description would be, the code loops through an array and for each item in the array a calculation of sorts is done
Re: PHP & Array
Posted: Thu Aug 18, 2011 5:44 pm
by phphelpme
hahaha you changed your code at the top didn't you. lol
Best wishes
Re: PHP & Array
Posted: Thu Aug 18, 2011 5:52 pm
by kasuals
Question:
Shouldn't $x < count($theitems) in the for be defined ahead of time? I know, I know I'm sure the script wont be run a 100,000 times in a day but still, would defining $cnt ahead of time be far more efficient than calling count()? Or does it only evaluate the 2nd condition in for during the initial execution?
If I'm wrong awesome -- I'll learn something, but reading through this post that stood out to me more than anything else. I like to know the subtly of things ahead of time.
(No I'm not an efficiency Nazi

just think people should try to be efficient in their code regardless of project size)
*edit*
PHP::for manual
In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.
Would make me think it's calling count() every iteration.
Re: PHP & Array
Posted: Fri Aug 19, 2011 10:10 am
by social_experiment
kasuals wrote:Shouldn't $x < count($theitems) in the for be defined ahead of time?
$theitems is undertermined when the script starts running but you could simplify it using count(). This is probably what you have in mind?
Code: Select all
<?php
// using count here is pointless because $theitems isn't an array yet
foreach ($item_array as $ia_key => $ia_value) {
$theitems = explode('->',$ia_key);
// here you can use it,
$total = count($theitems);
for($x = 0; $x < $total; $x++) {
$item_name = $theitems[$x];
$item_total = $total_per_item[$item_name];
$in_float = $ia_value / $item_total;
$in_percent = round($in_float * 100, 2);
$alt_item = $theitems[ ($x + 1) % count($theitems)];
echo "[+] $ia_key($ia_value) / $item_name($item_total) = ". $in_float ."\r\n";
?>
Re: PHP & Array
Posted: Fri Aug 19, 2011 2:07 pm
by kasuals
social_experiment wrote:kasuals wrote:Shouldn't $x < count($theitems) in the for be defined ahead of time?
Yeah I was just trying to help him be efficient. No sense in calling count every iteration of the for. (I wrote foreach in my first post, but the PHPDN database went down and I couldn't fix my post)
Re: PHP & Array
Posted: Fri Aug 19, 2011 2:47 pm
by social_experiment
Yeah, good point about the function be called in every iteration. There is no way of knowing how many iterations will be done and the script might be run via cron job every x minutes so every bit of time saved is probably crucial somewhere. And you're right, it is a good thing being efficient.
Lastly, i thought you were the OP but i just saw that it was another poster who started the topic

Re: PHP & Array
Posted: Fri Aug 19, 2011 2:49 pm
by kasuals
social_experiment wrote:Yeah, good point about the function be called in every iteration. There is no way of knowing how many iterations will be done and the script might be run via cron job every x minutes so every bit of time saved is probably crucial somewhere. And you're right, it is a good thing being efficient.
Lastly, i thought you were the OP but i just saw that it was another poster who started the topic

Yeah I noticed that

no problem!
Like I say, I'm no efficiency Nazi but I try to pitch in when I can.