• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

#2
It should be used like this as your JSON are stored as objects inside an array.
{$list[$i]->id}

$list = array with multiple arrays.
$list[$i] = returns an object.
$list[$i]->id = returns the ID inside that object.

Personally I would just use a foreach loop instead.
PHP Code:
foreach($list as $row) {
    echo 
$row->id;


In case you don't need the position in that array.
Reply

#3
Hi,

Your json format is incorrect. json keys should be in double quotes.

It should look like this:

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"
   }
]
}

Decode as normal
Code:
$list = json_decode($json);

Your for loop would then iterate like this:

Code:
for ($i=0; $i < count($list->products); $i++) {
echo "Item no. {$i} is {$list->products[$i]->id} <br>";
}

or, as jreklund points out, foreach is the best way to iterate:

Code:
foreach ($list->products as $key => $row) {
echo "Item no. {$key} is {$row->id} <br>";
}
Reply

#4
It is faster to get the count first before running a for loop.
Otherwise you are calling the count method on each loop irritation.

PHP Code:
$count count($list->products);
for (
$i=0$i $count$i++) { 
echo 
"Item no. {$i} is {$list->products[$i]->id} <br>";

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(11-11-2018, 12:52 AM)paulkd Wrote: Hi,

Your json format is incorrect. json keys should be in double quotes.
...

I removed some part of the JSON before posting here - that inclusive.

Thanks all for your replies, they were helpful.
Reply

#6
Another issue I have is that I want to sort $list in ascending order by "data_amount".
asort(), ksort() etc are returning bool(true) instead of the $list objects - and they should be.

Any ideas as to how I can sort the $object?
Reply

#7
Do you have control of what is producing the json?

If so, maybe you can use that process to sort (e.g. SQL query)
Reply

#8
(11-11-2018, 10:43 AM)paulkd Wrote: Do you have control of what is producing the json?
No, I'am receiving it via API call
Reply

#9
You could decode the json into an associated array sort it and then encode it back to json
and then create your list.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#10
Hi,

I got this answer from https://stackoverflow.com/questions/4282...ect-fields

Code:
$list = json_decode($json);

function cmp($a, $b) {
   return strcmp($a->data_amount, $b->data_amount);
}

usort($list->products, "cmp");
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.