如何返回数组"名称"作为一个字符串

时间:2016-12-27 06:40:57

标签: php

$myAry = array();

// I want to return the array's name.

// something like
echo $myAry.name;

// Should print "$myAry".

谢谢,

3 个答案:

答案 0 :(得分:0)

但是如果你输入$myAry,你就已经知道了变量的名称。什么是 你想要实现它?

如果你绝对必须知道变量的身份......

function my_function ($my_array) {
    $caller_info = array_shift(debug_backtrace());
    $lines = file($caller_info['file']);
    $line = $lines[$caller_info['line'] - 1];
    if(preg_match('/my_function\\s*\\(\$(\\w+)/', $line, $matches)) {
        $name_of_my_array = $matches[1];
        echo $name_of_my_array;
    }

}

这是你想要的吗?

答案 1 :(得分:0)

你可以得到这样的参数名称:

function get_function ($my_array) {
    $f = new ReflectionFunction($my_array);
    $result = array();
    foreach ($f->getParameters() as $param) {
        $result[] = $param->name;   
    }
    return $result;

}

function sayHello($helloWorlds){
    echo $helloWorld;
}

print_r(get_function('sayHello'));;


 //Output
 Array ( [0] => helloWorlds ) 

答案 2 :(得分:0)

$ array = array('name12');  $ comma_separated = implode(“,”,$ array);

echo $ comma_separated; // name12

//使用空数组时清空字符串:

var_dump(implode('hello',array())); // string(0)“”