2

I know this question has been asked but still i am having issue in getting it right

{
  "status": "400",
  "msg": "List Of Lotteries",
  "lotteryRes": {
    "result": {
      "Disawar": [
        "Disawar"
      ],
      "DL Bazzar": [
        "DL Bazzar"
      ],
      "Faridabad": [
        "Faridabad"
      ],
      "Gaziyabad": [
        "Gaziyabad"
      ],
      "X90": [
        "X90"
      ],
      "Gali": [
        "Gali"
      ],
      "RAJASTHAN ROYAL": [
        "RAJASTHAN ROYAL"
      ],
      "Rajasthan Bazaar": [
        "Rajasthan Bazaar"
      ]
    },
    "lottery_date": [
      "2018-03-25 05:30:00",
      "2018-03-31 15:20:00",
      "2018-03-30 18:15:00",
      "2018-03-31 20:20:00",
      "2018-04-04 23:00:00",
      "2018-04-02 23:25:00",
      "2018-04-14 16:00:00",
      "2018-04-13 14:00:00"
    ]
  }
}

I want to display the data like

<option>Disawar 2018-03-25 05:30:00</option>
<option>DL Bazar 2018-03-31 15:20:00</option>

for every element above is a respective date and time ,I tried but its giving me errors like : Array to string conversion and someother errors

 $json = file_get_contents('url');
                $obj = json_decode(stripslashes($json),true);
    echo "<a href='#' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='false'>  $obj[msg];<span class='caret'></span></a>
                    <ul class='dropdown-menu'>
                    <li>$obj[lotteryRes]</li>

Any idea how to do that?

2 Answers 2

1

Try this, the json is a little odd, so reset the arrays to numeric keys then use the keys to get the date value:

$array = json_decode($json, true);

foreach (array_values($array['lotteryRes']['result']) as $key => $result) {
    echo array_values($result)[0].' '.$array['lotteryRes']['lottery_date'][$key].PHP_EOL;
}

https://3v4l.org/Ki7Od

Result:

Disawar 2018-03-25 05:30:00
DL Bazzar 2018-03-31 15:20:00
Faridabad 2018-03-30 18:15:00
Gaziyabad 2018-03-31 20:20:00
X90 2018-04-04 23:00:00
Gali 2018-04-02 23:25:00
RAJASTHAN ROYAL 2018-04-14 16:00:00
Rajasthan Bazaar 2018-04-13 14:00:00

Then just apply the HTML to get your desired result.

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

Comments

0

how about this :

  $obj[2].lottery_date

Comments