dongshan0202405 2016-01-18 06:49
浏览 25
已采纳

将字符串,变量和数组索引连接到一个变量

I'm trying to do something out of my league, but I don't see how else I can achieve it.

I have two array variables.

<?php
$mobile_eu = array("alb", "bul");
$m_alb = array(array("Albanian"), array("test@test.com"),
    array("test2@test.com", "test3@test.com");
?>

What I'm trying to do is, get alb from $mobile_eu, prepend a string to the value, and then access $m_alb from it with [0][0] index. Something like...

function createLink($category) {
  foreach ($category as $short) {
  $langcode = "\$m_" . $short . [0][0];
  }
}

and yes, it doesn't work. When I dumped it, all it shows is

string 'alb0' (length=4)

What I hope to achieve here is to get

string 'Albanian' (length=8)

Is there any way to get this?

Any help would be great. Thanks.

  • 写回答

2条回答 默认 最新

  • dty47696 2016-01-18 06:54
    关注
    <?php
    $mobile_eu = array("alb", "bul");
    $m_alb = array(array("Albanian"), array("test@test.com"),
        array("test2@test.com", "test3@test.com"));
    
    
    echo ${'m_'.$mobile_eu[0]}[0][0];
    

    prints Albania.

    see also: http://docs.php.net/language.variables.variable


    But I think you should structure your data in another way, so you don't need something like this. E.g. instead of having a variable $m_alb and another $m_bul you could have an array where 'alb' and 'bul' are keys/elements:

    <?php
    createLinks( array('alb','bul') );
    
    function createLinks($regions) {
        static $data = array ( /* using static only as an example for "some data source" */
            'alb' => array(
                array("Albanian"), /* it would certainly be nice if you could give */
                array("test@test.com"), /* those elements key-names with some meaning */
                array("test2@test.com", "test3@test.com")
            ),
            'bul' => array(
                array("Bulgaria"),
                array("test5@test.com"),
                array("test6@test.com", "test7@test.com")
            ),
        );
    
        foreach( $regions as $r ) {
            echo $data[$r][0][0], "<br />
    ";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题