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

问题描述:

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.

我正试图在联盟中做点什么,但我不知道我怎么能做到这一点 。 p>

我有两个数组变量。 p>

 &lt;?php 
 $ mobile_eu = array(“alb”,“bul”  ); 
 $ m_alb = array(array(“Albanian”),array(“test@test.com”),
 array(“test2@test.com”,“test3@test.com”); 
  ?&gt; 
  code>  pre> 
 
 

我要做的是,从 $ mobile_eu code>获取 alb code>,prepend 该值的字符串,然后使用 [0] [0] code>索引从中访问 $ m_alb code>。类似于...... p> function createLink($ category){ foreach($ category as $ short){ $ langcode =“\ $ m_”。$ short。[0] [0]; } } code> pre>

是的,它不起作用。当我转储它时,显示的只是 p>

  string  'alb0'(长度= 4)
  code>  pre> 
 
 

我希望在这里实现 p>

  string' 阿尔巴尼亚语'(长度= 8)
  code>  pre> 
 
 

有没有办法得到这个? p> 任何帮助都会很棒。 谢谢。 p> div>

<?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 />
";
    }
}

Might be this will helps you.

Solution : I have made some modifications in your createlink function Kindly check those as below:

function createLink($category) {
  foreach ($category as $short) {
    global $m_alb;
    $arrayName = "m_" . $short;
    $newArray = $$arrayName;
    var_dump($newArray[0][0]);
  }
}

I have made $m_alb array as the global because the with out this we will get an Notice: Undefined variable: m_alb. As the $m_alb array is not in the scope of createlink function. Now you can call your createlink function and check the result.

Hope this will works for you.