我有“IT / Internet / Web Development / Ajax”形式的字符串。我解析它并创建一个像
的JSON对象[{
"name": "IT",
"subcategories":[
{
"name": "Internet",
"subcategories" : [
{
"name": "Web Development",
"subcategories" : [
{
"name":"Ajax"
}]}]}]
我通过
创建JSON对象$input = "IT/Internet/Web Development";
$items = explode("/", $input);
$parent = null;
$firstObject = null;
while (count($items))
{
$object = new StdClass();
$item = array_shift($items);
$object->name = $item;
if (count($items) == 0) {
$object->subcategories=NULL; // I made this null in order to know that this is the last item of the string that comes in
}
if ($parent)
$parent->subcategories = array($object);
else
$firstObject = $object;
$parent = $object;
}
array_push($category_collection, $firstObject); //$category_collection is an array
}
当另一个字符串出现时,例如“IT / Internet / Browsers”,我希望能够解析创建的类别并将“浏览器”放在正确的位置作为Internet的子类别,那么我的JSON对象看起来像
[{
"name": "IT",
"subcategories":[
{
"name": "Internet",
"subcategories" : [
{
"name": "Web Development",
"subcategories" : [
{
"name":"Ajax"
}],
{
"name":"Browsers"
}}]}]
我在编写一个递归函数时遇到问题,该函数只是循环JSON对象以在正确的位置对所有内容进行分类。我现在正在做的是
$arrlength = count($category_collection); //count the size of the array
$input = "IT/Internet/Browsers";
$items = explode("/",$input);
$tempVariable = array_shift($items);
$flag = false;
for ($x = 0; $x < $arrlength; $x++) {
//Here I check if the first a category with that name already exists
if ($category_collection[$x]['name'] == $tempVariable) {
$flag = true;
//Now here is where im having problems doing the recursion to check if the subcategory2 already exists and then if subcategory 3 and so on...
}
}
如果有人能指引我正确的方向,那将非常感激
答案 0 :(得分:1)
请尝试这个(未经测试,可能有一些错误需要纠正,我在这里提供帮助):
function boolean check_is_good($array, $input)
{
$element = array_shift($input);
for ($x = 0; $x < count($array), $x++) {
if ($array[$x]['name'] == $element){
if ((!isset($array[$x]['subcategories']) && (count($input) == 0))
return (true);
else if ((!isset($array[$x]['subcategories']) && (count($input) != 0))
return (false);
$newArray = $array[$x]['subcategories'];
return (check_is_good($newArray, $input));
}
}
return (false);
}
函数return true全部在正确的位置
你必须在parameter1中传递一个代表你的JSON的数组(在你的例子中为$ category_collecton)
你必须在参数2中传入一个包含所有元素的数组(在你的例子中为$ items)
答案 1 :(得分:1)
这是一个完整的函数,它应该可以工作,如果需要,可以将它转换为json:
$categoriesCollection = array();
$input = "IT/Internet/Web Development";
updateCategoriesCollection(explode('/', $input), $categoriesCollection);
$input = "IT/Internet/Browsers";
updateCategoriesCollection(explode('/', $input), $categoriesCollection);
function updateCategoriesCollection(array $categoriesList, array &$categoriesCollection)
{
$name = array_shift($categoriesList);
$category = null;
foreach ($categoriesCollection as $key => $value)
{
if ($value->name == $name)
{
$category = $value;
break;
}
}
if (!$category)
{
$category = new StdClass;
$category->name = $name;
$categoriesCollection[] = $category;
}
if (!empty($categoriesList))
{
if (empty($category->subcategories)) $category->subcategories = array();
updateCategoriesCollection($categoriesList, $category->subcategories);
}
}
var_dump($categoriesCollection);
输出:
Array
(
[0] => stdClass Object
(
[name] => IT
[subcategories] => Array
(
[0] => stdClass Object
(
[name] => Internet
[subcategories] => Array
(
[0] => stdClass Object
(
[name] => Web Development
)
[1] => stdClass Object
(
[name] => Browsers
)
)
)
)
)
)