将conf文件解析为PHP树

时间:2016-03-22 01:47:32

标签: php

我想使用PHP将以下数组转换为打印输出。

public static void main(String [ ] args){


CarSim carSimUser1 = new CarSim();



System.out.println("Hello, and welcome to Car Matcher!");

Scanner scanName = new Scanner(System.in);    // user input for name

System.out.println("What's your name?");
String userName = scanName.nextLine();

System.out.println("Hello " + userName);
System.out.println("So you are looking for a new Car?");
System.out.println("Would you like 4 door sedan");
System.out.println("Or a two door coupe?");
System.out.println("Enter a number only!");



Scanner scanDoor = new Scanner(System.in);   // user input for number of doors
int numbOfDoor = scanDoor.nextInt();



while(numbOfDoor <= 4){

    if(numbOfDoor == 4){
        System.out.println("Okay, so you want " + numbOfDoor);

    }


    else if(numbOfDoor == 2){

        System.out.println("Okay, so you want " + numbOfDoor);
break;  
    }

    else{
        System.out.println("Select either 2 or 4 doors!");
        break;
}

enter code here

Array

(

    [relation] => Array

        (

            [parent.item] => Array

                (

                    [0] => p0

                    [1] => p1

                )



            [p0.item] => Array

                (

                    [0] => business

                    [1] => tourism

                )

            [business.item] => Array

                (

                    [0] => X

                    [1] => Y

                )


        )

这是我发现并根据我的需求编辑的一段代码,但我无法理解如何做到这一点......

0 p0
  + business
    - 0 X
    - 1 Y

1 个答案:

答案 0 :(得分:0)

看看这个......

<?php

    $sampleArray = array();

    $sampleArray["relation"]["parent.item"] = array("p0", "p1");
    $sampleArray["relation"]["p0.item"] = array("business", "tourism");
    $sampleArray["relation"]["business.item"] = array("x", "y");

    $value2 = $sampleArray["relation"]["parent.item"];
    foreach($value2 as $key3=>$value3){
        echo "$key3 $value3<br>";
        $types = $sampleArray["relation"][$value3.".item"];
        foreach($types as $type){
            echo " + $type<br>";
            $items = $sampleArray["relation"][$type.".item"];
            foreach($items as $index=>$item){
                echo " - $index $item<br>";
            }

            echo "<p>";
        }
    }
?>