2

I'm using the following piece of code to count the number of locations from a table:

$result = mysqli_query($conn,"SELECT location FROM table ORDER BY location");

$aloc_list = array();
while ($row = mysqli_fetch_array($result))
        {$aloc_list[] = $row['location'];
        }

print_r(array_count_values($aloc_list));

Which gives me the following array:

Array
(
    [CANADA] => 106
    [USA] => 547
    [MEXICO] => 93
    [GREAT_BRITAIN] => 111
    [EASTERN_EUROPE] => 227
    [RUSSIA] => 405
    [CHINA] => 341
    [INDIA] => 253
)

I have tried to 'extract' the array values and put them in a variable but nothing seems to work:

$canada = $aloc_list[0];
$canada = $aloc_list['CANADA'];
$canada = $aloc_list[0]['CANADA'];
$canada = $aloc_list[0];

I want to end up with a variable called $canada equal to 106, a variable called $usa equal to 547, etc.

How do I accomplish this?

4
  • 2
    You can use MySQL's aggregate COUNT(col) function given we're working with integers. There are other ways also. Commented Sep 25, 2018 at 13:42
  • 5
    $canada = $aloc_list['CANADA']; should work, as long as you do $aloc_list = array_count_values($aloc_list); Commented Sep 25, 2018 at 13:45
  • @GrumpyCrouton nope, check the print_r(array_count_values($aloc_list));. Commented Sep 25, 2018 at 13:46
  • 2
    @Cid Updated my comment. It only doesn't work because OP isn't setting array_count_values to a variable. Commented Sep 25, 2018 at 13:46

5 Answers 5

5

If you don't want to change your query, you can store the result of array_count_values($aloc_list) and use it to extract the datas. You can use variables variables to generate dynamically variables, but I don't recommend this. You might want to check if variable exists before using it. (isset())

$aloc_list_count = array_count_values($aloc_list);
var_dump($aloc_list_count);
/*
Outputs
array (size=8)
    'CANADA' => int 106
    'USA' => int 547
    'MEXICO' => int 93
    'GREAT_BRITAIN' => int 111
    'EASTERN_EUROPE' => int 227
    'RUSSIA' => int 405
    'CHINA' => int 341
    'INDIA' => int 253
*/
foreach ($aloc_list_count AS $key => $value)
    ${strtolower($key)} = $value;
echo $canada; //106
echo $usa;    //547
echo $france; //Notice: Undefined variable: france in...

Or you can use extract function to achieve that

extract($aloc_list_count);
Sign up to request clarification or add additional context in comments.

1 Comment

Good idea with the variable variable, but you should apply strtolower() to it, OP said they want the variables to be lowercase.
3

You are trying to select the mentioned values from the original $aloc_list array - but they don't directly exist in there. They're the result of the call to array_count_values($aloc_list) - this produces a new array, and you need to read from that instead.

For example:

$counts = array_count_values($aloc_list);
$canada = $counts['CANADA'];
echo $canada; 

1 Comment

@MadhurBhaiya No it isn't, this answer is better because it explains what's happening, and what is needing to happen.
3

Try the following instead:

$sql = "SELECT location, COUNT(*) as count_per_location 
        FROM table 
        GROUP BY location 
        ORDER BY location"

$result = mysqli_query($conn, $sql);

$aloc_list = array();
while ($row = mysqli_fetch_assoc($result))
{ 
    ${strtolower($row['location']} = $row['count_per_location'];
}

1 Comment

This is certainly the best answer if he doesn't need the query to do something else than counting elements
1

on the line with:

print_r(array_count_values($aloc_list));

Try this instead:

$mergeLocations = array_count_values($aloc_list);
var_dump($mergeLocations['CANADA']);
var_dump($mergeLocations['USA'])

By the way, would be smarter to use mysql GROUP BY function for counting locations:

SELECT location, count(*) as total FROM table GROUP BY location ORDER BY total DESC

Comments

1

you are returning the array from array_count_values into the print_r function. try this

 $test = array_count_values($aloc_list);
 echo $test['CANADA']; 

1 Comment

I don't know of any PHP function called array_print_values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.