在json_decode之后无法获取数组键

时间:2016-01-26 02:55:52

标签: php html arrays json key

我json_decode编了一个数组,甚至以为我可以print_r它,我不能称它为键。我不是专业人士也不是什么,所以欢迎任何帮助。我尝试了很多东西,但我无法让它发挥作用。

我的目标是以易于阅读的方式呈现数组结果,因此我想在HTML部分中访问它,而不仅仅是要打印。

我在print_r之后获得的数组是:Array([0] => stdClass Object([pda] => 41.52258509711 [upa] => 51.345212698165 [uu] => www.chevrolet.com.ar /))

        string SizeName = "";
        double SizeCost = 0;
        string ToppingName = "";
        double ToppingCost = 0;
        double TotalCost = (SizeCost + ToppingCost);
        double Tax = (TotalCost*0.085);
        double TotalWithTax = (TotalCost + Tax);

        //Print a greeting message.
        Console.WriteLine("Welcome to the Central Pizza Parlor!");

        //Ask if the customer would like to order a pizza.
        Console.WriteLine("Would you like to order a pizza today? Enter y for Yes or n for No.");
        string Order = Console.ReadLine();

        //Start the order if answer is Yes, if not, then exit the program.
        if (Order == "y")
        {
            //Continue with order.
            Console.WriteLine("Great! Let's get started, please pick the size of your pizza:");
            Console.WriteLine("1 - Small  $5.00");
            Console.WriteLine("2 - Medium $7.00");
            Console.WriteLine("3 - Large  $9.00");

            //Get pizza size for order.
            Console.WriteLine("Please enter the number for the pizza size you would like.");
            string sizeAsAString = Console.ReadLine();
            int size = Convert.ToInt32(sizeAsAString);


            //Use If Else statement to set the variable value for SizeCost.
            if (size == 1)
            {
                SizeCost = 5.0;
                SizeName = ("Small");
            }
            else if (size == 2)
            {
                SizeCost = 7.0;
                SizeName = ("Medium");
            }
            else if (size == 3)
            {
                SizeCost = 9.0;
                SizeName = ("Large");
            }

            //Have Customer select toppings.
            Console.WriteLine("Please select which topping you would like on your pizza.");
            ;
            Console.WriteLine("1 - Pepperoni     $2.00");
            Console.WriteLine("2 - Ham           $2.00");
            Console.WriteLine("3 - Onions        $1.00");
            Console.WriteLine("4 - Mushrooms     $1.00");
            Console.WriteLine("5 - Green Peppers $1.00");

            Console.WriteLine("Please enter the number for the corresponding topping you would like.");
            string toppingAsAString = Console.ReadLine();
            int topping = Convert.ToInt32(toppingAsAString);


            //Use If Else statement to set the variable value for ToppingCost.
            if (topping == 1) 
            {
                ToppingCost = 2.0;
                ToppingName = ("Pepperoni");
            }
            else if (topping == 2)
            {
                ToppingCost = 2.0;
                ToppingName = ("Ham");
            }
            else if (topping == 3)
            {
                ToppingCost = 1.0;
                ToppingName = ("Onions");
            }
            else if (topping == 4)
            {
                ToppingCost = 1.0;
                ToppingName = ("Mushrooms");
            }
            else if (topping == 5)
            {
                ToppingCost = 1.0;
                ToppingName = "Green Peppers";
            }

            //Display order details.
            Console.WriteLine("Here are the details for your order.");
            Console.WriteLine("Thank you for your business!");
            Console.WriteLine("You can pick up your pizza in 25 minutes!");

            //Show current time of order.
            DateTime now = DateTime.Now;
            Console.WriteLine("Time Ordered: "+now+" ");

            //Show Current time of order with additional 25 minutes for pickup.
            DateTime pickup = DateTime.Now.AddMinutes(25);
            Console.WriteLine("Pick Up At: "+pickup+" ");

            //Output Pizza Size.
            Console.WriteLine("Size: " +SizeName+ "  ");

            //OutPut Topping name.
            Console.WriteLine("Topping: " +ToppingName+ " ");

            Console.WriteLine("---------------");

            //Output total price of size and topping chosen.
            Console.WriteLine("Pizza Price: $ "+TotalCost+" ");

            //Output tax amount.
            Console.WriteLine("Tax: $" +Tax+ " ");

            //Output total price with tax.
            Console.WriteLine("Total Price: $" +TotalWithTax+ "  ");

        }
        else 
        {
            //Exit the program because the customer does not want to order a pizza.
            Console.WriteLine("Alright, have a great day!");
        }

        Console.ReadLine();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

只需更改$ contents = json_decode($ content);

到$ contents = json_decode($ content,true);所以它可以是一个数组。

然后用:$ pageAuthority = $ contents [0] [" upa"];

而不是:$ pageAuthority = $ contents-> upa;