With PHP, using the array_push() function and $newarray[] syntax is an easy way to make arrays from a foreach or while loops. However, both methods create indexed arrays and may not be adequate to build the array you want.
For example, you may have a loop that is pulling data from two columns in a database and you just want a simple associative array that contains the matching key and value.
The $user_array that is commented out is the same array you want from the while loop. As you can see, the value from the the ‘access_level’ column is the key and the value from the ‘name’ column is the value.
//$user_array = array(1 => 'admin', 2 => 'managers', 3 => 'members', 4 => 'public'); $command = "SELECT DISTINCT access_level, name FROM access_level"; $result = $PDO->prepare($command); $result->execute(); $user_array = array(); while ($row = $result->fetch(PDO::FETCH_ASSOC)){ $user_array[$row['access_level']] = $row['name']; }
© 2018 ALL Rights Reserved | Sitemap
Vancouver | North Vancouver | Burnaby | Richmond | Coquitlam | Port Coquitlam | Surrey | Langley | Maple Ridge | Mission | Abbotsford | Kamloops | Kelowna | Victoria | Squamish | Whistler | Pemberton