Using this code to get some data into a PHP array:

$currentProducts = array();
$currentProducts = $wpdb->get_results("SELECT id, feedid, size, price FROM products WHERE shopid = $shopid");

No problems getting the data into a normal PHP array.

How do I get the data into an array where the feedid is key, so I can easily and quickly check if a specific feedid exist in the array, remove a specific feedid element etc?

2 Answers 2


Found the solution:

$currentProducts = $wpdb->get_results("SELECT feedid, id, size, price FROM products WHERE shopid = $shopid", OBJECT_K);

The OBJECT_K parameter makes an associative array with feedid as key: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results


Not sure why you need to do that but, all you need is a loop:

foreach ( $currentProducts as $currentProduct ) 
    $myNewArray[$currentProduct->feedid] = $currentProduct->size;//use whatever value you need
  • Wouldn't I need to set the feedid as key first?
    – Louisa
    Commented Jul 1, 2016 at 2:13
  • This sets the feedid as the key of the array $myNewArray Do you mean you need the database table key to be the feedid?
    – dg4220
    Commented Jul 1, 2016 at 9:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.