dsfsda121545 2014-10-19 16:43
浏览 214
已采纳

Php如何在变量名中包含另一个变量

In the example are two arrays, but actually may be other number of arrays (i do not know how many arrays i may have).

$some_name = array ( "Volvo_0",220,180, );
$array_name_for_variable[]='some_name';//here i create another array latter to loop through

$another_name = array ( "Volvo_1",221,181, );
$array_name_for_variable[]='another_name';

In the example i just want to print_r the arrays i may have. So i loop through $array_name_for_variable. Like

foreach( $array_name_for_variable as $i_array_name_for_variable => $val_array_name_for_variable ) {

trying to print particular array (like print_r($some_name)), using this

echo '<pre>', print_r($['val_array_name_for_variable'], true), '</pre> $val_array_name_for_variable __<br/>';

but see error Parse error: syntax error, unexpected '[', expecting T_VARIABLE or '$'

}

This print_r($['val_array_name_for_variable']) is wrong. Tried this print_r( $[$val_array_name_for_variable] );. Also got error.

Any ideas what need to change.

Why all this and what i need...

I have 12 arrays, but i do not know which of 12 would be used in one particular page.

So page loads, some of the 12 arrays are defined (used).

I may write like if array_1 exists, then long html code using variables from the array_1.

Then again if array_2 exists and not empty, then repeat the same html code with variables from array_2.

But instead of copy-paste (repeating) html code i decided to loop through arrays existing in opened page and the long html code write only once.

  • 写回答

2条回答 默认 最新

  • doubi7739 2014-10-19 16:57
    关注

    You could solve the problem with:

    $some_name = array ( "Volvo_0",220,180, );
    $array_name_for_variable[]=$some_name;
    
    $another_name = array ( "Volvo_1",221,181, );
    $array_name_for_variable[]=$another_name;
    

    then iterate through it:

    foreach( $array_name_for_variable as $i_array_name_for_variable => $val_array_name_for_variable ) {
    echo '<pre>', print_r($val_array_name_for_variable, true), '</pre><br/>';
    }
    

    This will print all the elements in $array_name_for_variable.

    Comment if you need any more changes to the output.

    Explanation: What the code is actually doing is iterating through all elements of the array $array_name_for_variable. They it creates a key => value for each element in it. The value ($val_array_name_for_variable) on the first iteration would be: $some_name. The key ($i_array_name_for_variable) - the element position in the array, so on the first iteration it would be 0 (as it always start from 0).

    If you don't need the element position you could do it like this:

    foreach( $array_name_for_variable as $val_array_name_for_variable ) {
    echo '<pre>', print_r($val_array_name_for_variable, true), '</pre><br/>';
    }
    

    It would generate exactly the same output as the previous code snippet.

    For adding elements to the array you have to pass a variable and you were passing just a string.

    EDIT: Based on the newly added info the code should be:

    foreach( $array_name_for_variable as $i_array_name_for_variable => $val_array_name_for_variable ) {
    if (array_key_exists($i_array_name_for_variable, $val_array_name_for_variable)) {
    echo '<pre>', print_r($val_array_name_for_variable, true), '</pre><br/>';
    } else {
    //Code if the array does not exist.
    }
    }
    

    References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目