选择条件数组 In PHP

Selection Condition Array In PHP

我有 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] => 数组 ( [编号] => 1 [名称] => 互联网 [0] => 阵列 ( [属性名称] => 下载 [属性值] => 2048 )

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

        )

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

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

但我只想为 internet 显示 attributeNameattributeValue.. 我试图在 while 下面添加 if($data['NAME'][0]='INTERNET'),但它没有不行,VOICE还有attributeNameattributeValue。请帮忙。谢谢

因此,对于您的数据,您有多种组织方式:

第一次检查 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'];
    } 

第 2 次检查当前父名称,因此:

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

和第 3 - 您可以修复您的查询以获取 INTERNET 行的下载和上传权限。如果你真的需要,我可以帮你。