Split two-dimensional array into 2 simple arrays - PHP

2

I'm 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 that the variable $query receives 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 cause a $sql variable to receive:

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

And another, called $arg , will receive:

array('aaa', 1)

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

Is there a native PHP function that makes this division? Or at least some routine that can do this by brute force?

Thank you in advance.

    
asked by anonymous 22.04.2015 / 19:21

1 answer

2

You can do this using the 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);

Result:

SELECT * FROM teste WHERE nome in (?) and id in (?)
-----------------------
array(2) {
  [0]=>
  string(3) "aaa"
  [1]=>
  int(1)
}
    
22.04.2015 / 19:31