16

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.

2
  • you cannot put an if inside an array like that. why not just do it after your initial assignment? Commented Nov 30, 2011 at 17:06
  • same problem here stackoverflow.com/questions/4118875/… Commented Nov 30, 2011 at 17:08

8 Answers 8

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')
Sign up to request clarification or add additional context in comments.

1 Comment

8

Maybe this one?

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

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

$multiarray[] = $array;

Comments

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

1 Comment

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.
2

You can define the array and then add some items:

$des = array(...);
if(...)
   $des["..."] = "...";

Comments

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'];
}

4 Comments

what is the @$product_option_value_description_query->row['smallimage'], mean
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 shown
Note however that if 'Scream' is enabled the suppression will be ignored.
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'];
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;

Comments

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'],
}

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.