简体   繁体   中英

PhP Array how to get a value of a key

I have a php array, $vararray, like so

[var1] => 1
[var2] => 1
[var3] => 1
[var4] => 1
[var5] => 2
[var6] => 2
[var7] => 1
[var8] => 1

After a bunch of operations i get a key/index of var8. How do I get value of var8 using the key? I can get the key using array key function. But is there a function for array value? I checked php manual but didnt seem to find any.

If $key contains the key you're interested in (eg var8 ) you use

$vararray[$key]

to access the value of the corresponding element.

You can access it like this:

$value = $vararray['var8'];

Or you can loop over the array using a foreach loop: http://php.net/manual/en/control-structures.foreach.php

foreach ($vararray as $value) {
    echo $value;
}

像这样可访问

$var8Value = $vararray['var8'];

For getting all keys value like $var1 , $var2 , $var3 , .... Try this

foreach($vararray as $key=>$val){
    $$key = $val;
}
echo $var1;
echo $var2;
echo $var3;
echo $var4;
echo $var5;
echo $var6;
echo $var7;
echo $var8;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM