PHP中的选择条件数组

时间:2015-01-30 01:49:57

标签: php arrays if-statement conditional conditional-statements

我有循环的PHP代码:

$explodeResult=explode(", ",$serviceName);
        foreach($explodeResult as $value)
        {
              $sql = "SELECT id,parent,code,name,xs1 as DOWNLOAD, xs2 as UPLOAD
                       FROM sc_params
                      WHERE rfen = 'SERVICE_REQUESTED'
                      AND code = :param1 
                        OR code IN
                        ( 
                            SELECT code 
                             FROM sc_params 
                             WHERE rfen = 'SERVICE_REQUESTED'
                             AND id = (
                            SELECT parent 
                                 FROM sc_params 
                                 WHERE rfen = 'SERVICE_REQUESTED' 
                                 AND code = :param1 )
                        )
                    ";          
            $stmt = oci_parse($this->_conn,$sql);
            oci_bind_by_name($stmt, ":param1", $value);
            $rs = oci_execute($stmt);
            if (!$rs) {
                $error = oci_error();
                $this->_err = $error;
                return false;
            }

$product    = Array();
$idx        = $idx2 = 0;
    while ($data = oci_fetch_array($stmt, OCI_BOTH)) {
    if($data['PARENT'] == 0) {
        $idx++;
        $product[$idx]['id']        = $data['ID'];
        $product[$idx]['name']      = $data['NAME'];
    }
    else {
        $product[$idx][0]['attributeName']   = 'DOWNLOAD';
        $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
        $product[$idx][1]['attributeName']   = 'UPLOAD';
        $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
    } 
   }
    print_r ($test_array);

来自查询的数据:

ID NAME      PARENT DOWNLOAD UPLOAD
1  INTERNET   0      null     null 
10 SPEED      1      2048     512
2  VOICE      0      null     null
12 PSTN       2      null     null

数组结果为:

阵         (             [1] =>排列                 (                 [id] => 1                 [name] =>互联网                 [0] =>排列                     (                         [attributeName] =>下载                         [attributeValue] => 2048                     )

            [1] => Array
                (
                    [attributeName] => UPLOAD
                    [attributeValue] => 512
                )

        )

    [2] => Array
        (
            [id] => 2
            [name] => VOICE
            [0] => Array
                (
                    [attributeName] => DOWNLOAD
                    [attributeValue] => 
                )

            [1] => Array
                (
                    [attributeName] => UPLOAD
                    [attributeValue] => 
                )
        )
)

但我只想展示attributeName的{​​{1}}和attributeValue ..我试图在internet下面添加if($data['NAME'][0]='INTERNET'),但它并没有#39} ; t work,while仍然有VOICEattributeName。请帮忙。感谢

1 个答案:

答案 0 :(得分:1)

因此,对于您的数据,您有很多可能性来组织它:

首先检查null

elseif (!($data['DOWNLOAD']==null && $data['UPLOAD']==null)) {
        $product[$idx][0]['attributeName']   = 'DOWNLOAD';
        $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
        $product[$idx][1]['attributeName']   = 'UPLOAD';
        $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
    } 

第二次检查当前的父名称:

if($data['PARENT'] == 0) {

    $idx++;
    $product[$idx]['id']        = $data['ID'];
    $product[$idx]['name']      = $data['NAME'];
    $curParent = $data['NAME'];
} elseif ($curParent == 'INTERNET') {

    $product[$idx][0]['attributeName']   = 'DOWNLOAD';
    $product[$idx][0]['attributeValue'] = $data['DOWNLOAD'];
    $product[$idx][1]['attributeName']   = 'UPLOAD';
    $product[$idx][1]['attributeValue'] = $data['UPLOAD'];
}

和3rd - 你可以修复你的查询以使DOWNLOAD和UPLOAD直接进入INTERNET行。如果你真的需要,我可以帮助你。