Echo all Keys and Values of array in a loop

Hello, I have been working with some arrays and I am having issues trying to sort and display all the data in the array. I am pretty sure I am adding the correct info to the array as var_dump seems to have all the info inside it.

Here is how I push the data to the array:

		$person = array('user' => $row['USERS'], 'total' => $row_cntw, 'goal' => $rgoal, 'dailycount' => $dailycount);
		array_push($sortusers, $person);

I would like to sort it by the “total” value then display all the data something like this.
User : anderson - Total : 259 - Goal : - dailycount :

Here is the var_dump($sortusers);

array(18) { [0]=> array(4) { ["user"]=> string(8) "anderson" ["total"]=> int(259) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [1]=> array(4) { ["user"]=> string(8) "anibal17" ["total"]=> int(17130) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [2]=> array(4) { ["user"]=> string(4) "BROC" ["total"]=> int(59) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [3]=> array(4) { ["user"]=> string(9) "CAITLINMC" ["total"]=> int(10413) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [4]=> array(4) { ["user"]=> string(7) "DGil915" ["total"]=> int(74) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [5]=> array(4) { ["user"]=> string(7) "dominic" ["total"]=> int(82) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [6]=> array(4) { ["user"]=> string(4) "gman" ["total"]=> int(306) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [7]=> array(4) { ["user"]=> string(11) "guevara7941" ["total"]=> int(6528) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [8]=> array(4) { ["user"]=> string(11) "johnnydingo" ["total"]=> int(72) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [9]=> array(4) { ["user"]=> string(8) "manchaca" ["total"]=> int(128) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [10]=> array(4) { ["user"]=> string(2) "MJ" ["total"]=> int(557) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [11]=> array(4) { ["user"]=> string(3) "Neo" ["total"]=> int(24) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [12]=> array(4) { ["user"]=> string(12) "onlineorders" ["total"]=> int(58) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [13]=> array(4) { ["user"]=> string(8) "pawnshop" ["total"]=> int(64) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [14]=> array(4) { ["user"]=> string(4) "room" ["total"]=> int(8) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [15]=> array(4) { ["user"]=> string(9) "roundrock" ["total"]=> int(1) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [16]=> array(4) { ["user"]=> string(5) "Zahir" ["total"]=> int(308) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } [17]=> array(4) { ["user"]=> string(11) "Zahirmadani" ["total"]=> int(18) ["goal"]=> string(2) "60" ["dailycount"]=> int(74) } }

Hope everything is clear.

Thanks!

array_multisort(array_column($person, 'total'), SORT_DESC, $person);

Is there some reason you are not getting the data from the DB the way you want it?

1 Like

If this is coming from a database, it is simple to make the tweak there…

Thanks that worked.
array_multisort(array_column($sortusers, ‘total’), SORT_DESC, $sortusers);

Everything is being pulled from a database let me give a run down of what I am doing.
All of my employees scan each piece of product when they are finished with it.
It saves into databases and there are 4 key components I need (user name) (ALL TIME TOTAL) (DAILY GOAL) (DAILY COUNT).

I was fist pulling the user name and all time totals and saving them to an array, then I would sort the array and run it threw a array_walk function to pull all the Goals and Daily Totals so I could calculate percentages and see where everyone is at with a quick glance. This was causing like a 18 second lag during the array_walk function so I wanted to try to take a different approch and just pull all the data at first and save it into an array and then run it through a php function to display the data.

Here is the array_walk function that was causing the delay.

 function printer($v, $k) {
 	$dailycount = 0;
 	$con=mysqli_connect("localhost","dsdgsdgsag","asgasggsd","asdgsagdsgad");
 	$querygoals = "SELECT * FROM dtsmembers WHERE username LIKE '" . $k . "%'";  
 	$resultg = mysqli_query($con,$querygoals) or die(mysqli_error());
 	while($row = mysqli_fetch_array($resultg)){
 		$rgoal = $row['GOAL'];
 	}
     mysqli_free_result($resultg);
 	$query = "SELECT USERS, count(USERS) FROM ltusers WHERE RETIRED NOT LIKE $isretired' GROUP BY USERS";  
 	$result = mysqli_query($con,$query) or die(mysql_error());
 	while($row = mysqli_fetch_array($result)){
 	$ruser = $k;
 	$sqlw = "SELECT * FROM ltusers WHERE USERS LIKE '" . $ruser . "%'";
 	if($resultzw = mysqli_query($con, $sqlw)){
 		$row_cntw = mysqli_num_rows($resultzw);
 		$sql = "SELECT * FROM ltusers WHERE reg_date > CURDATE() AND USERS LIKE '" . $ruser . "%' GROUP by STAGS"; 
 		if($resultz = mysqli_query($con, $sql)){
 			if(mysqli_num_rows($resultz) > 0){
 				$dailycount = mysqli_num_rows($resultz);
 			}
 		}
 	}
 }		
 
 if($rgoal <= 0){
 	echo "$k - $dailycount/GOAL NOT SET ";
 	echo "<br />";
     echo "TOTAL: $v\n";
 	echo "<br />";
 }
 else
 {
 	$x = $dailycount;
 	$y = $rgoal;
 	$percent = $x/$y;
 	$percent_friendly = number_format( $percent * 100, 2 ) . '%';
 	echo "$k - $dailycount/$rgoal $percent_friendly ";
 	echo "<br />";
     echo "TOTAL: $v\n";
 	echo "<br />";
 	}
 	mysqli_close($con);
 }
 arsort($sortusers);
 array_walk($sortusers, "printer");

Probably very noobish…

Two things, the biggest, use prepared statements. The last, you should be sorting in the query, not the code.

1 Like
$sql = "SELECT * FROM ltusers WHERE reg_date > CURDATE() AND USERS LIKE '" . $ruser . "%' GROUP by STAGS ORDER BY total";

And you don’t want to do a select *, you want to name the columns you want returned, always.

Actually, all of those queries are over complicating things for you. You just need a new query to get what you want.

1 Like

What @astonecipher said. Everything you want to do can and should be done in the query, not the code.

Sponsor our Newsletter | Privacy Policy | Terms of Service