There a few things going on here.
First, you’re getting back a multidimensional array (array of arrays) where the nested arrays have column names as keys. The reason for this is your use of the ARRAY_A
parameter. You can read about the output types in the codex entry. You may be just fine using the default OBJECT
. I’m going to answer as you have it though.
Second, the values of the nested arrays are SQL escaped strings.
This answer gives a good explanation as to why the result is escaped and suggests the stripslashes();
to fix it.
This answer gives a good explanation as to why strings come back instead of integers. It suggests typecasting or a PHP function but I would use the WordPress function absint() to make sure I had a non-negative integer.
So you could use something like this to achieve your desired outcome:
function wpsx_make_item_id_to_int($userGadgets) {
//Init out collection array
$item_id_ints = array();
//Process parent array
foreach ($userGadgets as $key => $array) {
//Process nested arrays
foreach ($array as $column_name => $column_value) {
//$column_value is your item_ids
if (is_string($column_value)) {
//Make ID into int and add it to our colection
$item_id_ints[] = absint(stripslashes($column_value));
}
}
}
//Return an associative array of (int)item_ids
return $item_id_ints;
}
However are you sure you need to be using $wpdb in the first place? I find most of the time you can achieve what you need to by leveraging built in WordPress functionally and API’s with the added benefit of a bit more ease and security.