1

Can you please help me extracting MySQL data in php array.

my sql:

SELECT count(*) as total, post_type as type FROM wp_posts group by post_type;

to php, like below:

<?php $total = array(5,7, .. , ..); $type = array('Page', 'Post', '..',..'); ?>

Array should come from database

Thanks :)

1
  • 1
    Did you tried something? Commented May 26, 2016 at 12:11

1 Answer 1

1

You can't get two separate arrays from single SQL query either you have to run the mysql query two times or Write a PHP code which gives you the desired result.

Your current query will give result as follow.

Array
(
    [0] => Array
    (
        [total] => 5
        [post_type] => Page
    )
)

Now you have to traverse these array to create two separate arrays you want.

$total=array_column($result,'total');
$type = array_column($result,'post_type');

Above code will give you two separate arrays.

Thanks to Niet the Dark Absol for array_column, it looks more clean then traversing manually.

Sign up to request clarification or add additional context in comments.

1 Comment

Alternative: $total = array_column($result,'total'); $type = array_column($result,'post_type'); - documentation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.