这是我的代码
$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']
);
我可以做这样的事情......
这是我的错误
Parse error: syntax error, unexpected T_IF, expecting ')' in /Users/mattelhotiby/Sites/posnation/shop_pos/catalog/model/catalog/product.php on line 419
其实我这样做了
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']
);
但是我仍然想知道在这个数组声明中有一种方法可以做一个if
答案 0 :(得分:24)
不是if,但类似的事情是可能的:
$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']
);
语法:
(<statement> ? <case: true> : <case: false>)
(1 == 1 ? 'yes!' : 'PHP is wrong')
答案 1 :(得分:3)
也许这个?
$array = array(
'key1' => 'value1',
'key2' => 'value2',
);
if (isset(...)) {
$array['key3'] = 'value3';
}
$multiarray[] = $array;
答案 2 :(得分:1)
在这种情况下,只有可能的选项是使用以下语法:
'smallimage' => (isset($product_option_value_description_query->row['smallimage'])
? isset($product_option_value_description_query->row['smallimage'])
: NULL)
虽然这有副作用,但如果你的情况失败,你会得到值为NULL的“smallimage”键
答案 3 :(得分:1)
不,您可以在线或外部进行:
$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']
);
或者如果smallimage
不能为空:
$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 :(得分:0)
您可以定义数组,然后添加一些项目:
$des = array(...);
if(...)
$des["..."] = "...";
答案 5 :(得分:0)
据我所知,没有。但为什么你会这样做,这是一个坏主意。你应该只在数组中设置变量,在外面做逻辑。
$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;
答案 6 :(得分:0)
否即可。就这么简单。
执行:
$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'],
}
答案 7 :(得分:0)
即使你能做到这一点,在数组之前或之前添加条件就更具可读性。