如何在不调用函数的情况下在单个数组中获得两个结果集数组?

时间:2017-02-20 06:35:29

标签: php arrays loops

这里我通过调用单个函数来激活两个查询..

$category  = [];
$sub_category = [];

$query_parent_category = "select name from category where parent_id = 0";
$query_child_category="select name from category where parent_id = 1";
$category = $this->runSelectQuery($query_parent_categoty);
$sub_category = $this->runSelectQuery($query_child_categoty);

return array($category,$sub_category);

函数runSelectQuery就像......

 public function runSelectQuery($sql) {
        $result = mysqli_query($this->con, $sql);
        $listinfdata = array();

        while ($row = mysqli_fetch_assoc($result)) {
            $listinfdata[] = $row;
        }

        return $listinfdata;
    }

但它不是lool优化..我需要单个函数调用中的两个数组..这意味着单个数组中的$category[]$sub_category[] ..结果就像是

Array
(
    [0] => Array
        (
            [name] => MEN
            [subcat] => Array
                (
                    [0] => Jeans
                    [1] => Shirts
                    [2] => T-shirts
                    [3] => Chinos
                    [4] => Blazers
                    [5] => Night Wear
                )

        )
) 

依旧......

3 个答案:

答案 0 :(得分:2)

更多答案......

如果您有超过1个父类别

,那该怎么办? 像这样

enter image description here

使用此查询

SELECT `parent_category`.`id`, `parent_category`.`name` as `parent_category`, `child_category`.`name` as `child_category` FROM `category` `parent_category` join `category` `child_category` on `parent_category`.`id` = `child_category`.`parent_id`

此查询产生类似

的结果

enter image description here

首先对您的类别进行分组

声明一个包含$row

的parent_id的变量
$prev_parent = null;

让它为null

然后

in while循环

创建3个条件

第一个条件:检查$prev_parent是否为空,然后创建第一个类别

if ($prev_parent == null) {
    $prev_parent = $row['id'];
    $listinfdata[] = array(
        'name' => $row['parent_category'],
        'subcat' => array($row['child_category'])
    );
}

这种情况会产生类似这样的东西

Array
(
    [0] => Array
        (
            [name] => 'Parent Category 1'
            [subcat] => Array
                (
                    [0] => 'Child Category 1'
                )
        )
);

第二个条件:检查$row['id']或parent_id是否与您之前的父ID类别的$ prev_parent相同

如果与此条件匹配,只需将子类别插入最新父类别的最新子类别

`sizeof($listinfdata)-1` since array always started at index 0

这样做

$prev_parent = $row['id'];
$listinfdata[sizeof($listinfdata)-1]['subcat'][] = $row['child_category'];

这种情况会产生类似这样的东西

Array
(
    [0] => Array
        (
            [name] => 'Parent Category 1'
            [subcat] => Array
                (
                    [0] => 'Child Category 1',
                    [1] => 'Child Category 2'
                )
        )
);

上次条件else表示$prev_parent不为空,但当前的parent_category与 $prev_parent插入另一个父类别及其第一个子类别

$prev_parent = $row['id'];
$listinfdata[] = array(
    'name' => $row['parent_category'],
    'subcat' => array($row['child_category'])
);

这种情况会产生类似这样的东西

Array
(
    [0] => Array
        (
            [name] => 'Parent Category 1'
            [subcat] => Array
                (
                    [0] => 'Child Category 1',
                    [1] => 'Child Category 2'
                )
        ),
    [1] => Array
        (
            [name] => 'Parent Category 2'
            [subcat] => Array
                (
                    [0] => 'Child Category 1',
                )
        )
);

所以你的代码就像这样

<?php

    $category  = [];
    $sub_category = [];
    $query_category = "SELECT `parent_category`.`id`, `parent_category`.`name` as `parent_category`, `child_category`.`name` as `child_category` FROM `category` `parent_category` join `category` `child_category` on `parent_category`.`id` = `child_category`.`parent_id`";

    $sub_category = $this->runSelectQuery($query_category);

    return array($sub_category);


function `runSelectQuery` is like...

public function runSelectQuery($sql) {
    $result = mysqli_query($this->con, $sql);
    $listinfdata = array();

    $prev_parent = null;
    while ($row = mysqli_fetch_assoc($result)) {
        $category = null;
        if ($prev_parent == null) {
            $prev_parent = $row['id'];
            $listinfdata[] = array(
                'name' => $row['parent_category'],
                'subcat' => array($row['child_category'])
            );
        } else if ($row['id'] == $prev_parent) {
            $prev_parent = $row['id'];
            $listinfdata[sizeof($listinfdata)-1]['subcat'][] = $row['child_category'];
        } else {
            $prev_parent = $row['id'];
            $listinfdata[] = array(
                'name' => $row['parent_category'],
                'subcat' => array($row['child_category'])
            );
        }
    }

    return $listinfdata;
}

结果就像这样

enter image description here

答案 1 :(得分:0)

为此,您可以使用&#34; OR&#34;修改您的查询。操作

$category  = [];
$query_parent_categoty = "select name from category where parent_id = 0 OR parent_id = 1";
$category = $this->runSelectQuery($query_parent_categoty);
 return array($category);

答案 2 :(得分:0)

就像@beginner评论一样,使用IN优于OR

$query = "SELECT name FROM category WHERE parent_id IN (0,1)";
$category = $this->runSelectQuery($query);