Php create vars from array

Generating Variables from Arrays in PHP

One suggestion is to use an array with associative id keys instead, which will give you a completed array that can be easily looped through. Another solution involves using two consecutive ‘$’ in PHP, which creates a new variable with the name of the value of the original variable.

Php: creating variables from array

I possess a complex array that I want to grab object and have established a variable for it. As an illustration, here’s a glimpse of the array:

array(17) < [0]=>array(2) < [0]=>int(1325003844) [1]=> array(2) < [0]=>int(31) [1]=> string(19) "ONBOARD TEMPERATURE" > > 

I have identified 32 parts similar to this, where the desired value (as shown in the quoted text) is located at $foo[x][1], with x ranging from 0 to 31. I am curious whether I can use a loop (such as foreach or for) to iterate through each value of the variable and retrieve the text, subsequently adding it to a variable.

So the current code I have is:

In order to get the desired value in a solitary variable, I have to assign $foo_01 to $json_a[0][1] at present, followed by repeating the process 32 times for the remaining variables ($json_a[1][1], $json_a[2][1], $json_a[3][1], and so on). However, it would be preferable to have a single statement that would assign all the variables simultaneously.

Please inform me if you require further details. Thank you once more!

//Initialize an array variable to hold the names $names= array(); //Creates an array of 32 strings (the names) foreach ($items as $item) < $names[]=$item[1]; >//If you want to display all of the name at once, you can easily //Implode the array to echo all of the items with a linebreak in between echo implode('
', $names); //or you can cycle through the results and do something for each name //although this can be done in the original loop. foreach ($names as $name)< echo $name.'
'; >

Your array architecture appears to be peculiar. However, without much information about your objective, do you believe a different format, such as the one presented, would be more suitable?

array(31) < [0]=>array(2) < [id]=>int(1325003844), [name]=> "ONBOARD TEMPERATURE" >, [1]=> array(2) < [id]=>int(1325003845), [name]=> "NAME 2" >, etc. > 

By using this method, your array will be fully completed and prepared for iteration, is that correct?

foreach($results as $result) < if(isset($result[1][10])) < // If index 10 of $result exists $foo[] = $result[1][10]; >> //Then you can access each one when needed echo $foo[1]; 

When two consecutive ‘$’ are used in PHP, a new variable with the name of the value of the original variable will be created. For instance:

$var1 = 'variable2' $$var1 = 'hello'; echo $var1; echo $variable2; 

Based on the aforementioned instances, it appears that your naming conventions do not align with the aforementioned approach (specifically, «ONBOARD TEMPERATURE» has a space). As a result, it may be more effective to use an array that utilizes associative id keys, as demonstrated below.

Receive a variable named $values[‘ONBOARD TEMPERATURE’] which would produce the output as 31 .

Php: creating variables from array, I have a multi-part array that I want to grab a piece of and create a variable for it. Below is a sample of the array: array (17) < [0]=>array (2) < [0]=>int (1325003844) [1]=> array (2) < [0]=>int (31) [1]=> string (19) «ONBOARD TEMPERATURE» > > There are 32 different parts like this where the value I want (like the text in quotes above Code samplearray(31) array(2) int(1325003844),[name]=> «ONBOARD TEMPERATURE»>,Feedback

Create variable from array php

how to store array in variable php

$something = array( 'http://www.marleenvanlook.be/admin.php', 'http://www.marleenvanlook.be/checklogin.php', 'http://www.marleenvanlook.be/checkupload.php', 'http://www.marleenvanlook.be/contact.php', ); foreach($something as $key => $value) < $key = 'something' . $key; $$key = $value; // OR (condensed version) // $"> = $value; > echo $something2; // http://www.marleenvanlook.be/checkupload.php

how to store array in variable php

$data = array( 'something1', 'something2', 'something3', ); extract($data, EXTR_PREFIX_ALL, 'var'); echo $var0; //Output something1

PHP Arrays, Create an Array in PHP. In PHP, the array () function is used to create an array: array (); In PHP, there are three types of arrays: Indexed arrays — Arrays with a numeric index. Associative arrays — Arrays with named keys. Multidimensional arrays — Arrays containing one or more arrays.

How to create php variable in array

in arrray php

$arr = array(‘php’,’java’,’python’); if ( in_array( ‘java’, $arr ) ) < echo 'available'; >else

PHP build array from variables, Create free Team Collectives™ on Stack Overflow. Find centralized, trusted content and collaborate around the technologies you use most. PHP build array from variables. Ask Question Asked 8 years, 7 months ago. Modified 8 years, 7 months ago. Viewed 6k times 0 So I have a variable which I explode: what is it exactly …

Источник

Php create vars from array

// Before php 5.4
$array = array(1,2,3);

// since php 5.4 , short syntax
$array = [1,2,3];

// I recommend using the short syntax if you have php version >= 5.4

Used to creating arrays like this in Perl?

Looks like we need the range() function in PHP:

$array = array_merge (array( ‘All’ ), range ( ‘A’ , ‘Z’ ));
?>

You don’t need to array_merge if it’s just one range:

There is another kind of array (php>= 5.3.0) produced by

$array = new SplFixedArray(5);

Standard arrays, as documented here, are marvellously flexible and, due to the underlying hashtable, extremely fast for certain kinds of lookup operation.

Supposing a large string-keyed array

$arr=[‘string1’=>$data1, ‘string2’=>$data2 etc. ]

when getting the keyed data with

php does *not* have to search through the array comparing each key string to the given key (‘string1’) one by one, which could take a long time with a large array. Instead the hashtable means that php takes the given key string and computes from it the memory location of the keyed data, and then instantly retrieves the data. Marvellous! And so quick. And no need to know anything about hashtables as it’s all hidden away.

However, there is a lot of overhead in that. It uses lots of memory, as hashtables tend to (also nearly doubling on a 64bit server), and should be significantly slower for integer keyed arrays than old-fashioned (non-hashtable) integer-keyed arrays. For that see more on SplFixedArray :

Unlike a standard php (hashtabled) array, if you lookup by integer then the integer itself denotes the memory location of the data, no hashtable computation on the integer key needed. This is much quicker. It’s also quicker to build the array compared to the complex operations needed for hashtables. And it uses a lot less memory as there is no hashtable data structure. This is really an optimisation decision, but in some cases of large integer keyed arrays it may significantly reduce server memory and increase performance (including the avoiding of expensive memory deallocation of hashtable arrays at the exiting of the script).

When creating arrays , if we have an element with the same value as another element from the same array, we would expect PHP instead of creating new zval container to increase the refcount and point the duplicate symbol to the same zval. This is true except for value type integer.
Example:

$arr = [‘bebe’ => ‘Bob’, ‘age’ => 23, ‘too’ => 23 ];
xdebug_debug_zval( ‘arr’ );

(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=0, is_ref=0)int 23
‘too’ => (refcount=0, is_ref=0)int 23

but :
$arr = [‘bebe’ => ‘Bob’, ‘age’ => 23, ‘too’ => ’23’ ];
xdebug_debug_zval( ‘arr’ );

(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=0, is_ref=0)int 23
‘too’ => (refcount=1, is_ref=0)string ’23’ (length=2)
or :

$arr = [‘bebe’ => ‘Bob’, ‘age’ => [1,2], ‘too’ => [1,2] ];
xdebug_debug_zval( ‘arr’ );

(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2
‘too’ => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2

This function makes (assoc.) array creation much easier:

function arr (. $array )< return $array ; >
?>

It allows for short syntax like:

$arr = arr ( x : 1 , y : 2 , z : 3 );
?>

Instead of:

$arr = [ «x» => 1 , «y» => 2 , «z» => 3 ];
// or
$arr2 = array( «x» => 1 , «y» => 2 , «z» => 3 );
?>

Sadly PHP 8.2 doesn’t support this named arguments in the «array» function/language construct.

Источник

Читайте также:  Сложение в системах счисления python
Оцените статью