0

我有一个大foreach循环,里面有其他循环和条件。在我正在修改的代码的顶部,我希望从option_data数组中取出一个值 - 这是如何完成的?

foreach ($this->cart->getProducts() as $product) {

    $option_data = array();
    foreach ($product['option'] as $option) {
        if ($option['type'] != 'file') {
            $value = $option['option_value'];   
        } else {
            $filename = $this->encryption->decrypt($option['option_value']);

            $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
        }               
        $option_data[] = array(     
            'cid'       => $option['option_value_id'], // WANT THIS CID VALUE                          
            'name'  => $option['name'],
            'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
            'type'  => $option['type']
        );
    }


    $sql_get_colour_image=mysql_query("
    SELECT
        imagefb_url
    FROM
        `ocm1__product_image_fb`
    WHERE 
        `pid`=`".$product['product_id']."`
    AND
        `cid`= //GOES HERE
    ");
4

1 回答 1

1

你可以试试这个:

    $option_data = array();
    foreach ($product['option'] as $option) {
    if ($option['type'] != 'file') {
        $value = $option['option_value'];   
    } else {
        $filename = $this->encryption->decrypt($option['option_value']);

        $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
    }               
    $option_data[] = array(     
        'cid'       => $option['option_value_id'], // WANT THIS CID VALUE                          
        'name'  => $option['name'],
        'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
        'type'  => $option['type']
    );
    $sql_get_colour_image=mysql_query("
        SELECT
          imagefb_url
        FROM
          `ocm1__product_image_fb`
        WHERE 
          `pid`=".$product['product_id']."
          AND `cid`= ".$option['option_value_id']."
    ");
  }

替代您可以:

$option_data = array();
$cids = array();
foreach ($product['option'] as $option) {
    if ($option['type'] != 'file') {
        $value = $option['option_value'];   
    } else {
        $filename = $this->encryption->decrypt($option['option_value']);

        $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
    }               
    $option_data[] = array(     
        'cid'       => $option['option_value_id'], // WANT THIS CID VALUE                          
        'name'  => $option['name'],
        'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
        'type'  => $option['type']
    );
    $cids[] = $option['option_value_id'];
}
$sql_get_colour_image=mysql_query("
    SELECT
        imagefb_url
    FROM
        `ocm1__product_image_fb`
    WHERE 
        `pid`=".$product['product_id']."
    AND
        `cid` IN (".implode(",",$cids).")
");
于 2013-07-04T08:45:53.703 回答