zyeu
zyeu

Reputation: 45

foreach to variable

I must do foreach array:

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

)

and get "value" to variable $value. How i can do ?

i have :

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

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

echo $key;

}

}

but is not good i have "news_number10".

Upvotes: 0

Views: 124

Answers (4)

Kevin Nielsen
Kevin Nielsen

Reputation: 4433

try this:

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

If you're looking for a specific variable in your configuration data, you could simply do this:

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

Upvotes: 1

Baba
Baba

Reputation: 95101

try

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

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

Output

 news_number 10

Upvotes: 0

SmileyWar
SmileyWar

Reputation: 25

Can't you do it assigning a numnber to the array?

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

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

echo $key;

}

}

Are you intending to have just one value? then just change the array number to the array you want to use in the foreach.

Upvotes: 0

hakre
hakre

Reputation: 197659

Or try that:

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

Upvotes: 1

Related Questions