0

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?

4

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

| improve this answer | |
0

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
}
| improve this answer | |
  • Wouldn't I need to set the feedid as key first? – Louisa Jul 1 '16 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 Jul 1 '16 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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