【发布时间】:2017-01-25 15:24:53
【问题描述】:
我尝试将 mysql 中的获取数据转换为 json 格式。为此,我正在使用 PHP。
我的 PHP 代码是
<?php
define('_HOST_NAME', 'localhost');
define('_DATABASE_USER_NAME', 'root');
define('_DATABASE_PASSWORD', 'admin321');
define('_DATABASE_NAME', 'tree');
$dbConnection = new mysqli(_HOST_NAME,
_DATABASE_USER_NAME, _DATABASE_PASSWORD, _DATABASE_NAME);
if ($dbConnection->connect_error) {
trigger_error('Connection
Failed: ' . $dbConnection->connect_error, E_USER_ERROR);
}
$_GLOBAL['dbConnection'] = $dbConnection;
$categoryList = categoryParentChildTree();
foreach($categoryList as $key => $value){
echo $value['name'].'<br>';
}
function categoryParentChildTree($parent = 0,
$spacing = '', $category_tree_array = '') {
global $dbConnection;
$parent = $dbConnection->real_escape_string($parent);
if (!is_array($category_tree_array))
$category_tree_array = array();
$sqlCategory = "SELECT id,name,parent FROM php WHERE parent = $parent ORDER BY id ASC";
$resCategory=$dbConnection->query($sqlCategory);
if ($resCategory->num_rows != null && $resCategory->num_rows>0) {
while($rowCategories = $resCategory->fetch_assoc()) {
$category_tree_array[] = array("id" => $rowCategories['id'], "name" => $spacing . $rowCategories['name']);
$category_tree_array = categoryParentChildTree(
$rowCategories['id'],
' '.$spacing . '- ',
$category_tree_array
);
}
}
return $category_tree_array;
}
?>
mysql 表
ID PARENT NAME
1 0 category
2 1 fruit
3 2 apple
4 2 orange
5 1 animals
6 5 tiger
7 5 lion
8 1 car
我的输出是:
category
- fruit
- - apple
- - orange
- animal
- - tiger
- - lion
- cars
我想获得嵌套的 json 输出。已经问过here。没有适当的回应。
我尝试使用 json_encode,没有得到嵌套的 json。
更新的 PHP
<?php
$con=mysqli_connect("localhost","root","admin321","tree");
if (mysqli_connect_errno()) //con error
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$r = mysqli_query($con,"SELECT * FROM php ");
$data = array();
while($row = mysqli_fetch_assoc($r)) {
$data[] = $row;
}
function buildtree($src_arr, $parent_id = 0, $tree = array())
{
foreach($src_arr as $idx => $row)
{
if($row['parent'] == $parent_id)
{
foreach($row as $k => $v)
$tree[$row['id']][$k] = $v;
unset($src_arr[$idx]);
$tree[$row['id']]['children'] = buildtree($src_arr, $row['id']);
}
}
ksort($tree);
return $tree;
}
function insertIntoNestedArray(&$array, $searchItem){
// Insert root element
if($searchItem['parent'] == 0){
array_push($array, $searchItem);
return;
}
if(empty($array)){ return; }
array_walk($array, function(&$item, $key, $searchItem){
if($item['id'] == $searchItem['parent']){
array_push($item['children'], $searchItem);
return;
}
insertIntoNestedArray($item['children'], $searchItem);
}, $searchItem);
}
$nestedArray = array();
foreach($data as $itemData){
// First: Mount the nested array item
$nestedArrayItem['id'] = $itemData['id'];
$nestedArrayItem['name'] = $itemData['name'];
$nestedArrayItem['parent'] = $itemData['parent'];
$nestedArrayItem['children'] = array();
// Second: insert the item into the nested array
insertIntoNestedArray($nestedArray, $nestedArrayItem);
}
$json = json_encode($nestedArray);
echo $json;
?>
【问题讨论】:
-
@YIVI 我在问题中提到过..请去看看..
-
同一个问题问两次不好,只会增加噪音。如果您没有收到回复,请处理您的问题。
-
上一个问题已更新