从$$变量[0]获取数组值(数组的值)

时间:2022-10-09 07:27:30

i have a problem on PHP

我有PHP的问题

i have some array

我有一些阵列

$hp = array('samsung', 'iphone', 'blackberry');

$samsung = array(array('Tab', 3,2),array('Note', 4), array('Mini', 5, 6, 8));
$iphone = array(array('2G', 2, 3, 6),array('4S', 5), array('5', 2,2), array('3GS', 2,7));
$blackberry = array(array('onix', 2),array('curve', 2, 6), array('armstrong', 5), array('gemini', 8. 1));

i need to count value from list of type of hp ($hp), and i have try this but fail;

我需要计算hp($ hp)类型列表中的值,我试过这个但是失败了;

for($p=0; $p=count($hp); $p++){ //this ok can get count of $hp = 3
    for($i=0; $i=count($$hp[$p]); $i++){ //this ok can get count of type from $hp[index], if samsung = 3, if iphone= 4, if blackberry = 4
        for($u=0; $i=count($$hp[$p][$i]); $u++){ //this fail to get count the next level of hp type, should be if samsung[0] = 3, samsung[1] = 2, samsung[2]=4 
            //any code here
        }
   }
}

can someone help me??

有人能帮我吗??

thanks

谢谢

3 个解决方案

#1


0  

A foreach loop would be a bit easier to deal with here, so that you don't have to deal with so many iterators/numbers - and you can easily assign nicely named variables to work with at each level, e.g. phone, model, specification, feature etc. You also only have to deal with one variable variable this way.

foreach循环在这里处理起来会容易一些,因此您不必处理如此多的迭代器/数字 - 并且您可以轻松地分配命名良好的变量以在每个级别使用,例如:电话,型号,规格,功能等。你也只需要这样处理一个变量变量。

Try this:

尝试这个:

foreach($hp as $phone) {
    echo 'Phone: ' . $phone . PHP_EOL;
    foreach($$phone as $model) {
        foreach($model as $value) {
            echo 'Value: ' . $value . PHP_EOL;
        }
    }
}

See it working

看它工作


Note: there's a . instead of a , in your $blackberry array example.

注意:有一个。而不是a,在你的$ blackberry数组示例中。

#2


1  

As with so many cases where people think they need "variable variables" ($$something), what you actually want here is an associative array:

与许多人认为他们需要“变量变量”($$某些东西)的情况一样,你真正想要的是一个关联数组:

$phones = array(
    'samsung' => array(array('Tab', 3,2),array('Note', 4), array('Mini', 5, 6, 8)),
    'iphone' => array(array('2G', 2, 3, 6),array('4S', 5), array('5', 2,2), array('3GS', 2,7)),
    'blackberry' = array(array('onix', 2),array('curve', 2, 6), array('armstrong', 5), array('gemini', 8, 1))
);

Now your $hp array isn't representing a set of variable names, it's representing keys in a data structure, which makes a lot more sense. (What if, later, you want to store those lists in a database, or a config file? You should be the one choosing variable names, not your program.)

现在你的$ hp数组不代表一组变量名,它表示数据结构中的键,这更有意义。 (如果以后想要将这些列表存储在数据库或配置文件中,您应该选择变量名,而不是您的程序。)

As scrowler points out, you can also make use of a foreach rather than a for loop to end up with more readable code:

正如scrowler指出的那样,你也可以使用foreach而不是for循环来获得更易读的代码:

$phones_wanted = array('samsung', 'iphone', 'blackberry');

foreach($phones_wanted as $phone_key) {
    echo 'Phone: ', $phone_key, PHP_EOL;

    // This is the point where you thought you needed a variable variable, but don't
    foreach($phones[$phone_key] as $model) {

        echo 'Model: ', $model, PHP_EOL;
        foreach($model as $value) {
            echo 'Value: ', $value, PHP_EOL;
        }
    }
}

Note that I've also used much more meaningful variable names. Your original bug was that you were using $i in two different loops. It's a lot easier to read:

请注意,我还使用了更有意义的变量名称。你最初的错误是你在两个不同的循环中使用$ i。它更容易阅读:

for ($model_index=1; $model_index=count($phones_wanted[$phone_index][$model_index]); $model_index++)

than:

比:

for($i=0; $i=count($$hp[$p][$i]); $i++){

#3


0  

You're not accessing the array correctly for the count. You would need to use $i instead of $u. Also, you are overriding the variable $i again with $i=count($$hp[$p][$u]);. Plus, I think you are looking for < instead of = for $i=count($$hp[$p][$i]); and the others:

您没有正确访问数组以进行计数。您需要使用$ i而不是$ u。此外,您使用$ i = count($$ hp [$ p] [$ u]);再次覆盖变量$ i。另外,我认为你正在寻找 <而不是= for $ i="count($$" hp [$ p] i]);和其他人:< p>

for($p=0; $p<count($hp); $p++){ //this ok can get count of $hp = 3
    for($i=0; $i<count($$hp[$p]); $i++){ //this ok can get count of type from $hp[index], if samsung = 3, if iphone= 4, if blackberry = 4
        for($u=0; $u<count($$hp[$p][$i]); $u++){ //this fail to get count the next level of hp type, should be if samsung[0] = 3, samsung[1] = 2, samsung[2]=4 
            //any code here
        }
   }
}

As IMSoP suggested, you may want to use more detailed variable names that aren't one letter to avoid future mistakes.

正如IMSoP建议的那样,您可能希望使用不是一个字母的更详细的变量名来避免将来出现错误。

#1


0  

A foreach loop would be a bit easier to deal with here, so that you don't have to deal with so many iterators/numbers - and you can easily assign nicely named variables to work with at each level, e.g. phone, model, specification, feature etc. You also only have to deal with one variable variable this way.

foreach循环在这里处理起来会容易一些,因此您不必处理如此多的迭代器/数字 - 并且您可以轻松地分配命名良好的变量以在每个级别使用,例如:电话,型号,规格,功能等。你也只需要这样处理一个变量变量。

Try this:

尝试这个:

foreach($hp as $phone) {
    echo 'Phone: ' . $phone . PHP_EOL;
    foreach($$phone as $model) {
        foreach($model as $value) {
            echo 'Value: ' . $value . PHP_EOL;
        }
    }
}

See it working

看它工作


Note: there's a . instead of a , in your $blackberry array example.

注意:有一个。而不是a,在你的$ blackberry数组示例中。

#2


1  

As with so many cases where people think they need "variable variables" ($$something), what you actually want here is an associative array:

与许多人认为他们需要“变量变量”($$某些东西)的情况一样,你真正想要的是一个关联数组:

$phones = array(
    'samsung' => array(array('Tab', 3,2),array('Note', 4), array('Mini', 5, 6, 8)),
    'iphone' => array(array('2G', 2, 3, 6),array('4S', 5), array('5', 2,2), array('3GS', 2,7)),
    'blackberry' = array(array('onix', 2),array('curve', 2, 6), array('armstrong', 5), array('gemini', 8, 1))
);

Now your $hp array isn't representing a set of variable names, it's representing keys in a data structure, which makes a lot more sense. (What if, later, you want to store those lists in a database, or a config file? You should be the one choosing variable names, not your program.)

现在你的$ hp数组不代表一组变量名,它表示数据结构中的键,这更有意义。 (如果以后想要将这些列表存储在数据库或配置文件中,您应该选择变量名,而不是您的程序。)

As scrowler points out, you can also make use of a foreach rather than a for loop to end up with more readable code:

正如scrowler指出的那样,你也可以使用foreach而不是for循环来获得更易读的代码:

$phones_wanted = array('samsung', 'iphone', 'blackberry');

foreach($phones_wanted as $phone_key) {
    echo 'Phone: ', $phone_key, PHP_EOL;

    // This is the point where you thought you needed a variable variable, but don't
    foreach($phones[$phone_key] as $model) {

        echo 'Model: ', $model, PHP_EOL;
        foreach($model as $value) {
            echo 'Value: ', $value, PHP_EOL;
        }
    }
}

Note that I've also used much more meaningful variable names. Your original bug was that you were using $i in two different loops. It's a lot easier to read:

请注意,我还使用了更有意义的变量名称。你最初的错误是你在两个不同的循环中使用$ i。它更容易阅读:

for ($model_index=1; $model_index=count($phones_wanted[$phone_index][$model_index]); $model_index++)

than:

比:

for($i=0; $i=count($$hp[$p][$i]); $i++){

#3


0  

You're not accessing the array correctly for the count. You would need to use $i instead of $u. Also, you are overriding the variable $i again with $i=count($$hp[$p][$u]);. Plus, I think you are looking for < instead of = for $i=count($$hp[$p][$i]); and the others:

您没有正确访问数组以进行计数。您需要使用$ i而不是$ u。此外,您使用$ i = count($$ hp [$ p] [$ u]);再次覆盖变量$ i。另外,我认为你正在寻找 <而不是= for $ i="count($$" hp [$ p] i]);和其他人:< p>

for($p=0; $p<count($hp); $p++){ //this ok can get count of $hp = 3
    for($i=0; $i<count($$hp[$p]); $i++){ //this ok can get count of type from $hp[index], if samsung = 3, if iphone= 4, if blackberry = 4
        for($u=0; $u<count($$hp[$p][$i]); $u++){ //this fail to get count the next level of hp type, should be if samsung[0] = 3, samsung[1] = 2, samsung[2]=4 
            //any code here
        }
   }
}

As IMSoP suggested, you may want to use more detailed variable names that aren't one letter to avoid future mistakes.

正如IMSoP建议的那样,您可能希望使用不是一个字母的更详细的变量名来避免将来出现错误。