foreach to variable

时间:2012-05-03 17:27:41

标签: php

我必须做foreach数组:

    Array
(
    [0] => Array
        (
            [name] => news_number
            [value] => 10
        )

)

并获得变量$ value的“value”。我该怎么做?

我有:

foreach($this->_config as $key){

foreach($key as $value['news_number'] => $key){

echo $key;

}

}

但不好我有“news_number10”。

4 个答案:

答案 0 :(得分:1)

试试这个:

foreach( $this->_config as $key => $data )
{
    echo $data['name']." is ".$data['value']."\n";
}

如果您在配置数据中查找特定变量,则可以执行以下操作:

foreach( $this->_config as $key => $data )
{
    if ( $data['name'] == 'news_number' )
    {
        $myNewsNumber = $data['value'];
        break;
    }
}

答案 1 :(得分:1)

或尝试:

foreach( $this->_config as $data )
{
    extract($data);
    printf("%s is %s\n", $name, $value);
}

答案 2 :(得分:0)

你能不能为数组分配一个数字?

foreach($this->_config as $key){

foreach($key as $value[0] => $key){

echo $key;

}

}

您打算只拥有一个价值吗?然后只需将数组编号更改为要在foreach中使用的数组。

答案 3 :(得分:0)

$this->_config  = Array (
        0 => Array (
                'name' => "news_number",
                'value' => 10 
        ) 
);

foreach ( $this->_config as $value ) {
    echo $value ['name'], " ", $value ['value'];
}

输出

 news_number 10