Jump to content

Select multiple values using Db::getInstance()->executeS into simple, one dimensional array


hakeryk2
 Share

Recommended Posts

Hello dev's

 

I have probably easy question for advanced prestashop and php developers. I will show an example what I want to achieve in simpler way. Let's start with just example.

 

Currently when I want to retrieve ie all employees ID I will use: 

$employees_id = Db::getInstance()->executeS('
    SELECT id_employee FROM '._DB_PREFIX_.'employee
');

but the output that I will receive with ddd($order_employees_id) is 

Array
(
    [0] => Array
        (
            [id_employee] => 1
        )

    [1] => Array
        (
            [id_employee] => 2
        )

    [2] => Array
        (
            [id_employee] => 3
        )

    [3] => Array
        (
            [id_employee] => 4
        )

    [4] => Array
        (
            [id_employee] => 5
        )

    [5] => Array
        (
            [id_employee] => 7
        )

)

and I have to use 

$ids = array();

foreach ($employees_id as $key => $value) {
    $ids[] .= $employees_id[$key]['id_employee'] ;
}

to receive array type that I want which is

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 7
)

Maybe there is other way to acomplish that without using foreach every time? It is just much easier to operate sometimes. Any help will be very appreciated:)

Edited by hakeryk2 (see edit history)

Share this post


Link to post
Share on other sites

  • 4 years later...
On 4/14/2017 at 1:45 PM, ventura said:

Sure. but in anycase it is always better get a default function for this kind of methods.

In your query method you can use

$onlyIds = array_column($employees_id, 'id_employee');

Thanks a lot. Its just what i needed

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More