Welcome Guest, Not a member yet? Register   Sign In
Help with Looping through an object
#1

Hi all,
i have a JSON that looks similar to one shown below:
Code:
products: [
    {
        id: "D-MFIN-2-150MB",
        pur_currency: "NN",
        topup_amount: 200,
        price: 200,
        data_amount: "150"
    },
    {
        id: "D-MFIN-2-1",
        pur_currency: "NN",
        topup_amount: 1000,
        price: 1000,
        data_amount: "1000"
    },
    {
        id: "D-MFIN-2-1.5",
        pur_currency: "NN",
        topup_amount: 1200,
        price: 1200,
        data_amount: "1500"
    },
    {
        id: "D-MFIN-2-2.5",
        pur_currency: "NN",
        topup_amount: 2000,
        price: 2000,
        data_amount: "2500"
    },
    {
        id: "D-MFIN-2-4",
        pur_currency: "NN",
        topup_amount: 3000,
        price: 3000,
        data_amount: "4000"
    },
    {
        id: "D-MFIN-2-11.5",
        pur_currency: "NN",
        topup_amount: 8000,
        price: 8000,
        data_amount: "11500"
    },
    {
        id: "D-MFIN-2-15",
        pur_currency: "NN",
        topup_amount: 10000,
        price: 10000,
        data_amount: "15000"
    },
    {
        id: "D-MFIN-2-27.5",
        pur_currency: "NN",
        topup_amount: 18000,
        price: 18000,
        data_amount: "27500"
    },
    {
        id: "D-MFIN-2-30",
        pur_currency: "NN",
        topup_amount: 27500,
        price: 27500,
        data_amount: "30000"
    },
    {
        id: "D-MFIN-2-60",
        pur_currency: "NN",
        topup_amount: 55000,
        price: 55000,
        data_amount: "60000"
    },
    {
        id: "D-MFIN-2-100",
        pur_currency: "NN",
        topup_amount: 84992,
        price: 84992,
        data_amount: "100000"
    },
    {
        id: "D-MFIN-2-120",
        pur_currency: "NN",
        topup_amount: 110000,
        price: 110000,
        data_amount: "120000"
    },
    {
        id: "D-MFIN-2-500MB",
        pur_currency: "NN",
        topup_amount: 500,
        price: 500,
        data_amount: "500"
    },
    {
        id: "D-MFIN-2-5.5",
        pur_currency: "NN",
        topup_amount: 4000,
        price: 4000,
        data_amount: "5500"
    },
    {
        id: "D-MFIN-2-40MB",
        pur_currency: "NN",
        topup_amount: 100,
        price: 100,
        data_amount: "40"
    }
]

I want to loop through it and display in a select tag. I've failed to do that so far.
To test I converted the JSON to PHP objects using $list = json_decode), then:
PHP Code:
for ($i=0$i count($list); $i++) { 
echo 
"Item no. {$i} is {$list->$i->$id} <br>";

That gives an error:
Code:
A PHP Error was encountered
Severity: Notice

Message: Trying to get property of non-object
Next, id did
PHP Code:
for ($i=0$i count($list); $i++) { 
echo 
"Item no. {$i} is {$list[$i]['id']} <br>";

This was worse:
Code:
An uncaught Exception was encountered
Type: Error

Message: Cannot use object of type stdClass as array
I was able ot do this:
PHP Code:
for ($i=0$i count($list); $i++) { 
echo 
"Item no. {$i} <br>";

to just count the object.

My question, how can I get id, pur_currency, etc... from the $list variable.
Your answer is appreciated.
Reply


Messages In This Thread
Help with Looping through an object - by enelson - 11-10-2018, 11:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB