Question

I have 2 arrays:

$arrA = array("a", "b");
$arrB = array("1", "2");

How to set array a to array b and keep them different objects like making $arrayA a variable (a & b) and $arrB will be the variable of variable $a & $b? Like:

$a = 1;
$b = 2;

My desired output like testing the code is like:

$c = $a + $b;
echo $c;

thanks for the help

Was it helpful?

Solution

You use extract & array_combine

$arrA = array("a", "b");
$arrB = array("1", "2");

extract(array_combine($arrA, $arrB));
echo $a + $b ;

Output

3

OTHER TIPS

Use array_combine()

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

The above example will output:

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top