I
I
Ibishka2020-04-27 13:27:44
PHP
Ibishka, 2020-04-27 13:27:44

How to get an array from PHP keys?

From js to php, an array comes in the form of a string, because it is sent like this.

const a = [{3:5},{1:2}];
JSON.stringify(a)

I need an array of keys in php, i.e. To then make a request to the db
$arr = [3,1];
$goods = mysqli_query($connection, "SELECT * FROM `goods` WHERE `id` in $arr");

Yes, I know similar to the previous question, I just formatted the question incorrectly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-04-27
@Ibishka

$str ='[{"3":5},{"1":2}]';
$arr = json_decode($str, true);
$result = array_map(
    function ($el) {
        return array_keys($el)[0];
    },
    $arr
);
var_dump($result);
// array(2) {
//  [0] => int(3)
//  [1] => int(1)
// }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question