Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement within an array declaration ...is that possible? [duplicate]

Possible Duplicate:
A conditional element inside an array(…) construct

Here is my code

$product_option_value_data[] = array(
    'product_option_value_id' => $product_option_value['product_option_value_id'],
    if (isset($product_option_value_description_query->row['smallimage'])) {
        'smallimage'                  => $product_option_value_description_query->row['smallimage'],
    }
    'name'                    => $product_option_value_description_query->row['name'],
    'price'                   => $product_option_value['price'],
    'prefix'                  => $product_option_value['prefix']
);

can i do something like this....

here is my error

  Parse error: syntax error, unexpected T_IF, expecting ')' in /Users/mattelhotiby/Sites/posnation/shop_pos/catalog/model/catalog/product.php on line 419 

Actually i did this

if (isset($product_option_value_description_query->row['smallimage'])) {
    $smallimage = $product_option_value_description_query->row['smallimage'];
}else{
    $smallimage = '';
}
$product_option_value_data[] = array(
    'product_option_value_id' => $product_option_value['product_option_value_id'],
    'smallimage'                  => $smallimage,
    'name'                    => $product_option_value_description_query->row['name'],
    'price'                   => $product_option_value['price'],
    'prefix'                  => $product_option_value['prefix']
);

But i still want to know f there is a way to do an if within this array declaration

like image 693
Matt Elhotiby Avatar asked Nov 30 '11 17:11

Matt Elhotiby


People also ask

Can I put an if statement in an array?

If that array is at that first state of [1], and "one". equals(match) then it sets the array to arrayCount[2] and then from there on. Basically, if "one" = match, it should set arrayCount to 2, if "two" = match AND the first if statement has already been executed, it will play the test sound.

How do you write an if else condition in an array?

php $LibraryStatus='true' $arrLayout = array( "section1" => array( if $LibraryStatus='true' ( "wLibrary" => array( "title" => "XBMC Library", "display" => "" ), else blank. if $ControlStatus='true' ( "wControl" => array( "title" => "Control", "display" => "" ) ) ) ?>

How do you check if value exists in array of objects PHP?

The function in_array() returns true if an item exists in an array. You can also use the function array_search() to get the key of a specific item in an array.


2 Answers

Not an if, but a similar thing is possible:

$product_option_value_data[] = array(
    'product_option_value_id' => $product_option_value['product_option_value_id'],
    'smallimage' => (isset($product_option_value_description_query->row['smallimage'])) ?
        $product_option_value_description_query->row['smallimage'] : null,
    'name'                    => $product_option_value_description_query->row['name'],
    'price'                   => $product_option_value['price'],
    'prefix'                  => $product_option_value['prefix']
);

Syntax:

(<statement> ? <case: true> : <case: false>)
(1 == 1 ? 'yes!' : 'PHP is wrong')
like image 79
Tom van der Woerdt Avatar answered Sep 18 '22 17:09

Tom van der Woerdt


Maybe this one?

$array = array(
    'key1' => 'value1',
    'key2' => 'value2',
);

if (isset(...)) {
    $array['key3'] = 'value3';
}

$multiarray[] = $array;
like image 31
balkon_smoke Avatar answered Sep 19 '22 17:09

balkon_smoke