1

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!

4
  • That is possible in php. That data structure you show, is not json and not php, what is it? eg do the [] mean array in that format? Commented Jun 26, 2020 at 0:15
  • 1
    It's my terrible ability to copy/paste the correct stuff. I updated the code in the OP with a var_dump of the object. Thanks! Commented Jun 26, 2020 at 0:26
  • can you echo (string) $this->singleitem, then copy/paste the raw json in a comment, so i don't have to create the same data stucture. Commented Jun 26, 2020 at 0:33
  • I tried that, and it printed out: 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} Commented Jun 26, 2020 at 0:41

2 Answers 2

1
  1. Get the order as an array and sort by the number value (just in case they aren't always in order already)
  2. Iterate the keys of the sorted order array and output the corresponding property from singleitem
$order = json_decode($this->singleitem->order, true); // "true" to produce an assoc array
asort($order); // sort by value, maintaining keys

foreach(array_keys($order) as $prop) {
  $val = json_decode($this->singleitem->$prop);
  echo $val->data->example, PHP_EOL;
}

Demo ~ https://3v4l.org/XCcj5

Sign up to request clarification or add additional context in comments.

1 Comment

This did the job perfectly. You are a hero!!
1

This proabably won't work exactly as expected, because the data structure i have used, might be slightly different, but it should give you a good idea of how to do it.

<?php
$singleitem = '
{
  "id": 1,
  "created_date": "2020-06-25 17:50:00",
  "cricket": {
    "data": {
      "example": "data"
    }
  },
  "rugby": {
    "data": {
      "example": "data"
    }
  },
  "football": {
    "data": {
      "example": "data"
    }
  },
  "tennis": {
    "data": {
      "example": "data"
    }
  },
  "swimming": {
    "data": {
      "example": "data"
    }
  },
  "order": {
    "cricket": 1,
    "rugby": 3,
    "swimming": 4,
    "tennis": 5
  }
}
';

$json = json_decode($singleitem, true); // true turns the data into an assoc array
    
$sport_order = array_keys($json['order']);

// list all sport data in order
foreach ($sport_order as $sport) {
    echo $sport .  json_encode($json[$sport]['data']) . "<br>";
}

// list all sport data, that is not in sort list, last
// eg in this example football should be listed last
foreach ($json as $key => $val) {
    
    // idealy we should have a list of valid values, rather then excluding id, order and created_date,
    // unless we are sure there will never be any further meta data added
    
    // ignore id
    if ($key === 'id') {
        continue;
    }

    // ignore order
    if ($key === 'order') {
        continue;
    }

    // ignore created_date
    if ($key === 'created_date') {
        continue;
    }

    if (in_array($key, $sport_order)) {
        continue;
    }

    echo $key .  json_encode($val) . "<br>";
}

1 Comment

Great approach, thanks. That lets me easily decide which objects I want to display and which I can ignore. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.