I have been stuck with this problem for many days now, but cannot get anything to work. I have found many conflicting posts on SO about using various methods which are much slower in php7 and there are newer, better methods to use. My target server is php 7, so that is OK.
Here is the issue I have:
object(SingleClass)#463 (8) {
["id"]=>
string(1) "1"
[created_date]=>
string(19) "2020-06-25 17:50:00"
[cricket]=>
string(27) "{"data":{"example":"data"}}"
[rugby]=>
string(27) "{"data":{"example":"data"}}"
[football]=>
string(27) "{"data":{"example":"data"}}"
[tennis]=>
string(27) "{"data":{"example":"data"}}"
[swimming]=>
string(27) "{"data":{"example":"data"}}"
[order]=>
string(60) "{"cricket":1,"football":2,"rugby":3,"swimming":4,"tennis":5}"
}
In my view, I have access to this object. I need to order the output of each of the above depending on the [order] array values and then print the object. So in this example, the output can ignore some - like id and created_date - but should be something like:
echo `cricket` data
echo `football` data
echo `rugby` data
echo `swimming` data
echo `tennis` data
The output above is a print_r of the $this->singleitem, so I can access each object like this:
<?php $orders = json_decode($this->singleitem->order);?>
<?php foreach ($orders as $itemOrder) {
print_r($itemOrder);
} ?>
Which gives me 12345 with a print_r on $this->singleitem->order I get:
stdClass Object ( [cricket] => 1 [football] => 2 [rugby] => 3 [swimming] => 4 [tennis] => 5 )
If the [order] is empty, or a value is missing, it should skip the corresponding missing item and append it to the end, is this even possible in php?
Thanks!
SingleClass, so I did each one, like $this->singleitem->order and it gave me the exact same string as above after the string(60) minus the quotes, e.g.{"cricket":1,"football":2,"rugby":3,"swimming":4,"tennis":5}