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

8

15

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 if there is a way to do an if within this array declaration.

Ilene answered 30/11, 2011 at 17:2 Comment(2)
you cannot put an if inside an array like that. why not just do it after your initial assignment?Chuckwalla
same problem here #4119375Damage
D
31

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')
Dree answered 30/11, 2011 at 17:5 Comment(1)
PHP docs: ternary operatorLook
C
8

Maybe this one?

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

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

$multiarray[] = $array;
Cabezon answered 30/11, 2011 at 17:6 Comment(0)
S
2

In this case only possible option is to use following syntax:

'smallimage' => (isset($product_option_value_description_query->row['smallimage']) 
  ? isset($product_option_value_description_query->row['smallimage'])
    : NULL)

Though this has side effect, if your condition fails you will have "smallimage" key with value of NULL

Stillborn answered 30/11, 2011 at 17:5 Comment(1)
I did like this answer, but the 'true' value of the ternary (here, "isset($product_option_value_description_query->row['smallimage'])" displayed a value of 1, for true. Change that to "$product_option_value_description_query->row['smallimage']" if you want the displayed value to be smallimage.Francenefrances
M
2

You can define the array and then add some items:

$des = array(...);
if(...)
   $des["..."] = "...";
Mesmerize answered 30/11, 2011 at 17:6 Comment(1)
This worked great - thanks!!!Fougere
L
1

No, You can do it inline or externaly:

$product_option_value_data[] = array(
    'product_option_value_id' => $product_option_value['product_option_value_id'],
    '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']
);

Or if smallimage cannot be empty:

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

if (isset($product_option_value_description_query->row['smallimage'])) {
    $product_option_value_data['smallimage'] = $product_option_value_description_query->row['smallimage'];
}
Laurice answered 30/11, 2011 at 17:6 Comment(4)
what is the @$product_option_value_description_query->row['smallimage'], meanIlene
Lines prefixed by @ will not raise errors when notices occurs. So, if smallimage is not present in $product_option_value_description_query->row array, it will return null and no notice error will be shownLaurice
Note however that if 'Scream' is enabled the suppression will be ignored.Quartering
For multi dimensional array do add the position to append the value i.e. $product_option_value_data[sizeof($product_option_value_data-1)]['smallimage'] = $product_option_value_description_query->row['smallimage'];Seraphina
M
0

As far as I know, no. But why would you even do that, that is a bad idea. You should only be setting variables in an array, do you logic outside.

$data = array(
    'product_option_value_id' => $product_option_value['product_option_value_id'],
    'name'                    => $product_option_value_description_query->row['name'],
    'price'                   => $product_option_value['price'],
    'prefix'                  => $product_option_value['prefix']
);

if (isset($product_option_value_description_query->row['smallimage'])) {
    $data['small_image'] = $product_option_value_description_query->row['smallimage'];
}

$product_option_value_data[] = $data;
Moussaka answered 30/11, 2011 at 17:6 Comment(0)
V
0

NO. Simple as that.

Do:

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

if (isset($product_option_value_description_query->row['smallimage'])) {
     $product_option_value_data[count($product_option_value_data) - 1]['smallimage'] = $product_option_value_description_query->row['smallimage'],
     // I'm not sure if you meant to have that [] in your declaration above
     // You may need to drop it, in which case the line would be:
     // $product_option_value_data['smallimage'] = $product_option_value_description_query->row['smallimage'],
}
Valerianaceous answered 30/11, 2011 at 17:7 Comment(0)
S
0

Even if you could do that it is much more readable to add the conditional after or before the array.

Someplace answered 30/11, 2011 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.