0

I have a super simple array that I need to turn into individual string/integer variables. Eg:

Array ( [0] => Array ( [0] => 35 [1] => 507 [2] => 203 ) ) 

Should result in

$value1 = 35
$value2 = 507
$value3 = 203

I'm sure this is pretty simple but I've looked at implode and it seems to concatenate the arrays into a single string, whereas I need each value as a separate variable. Have also tried foreach but come unstuck. Anyone know how to do this?

I can't seem to use $array[0][0] (=35) as I'm putting these variables into a MySQLi query and keep getting errors when I do it this way.

Thanks!

5
  • 2
    use php extract Commented Apr 28, 2017 at 7:46
  • $array[0][0], $array[0][1], etc. And you don't need individual variables. Commented Apr 28, 2017 at 7:47
  • share you error as well. Commented Apr 28, 2017 at 7:48
  • 1
    i gave hint to him . extract have parameter too . he need to check that all to get what he need . @BunkerBoy Commented Apr 28, 2017 at 8:08
  • thanks @jYoThi i got it.... Commented Apr 28, 2017 at 8:13

3 Answers 3

2
extract($array[0], EXTR_PREFIX_ALL, "value"); 
// $value_1, $value_2, $value_3 are available

Read more: http://php.net/manual/en/function.extract.php

Sign up to request clarification or add additional context in comments.

Comments

2

Use php list() function like:

$arr = array( 0 => array ( 0 => 35, 1 => 507, 2 => 203 ) ) ;
list($a, $b, $c) = $arr[0];
echo $a.' : '.$b.' : '.$c;

Working Code

Php List Reference

1 Comment

Sounds great!! :)
0

Used extract and it works perfectly now for my query. Thanks all.

extract($array, EXTR_PREFIX_ALL, "custom");
extract($custom_0, EXTR_PREFIX_ALL, "custom2");
echo "Array item 1 is " . $custom2_0;
echo "Array item 2 is " . $custom2_1;
echo "Array item 3 is " . $custom2_2;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.