teoo315 0 Posted July 14, 2012 Report Share Posted July 14, 2012 Hi everybody,it's been a couple of days that i'm trying to show the values inside a multidimensional array but unsuccessfully. This is my code: $i=0;$countLow=0;$fissoLow=0;for ($i=0;$i<10; $i++){ $z+=2; $fissoLow = 10*2*$z; $countLow = $fissoLow*$z; $res=array("fissi" => array($fissoLow),"conteggi" => array($countLow)); }for ($j=0; $j<count($res); $j++){ echo $res['fissi'][$j] . " " . $res['conteggi'][$j] . "<br />"; } And this is the (wrong) result: 400 8000 This should be the last row of $res array, not the only one. Waiting your replies Quote Link to post Share on other sites
boen_robot 107 Posted July 14, 2012 Report Share Posted July 14, 2012 At the line $res=array("fissi" => array($fissoLow),"conteggi" => array($countLow)); you're replacing the whole $res array, so $res will always have just one entry. Replace that with $res[]=array("fissi" => array($fissoLow),"conteggi" => array($countLow)); to append the new array onto $res. BTW, you'd also have to define $res first, like $i=0;$countLow=0;$fissoLow=0;$res = array();for ($i=0;$i<10; $i++){ Quote Link to post Share on other sites
teoo315 0 Posted July 15, 2012 Author Report Share Posted July 15, 2012 (edited) It works the first part But i can't show the values. What's the right syntax? Sorry but i'm a newbie with php and programming. Again, if i want to find the max value of array "conteggi", what's the right way? I'm trying with this: $max = max($res["conteggi"]);echo $max; But doesn't work: Warning: max() : When only one parameter is given, it must be an array in on line 13 Edited July 15, 2012 by teoo315 Quote Link to post Share on other sites
Ingolme 1,035 Posted July 15, 2012 Report Share Posted July 15, 2012 You need to access it like this: $res[$index]['conteggi'] where $index is a number indicating one of the elements in the list. Quote Link to post Share on other sites
teoo315 0 Posted July 15, 2012 Author Report Share Posted July 15, 2012 Ok great!I fixed for cycle and now it look like this: for ($i=0;$i<count($res); $i++){$max = max($res[$i]['contegg']);echo $max . " ";} Output: 80 320 720 1280 2000 2880 3920 5120 6480 8000 Notation: I inserted "echo $max" inside for cycle, only to see the change of values. I think it should be correct. Is not? And now last but not least for cycle to show $res 2D array: for ($j=0; $j<count($res); $j++){echo $res['fiss'][$j] . " " . $res[$j]['contegg'] . "<br />";} Output: ArrayArrayArrayArrayArrayArrayArrayArrayArrayArray Quote Link to post Share on other sites
Ingolme 1,035 Posted July 15, 2012 Report Share Posted July 15, 2012 This is, actually, a three dimensional array because ['contegg'] anf ['fiss'] are both arrays. ['fiss'] isn't an index of $res but of $res[$j]. $res[$j]['fiss'][0] . ' ' . $res[$j]['contegg'][0] . '<br />'; Quote Link to post Share on other sites
teoo315 0 Posted July 15, 2012 Author Report Share Posted July 15, 2012 Yeah you're right. Now everything seems working fine. One last thing, do you confirm that the process to find max of $res is suitable? Thx man Quote Link to post Share on other sites
boen_robot 107 Posted July 15, 2012 Report Share Posted July 15, 2012 If it works, it's "suitable". Is it "efficient" is a separate concern. It isn't really.There are two ways to make it more efficient - collect all values you wish to find the maximum of ("candidate values") in a single array, and apply max() only once on that array OR assume the first candidate value to be the maximum one, then loop over the other candidate values, and whichever is greater, take it up as the new maximum. By the end of the loop, you'll have the greatest value.I'm not exactly sure what's the set of candidate values you're working with, so I can't code the above for you. But that's essentially the algorithm. Quote Link to post Share on other sites
teoo315 0 Posted July 15, 2012 Author Report Share Posted July 15, 2012 (edited) This is the logical process i'm trying to coding: Create an array "res" of two arrays called "fissi" and "conteggi" and populate it.Find the maximum value of array "conteggi"Find the matching value of array "fissi" inside array "res"Save this values into $c and $f For example: Array "conteggi"; (40, 80, 120, 160, 200, 240, 280, 320, 360, 400)Array "fissi"; (80, 320, 720, 1280, 2000, 2880, 3920, 5120, 6480, 8000)Array "res"; :[0] 40 80[1] 80 320[2] 120 720[3] 160 1280[4] 200 2000[5] 240 2880[6] 280 3920[7] 320 5120[8] 360 6480[9] 400 8000Max of "conteggi" = 8000 with key=[9] then$c = 8000$f = 400 Edited July 15, 2012 by teoo315 Quote Link to post Share on other sites
boen_robot 107 Posted July 15, 2012 Report Share Posted July 15, 2012 Let's do this with the concrete values first. You can abstract it later with "real" values. Your full $res array can be expressed like so: $res = array( 'conteggi' = array(40, 80, 120, 160, 200, 240, 280, 320, 360, 400), 'fissi' = array(80, 320, 720, 1280, 2000, 2880, 3920, 5120, 6480, 8000)); With that in your code, the most efficient way to get what you want: $f = max($res['conteggi']);$c = $res['fissi'][array_search($f, $res, true)]; Now... to generate the array properly, let's see your generation code: $i=0;$countLow=0;$fissoLow=0;for ($i=0;$i<10; $i++){ $z+=2; $fissoLow = 10*2*$z; $countLow = $fissoLow*$z; $res[]=array("fissi" => array($fissoLow),"conteggi" => array($countLow)); } This appends new arrays onto $res. It doesn't append on $res' subarrays ('fissi' and 'conteggi') - new 'fissi' and 'conteggi' arrays are created, and added on the $res array. To put this in another way, your code generates the equivalent of: $res = array( array( 'fissi' => array(80), 'conteggi' => array(40) ), array( 'fissi' => array(320), 'conteggi' => array(80) ), array( 'fissi' => array(720), 'conteggi' => array(120) ), array( 'fissi' => array(1280), 'conteggi' => array(160) ), array( 'fissi' => array(2000), 'conteggi' => array(200) ), array( 'fissi' => array(2880), 'conteggi' => array(240) ), array( 'fissi' => array(3920), 'conteggi' => array(280) ), array( 'fissi' => array(5120), 'conteggi' => array(320) ), array( 'fissi' => array(6480), 'conteggi' => array(360) ), array( 'fissi' => array(8000), 'conteggi' => array(40) )); and that's very different from what we set out for. Instead of creating new arrays, you need to append to the existing ones, just as we did with $res - using the "[]" operator. BTW, you should also initialize your $z variable... so: $z=0;$countLow=0;$fissoLow=0;$res = array();for ($i=0;$i<10; $i++){ $z+=2; $fissoLow = 10*2*$z; $countLow = $fissoLow*$z; $res['fissi'][] = $fissoLow; $res['conteggi'][] = $countLow;} Quote Link to post Share on other sites
teoo315 0 Posted July 15, 2012 Author Report Share Posted July 15, 2012 Now it's more cleary, but if i try your code: $z=0;$countLow=0;$fissoLow=0;$res = array();for ($i=0;$i<10; $i++){ $z+=2; $fissoLow = 10*2*$z; $countLow = $fissoLow*$z; $res['fissi'][] = $fissoLow; $res['conteggi'][] = $countLow;}$f = max($res['conteggi']);$c = $res['fissi'][array_search($f, $res, TRUE)];echo "f should be 8000 =>" . $f . " c should be 400 =>". $c; This is the output: f should be 8000 =>8000 c should be 400 =>40 There is a problem with $c as you can see, but i don't know why.It seems right the function array_search... Quote Link to post Share on other sites
boen_robot 107 Posted July 15, 2012 Report Share Posted July 15, 2012 Nope. the fault is in the array_search() alright... I made it analyze the first dimension of the $res array, rather then the array within the "conteggi" key. So replace that with $c = $res['fissi'][array_search($f, $res['conteggi'], true)]; Quote Link to post Share on other sites
teoo315 0 Posted July 16, 2012 Author Report Share Posted July 16, 2012 Thank you, now everything works perfectly Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.