Split two-dimensional array into 2 simple arrays - PHP

Asked

Viewed 395 times

2

I am making adaptations in an old system and need to create a new routine to split a two-dimensional array into 2 simple arrays.

Let’s say the variable $query get the array below:

$query = array("SELECT * FROM teste WHERE nome in (?) and id in (?)", array('aaa', 1));

My goal is to split this array and make a variable $sql receive:

SELECT * FROM teste WHERE nome in (?) and id in (?)

And another, called $arg, receive:

array('aaa', 1)

I’ve tried this division with implode, but the return was not what was expected. And with explode always get NULL.

Is there a native PHP function that does this division? Or at least some routine that can do it through brute force?

Thanks in advance.

1 answer

2


You can do this using the builder void list ( mixed $varname [, mixed $... ] )

PHP:

$query = array("SELECT * FROM teste WHERE nome in (?) and id in (?)", array('aaa', 1));


list($sql, $arg) = $query;

echo $sql;
echo PHP_EOL.'-----------------------'.PHP_EOL;
var_dump($arg);

Upshot:

SELECT * FROM teste WHERE nome in (?) and id in (?)
-----------------------
array(2) {
  [0]=>
  string(3) "aaa"
  [1]=>
  int(1)
}
  • That’s exactly what I needed, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.