如何用PHP将数组添加到另一个数组?

41zrol4v  于 2023-04-04  发布在  PHP
关注(0)|答案(2)|浏览(131)

我想添加一组数组到现有的数组中具有相同的关键字。我可以使这些数据从db。但我不能把它们放在一起工作。

数据库

菲律宾

$stmt=$mysqli->prepare("select * from option_unit order by level asc");
$stmt->execute();
$res=$stmt->get_result();           
    while($row=$res->fetch_assoc()){
        $_parent=$row['parent'];            
                //child
                $_chl=$mysqli->prepare("select * from option_unit where parent=?");
                $_chl->bind_param('i',$_parent);
                $_chl->execute();
                $res1=$_chl->get_result();
                while($row1=$res1->fetch_assoc()){
                    $chld[]=array("head"=>$row['title'],"id"=>$row['id'],"contents"=>$row['title'],"children"=>"");
                }                   
                $_chl->close();                 
                
                $data[]=array("head"=>$row['title'],"id"=>$row['id'],"contents"=>$row['title'],"children"=>$chld);
        
            }//while

$stmt->close();
echo json_encode($data,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

JSON(结果重复上百行)

但我期望的结果是:

[
    {
        "head": "CEO",
        "id": 24,
        "contents": "CEO",
        "children": [
            {
                "head": "Accountant",
                "id": 2,
                "contents": "CEO",
                "children": [{ "head": "Manager", "id": "25", "contents": "Mr.Bill" }]
            },{
                "head": "HR",
                "id": 9,
                "contents": "CEO",
                "children": ""
            }
    }
    ]

我想一定是加法数组出了问题。但我不知道是什么。如果我知道这个。另一层的孩子会更容易。

ohtdti5x

ohtdti5x1#

你永远不会声明$chld,也不会重置它。

$stmt=$mysqli->prepare("select * from option_unit order by level asc");
$stmt->execute();
$res = $stmt->get_result();

while ($row=$res->fetch_assoc()) {
    $_parent = $row['parent'];
    //child
    $_chl = $mysqli->prepare("select * from option_unit where parent=?");
    $_chl->bind_param('i', $_parent);
    $_chl->execute();
    $res1 = $_chl->get_result();
    $chld = [];
    while ($row1 = $res1->fetch_assoc()) {
        $chld[]=[
            "head" => $row1['title'],
            "id" => $row1['id'],
            "contents" => $row1['title'],
            "children" => ""];
    }
    $_chl->close();

    $data[]=[
        "head" => $row['title'],
        "id" => $row['id'],
        "contents" => $row['title'],
        "children" => $chld];
}//while

$stmt->close();
echo json_encode($data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
hwamh0ep

hwamh0ep2#

你可以用这种方式创建递归函数,这样它就可以管理你所期望的所有级别。

function get_data($mysqli, $parent=0)
{
    if($parent === 0) {
        $stmt=$mysqli->prepare("select * from option_unit where parent=0 order by level asc");
    } else {
        $stmt = $mysqli->prepare("select * from option_unit where parent=?");
        $stmt->bind_param('i',$_parent);
    }

    $stmt->execute();
    $data = [];
    $res=$stmt->get_result();           
    while($row=$res->fetch_assoc()){
        $_parent=$row['id'];
        $child = get_data($mysqli, $_parent); 
        $data[]=array("head"=>$row['title'],"id"=>$row['id'],"contents"=>$row['title'],"children"=>$child);
    }
    $stmt->close();
    return $data;
}

$final_data = get_data($mysqli);
echo json_encode($final_data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

相关问题