funbinod Posted February 9, 2015 Share Posted February 9, 2015 how can I print php arrays as we print sql arrays using while loop.? print_r ( ) returns array as Array ([1] => A [2] => B [3] => C [4] => D [5] => E) But I wish to print as A B C D E That we get using sql query like... while {$row = mysqli_fetch_array($query) { ................... .................. } Link to comment Share on other sites More sharing options...
Ingolme Posted February 9, 2015 Share Posted February 9, 2015 PHP has a few iterating functions. Such as each(), current(), and next(). Here's an example while($item = each($arr)) { echo $item . '<br>';} But you can print elements in an array without them, the more common way: foreach($arr as $item) { echo $item . '<br>';} 1 Link to comment Share on other sites More sharing options...
funbinod Posted February 9, 2015 Author Share Posted February 9, 2015 (edited) solved! thank u. now will u take me more deeper? I wonder if there is some method of calculation between arrays!! meaning, I want to calculate value(s) of this row with the value of previous row. below is the example.. while ($row = ....................) { $ttl = ($row['a']+$row['b']-$row['c']); echo $row['a'] . ' | ' . $row ['b'] . ' | ' . $row['c'] . ' | ' . $ttl . '<br />';} but after each row I wish the value of $ttl on each rows be calculated in respect with the previous row.. lets look with practical example... lets assume some data derived from above query... $row['a'] | $row['b'] | $row['c'] | $ttl | THIS ------------|-------------|------------|------|------ 9 | 2 | 3 | 8 | 8 ------------|-------------|------------|-------|------ 7 | 1 | 5 | 3 | 11 ------------|-------------|------------|-------|------ 8 | 6 | 4 | 10 | 21 what I wish is the calculation of THIS column... on first row the value of $ttl is put on THIS column. on second row value of $ttl is added with value of THIS column of first row and put on THIS column of second row and so on third row... this is something like prev() function on an array... possible????? Edited February 9, 2015 by funbinod Link to comment Share on other sites More sharing options...
justsomeguy Posted February 9, 2015 Share Posted February 9, 2015 Here is the list of array functions:http://php.net/manual/en/ref.array.phpThey are general-purpose functions for the most part. If you want to do something specific like what you're asking then just use a loop to go through and print the array, and have a set of variables to keep track of whatever you want to keep track of. Link to comment Share on other sites More sharing options...
funbinod Posted February 9, 2015 Author Share Posted February 9, 2015 understood nothing on the link that can guide me through what I wish.. I understand of "having a set of variables to keep track of whatever I want" as u suggested. but my problem is for calculation using the value derived from the calculation between the values of previous row.... Link to comment Share on other sites More sharing options...
justsomeguy Posted February 9, 2015 Share Posted February 9, 2015 How is that a problem? The far right column in your example starts at 0, and every time you process a record you add that record's TTL value to the variable. It's just a running total of one of the columns in the result, it's just addition. Link to comment Share on other sites More sharing options...
funbinod Posted February 10, 2015 Author Share Posted February 10, 2015 (edited) ok! let me guess and try something. assuming $bal is the rightmost column $bal = $prebal // this assumes the first ever row starts with some value calculated previously$bal += $svalue - $pvalue // preceding columnsecho $bal; is this the method you suggested me? but this calculated value for each row only. like, $prebal + $svalue - $pvalue. it didn't continued adding to further rows. Edited February 10, 2015 by funbinod Link to comment Share on other sites More sharing options...
Ingolme Posted February 10, 2015 Share Posted February 10, 2015 Before loop: $total = 0; In loop: $subtotal = $row['a']+$row['b']-$row['c'];$total += $subtotal; Link to comment Share on other sites More sharing options...
funbinod Posted February 10, 2015 Author Share Posted February 10, 2015 (edited) ok! I got it now and solved it. I did the mistake. I had placed the initial value of $bal inside the loop. now I placed the initial value of $bal (i.e. $bal = $prebal) before starting a loop. $bal = $prebal;foreach ($period as $month) { ......................; ......................; $bal += ($svalue - $pvalue); echo $bal;} this solved the problem. thank u both for your company.... Edited February 10, 2015 by funbinod Link to comment Share on other sites More sharing options...
sandeepm Posted February 25, 2015 Share Posted February 25, 2015 both are arrays so display in same format Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now