create single variables from two arrays

write statements to declare the array and assign the values to the corresponding positions.
how to create an array in java
creating an array c#
simple array
creating an array c++
how many elements are in an 8 3 2 array
how to create an array in c
assign value to array, c++

I have two php arrays

$arra = array('one', 'two', 'five', 'seven');

$arrb = array('black', 'white', 'gold', 'silver');

I need to create variables, like this way:

foreach ($arra as $el) {
    $el = $arrb[index of $el];
}

So echoing resulting variables:

echo $one should be black echo $two should be white and so on.

How to do that?

You can actually set a variable's name using a variables value, You need something like this:

for($x=0; $x < count($arra); $x++){
    $$arra[$x] = $arrb[$x];
}

Notice the double $$. This is not a mistake.

Creating and Using Arrays, that creates the array, puts some values in it, and displays the values. Like declarations for variables of other types, an array declaration has two program, the compiler would print an error like the following one and compilation would fail​. C++ Arrays. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store:

Try with array_combine() and then use foreach() to make dynamic variables. array_combine()- Creates an array by using one array for keys and another for its values

<?php
 $arra = array('one', 'two', 'five', 'seven');
 $arrb = array('black', 'white', 'gold', 'silver');
 $result = array_combine($arra, $arrb);
 foreach($result as $key=>$value){
    ${$key} = $value;
 }
 echo $two;
?>

DEMO: https://3v4l.org/VPNQh

OR use extract() after combining $arra and $arrb. extracts()- Imports variables into the current symbol table from an array.

<?php
$arra = array('one', 'two', 'five', 'seven');
$arrb = array('black', 'white', 'gold', 'silver');
$result = array_combine($arra, $arrb);
extract($result, EXTR_PREFIX_SAME,'');
echo "$one, $two, $five, $seven";
?> 

DEMO: https://3v4l.org/VW55k

array_combine - Manual, array_combine — Creates an array by using one array for keys and another for its Array ( [a] => 2 [b] => 3 ) But if you need to keep all values, you can use the  First, let’s create a one-dimensional array or an array with a rank 1. arange is a widely used function to quickly create an array. Passing a value 20 to the arange function creates an array with values ranging from 0 to 19. Output: To verify the dimensionality of this array, use the shape property.

I think this is what you want:

$arra = array('one', 'two', 'five', 'seven');

$arrb = array('black', 'white', 'gold', 'silver');

foreach($arra as $key=>$ela) {
  $elb = $arrb[$key];
  ${$ela} = $elb;
}

echo $one;
echo $two;
echo $five;
echo $seven;

For using strings as variables I found this link helpful.

array_merge - Manual, Merges the elements of one or more arrays together so that the values of one are appended to the end of Example #2 Simple array_merge() example Creates an array by using one array for keys and another for its values; array operators. So I have the below variables which are a mix of data types including integers, strings, doubles and arrays. I already have the methods required to fill each variable. All I need now is to gather all the variables into a single row of data, and than display that data in another class for later use.

Manipulating data in arrays and data frames, Here are two methods of creating an array of numbers A matrix is a two dimensional array. A column of a data frame corresponds to a single variable. S = stack(U,vars) converts the table or timetable, U, into an equivalent table or timetable, S, that is stacked. The stack function stacks up multiple variables from U, specified by vars, into a single variable in S. In general, S contains fewer variables, but more rows, than U.

[PDF] Loop-Do-Loop Around Arrays, Read variable length input files. • Create multiple records from one observation. • Create one observation from multiple observations. Arrays in SAS are very  So it is a Ruby function that returns a fixed number of values in the array, and the caller wants them in two separate variables. This is where the feature makes most sense: first_name, last_name = get_info() // always returns an array of length 2

[PDF] Using Arrays in SAS Programming, single year, 12 variables for revenues (Rev1–Rev12), and 12 variables for 2. Using Arrays in SAS® Programming. Variables that are associated with an array have variables do not exist in the Rev_Exp data set, so they are created as new  Example values in the paramVal[i] array: ["Jon", "15", "jon@gmail.com"] I need to create a JavaScript object that places all of the items in the array in the same object. For example {newParamArr[0]:paramVal[0], newParamArr[1]:paramVal[1], } The lengths of the two arrays are always the same, but the length of arrays can increase or decrease

Comments
  • extract(array_combine($arra, $arrb)); php.net/manual/en/function.extract.php
  • @splash58, it works, thanks.
  • Glad I could help, what changed?
  • some typo in my code. it's ok now. where is a reference for double $, pls?