0

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

1
  • echo $c; ... what do you expect to see here? It's not clear from your question (at least, it's not clear to me). Commented Dec 19, 2012 at 20:09

2 Answers 2

4

You use extract & array_combine

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

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

Output

3

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

3 Comments

is it deafault without printing it? like $a the default value is 1? LIKE: $a = 1; without the used of echo?
extract would automatically create the variable even if you don't pint it
whats the difference putting a true ex: extract(array_combine($arr1, $arr2), TRUE);
2

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
)

2 Comments

how im gonna print the variable something like when I used the array_combine.
Use extract() if you want to turn each array key into its own variable. In my example it would be extract($c)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.