我们可以将括号存储到PHP变量中以获取数组的值吗?

时间:2014-02-07 01:28:55

标签: php arrays variables brackets square-bracket

我想将方括号字符串存储在变量中,以便我可以使用该变量来获取数组的值。我怎么能这样做?

示例:

$vehicle = array("car"=>"volvo","bike"=>"polygon");

$bracket1="['car']";

$bracket2="['bike']";

echo $vehicle.$bracket1;//my expected result = 'volvo';

echo $vehicle.$bracket2;//my expected result = 'polygon';

案例 假设我有这个数据

$data = array(
"vehicles"=>array(
    array(
        "name"=>"volvo",
        "manufacturer"=>"abc",
        "color"=>array("blue"=>"wonderful","red"=>"fantastic")),
    array(
        "name"=>"toyota",
        "manufacturer"=>"def",
        "color"=>array("blue"=>"awesome","red"=>"good")),
    array(
        "name"=>"mecedes",
        "manufacturer"=>"ghi",
        "color"=>array("blue"=>"nice","red"=>"great","green"=>"good","brown"=>"elegant")),
));

$fields  = array(
    "$data['vehicles']['name']",
    "$data['vehicles']['manufacturer']",
    "$data['vehicles']['color']['blue']",
    "$data['vehicles']['color']['red']"
);

//a function to print those data according to user parameter($fields, it may uncertain pattern)

function get_data($data,$fields){
    for($c=0;$c<count($data);$c++){
        foreach($fields as $field){     //field to show
            echo $field;
        }
    }
}

编辑:

$data = array(
"vehicles"=>array(
    array(
        "name"=>"volvo",
        "manufacturer"=>"abc",
        "color"=>array("blue"=>"wonderful","red"=>"fantastic")),
    array(
        "name"=>"toyota",
        "manufacturer"=>"def",
        "color"=>array("blue"=>"awesome","red"=>"good")),
    array(
        "name"=>"mecedes",
        "manufacturer"=>"ghi",
        "color"=>array("blue"=>"nice","red"=>"great","green"=>"good","brown"=>"elegant")),
));

$c=0;

$fields  = array( // note added zeros here... these are your "vehicle" array key

    "{$data['vehicles'][$c]['name']}",

    "{$data['vehicles'][$c]['manufacturer']}",

    "{$data['vehicles'][$c]['color']['blue']}",

    "{$data['vehicles'][$c]['color']['red']}"

);


for($c=0;$c<count($data['vehicles']);$c++){

    foreach($fields as $field) {

        echo $field . PHP_EOL;

    }

}

//the output print : volvo abc wonderful fantastic volvo abc wonderful fantastic volvo abc wonderful fantastic 

//the output expectetd : volvo abc wonderful fantastic toyota dev awesome good mercedes ghi nice great 

1 个答案:

答案 0 :(得分:5)

你为什么不试试这个:

$vehicle = array("car" => "volvo", "bike" => "polygon");

$bracket1 = "car";

$bracket2 = "bike";

echo $vehicle[$bracket1]; //my expected result = 'volvo';

echo $vehicle[$bracket2]; //my expected result = 'polygon';

编辑:您需要一个执行此功能的功能......您不需要它。这是一个基本的PHP语言结构。

Anywho,这是你的函数(将基本的PHP语言包含在一个额外的层中) - 我甚至会抛出一个基本的错误检查(如果数组键不存在则返回false):

function searchMyHugeArrayForSomething($huge_array, $something) {
    if(!array_key_exists($something, $huge_array))
        return false; // return false if array key doesn't exist
    return $huge_array[$something]; // return your desired result......
}

演示:

$my_huge_array = array(
    'pet' => 'cat',
    'wife' => 'sarah',
    'brother' => 'john'
    // etc
);

echo searchMyHugeArrayForSomething($my_huge_array, 'wife'); // sarah
echo searchMyHugeArrayForSomething($my_huge_array, 'husband'); // returns false (oop, sexist!)
echo searchMyHugeArrayForSomething($my_huge_array, 'brother'); // john
// etc............?

有意义吗?

编辑:好的,多维数组非常不同(您的原始问题是平坦的)。您在此处遇到问题,因为您错过了vehiclesname之间的等级,等等。这里有一个包含数字索引的数组,因此您的路径实际$data['vehicles'][0]['name'] - 在这种情况下,基本上只需echo数组的每一行就可以获得解析为字符串的数组键的值。

在这个例子中,我已经为每一行添加了两个花括号来解析为variable variable,并且因为你的例子实际上不会按原样运行(它试图将数组键解析为变量和语法失败),并添加了零数组键。您需要确定自己要定位的数组键,这只是使用第一个:

$fields  = array( // note added zeros here... these are your "vehicle" array key
    "{$data['vehicles'][0]['name']}",
    "{$data['vehicles'][0]['manufacturer']}",
    "{$data['vehicles'][0]['color']['blue']}",
    "{$data['vehicles'][0]['color']['red']}"
);

foreach($fields as $field) {
    echo $field . PHP_EOL;
}

<强>输出:

volvo
abc
wonderful
fantastic