PHP JSON උදව්

Dokolla

Well-known member
  • Nov 19, 2011
    954
    244
    63
    On the spot..
    මම REST API එකක් Call කරල පහල එක response අවිත් තියෙනව.. මට ඕනෙ මේකෙ එන values ටික පල්ලෙහා format එකට හදාගන්න.

    මෙකෙ එන JSON Objects ගානවෙනස් වෙන්න පුලුවන් වෙලාවෙන් වෙලාවට.. While loop එකක් දාගන්න ඕනෙ මට හිතෙන විදියට..

    අනෙ යාලුවනෙ Please help... පින් සිද්ද වෙනවා....


    Response :
    [{"code":"green","id":"A1","name":"colombo"},
    {"code":"red","id":"A2","name":"kandy"},
    {"code":"blue","id":"A3","name":"jaffna"},
    {"code":"orange","id":"A4","name":"galle"},
    {"code":"yellow","id":"A5","name":"mathara"}]


    Pahala widyata thani variable ekkata asssign karaganna one...


    1. green A1 is colombo
    2. red A2 is kandy
    3. blue A3 is jaffna
    4. orange A4 is galle
    5. yellow A5 is mathara
     

    stasia

    Well-known member
  • May 28, 2018
    12,814
    26,356
    113
    כדור הארץ
    PHP:
    $response = '[{"code":"green","id":"A1","name":"colombo"},
        {"code":"red","id":"A2","name":"kandy"},
        {"code":"blue","id":"A3","name":"jaffna"},
        {"code":"orange","id":"A4","name":"galle"},
        {"code":"yellow","id":"A5","name":"mathara"}]';
    
        $arr = [];
    
        foreach (json_decode($response, true) as $item) {
            $arr[] = $item['code'].' '. $item['id']. ' is '.$item['name'];
        }
    
        echo '<pre>';
        var_dump($arr);

    Code:
    array(5) {
      [0]=>
      string(19) "green A1 is colombo"
      [1]=>
      string(15) "red A2 is kandy"
      [2]=>
      string(17) "blue A3 is jaffna"
      [3]=>
      string(18) "orange A4 is galle"
      [4]=>
      string(20) "yellow A5 is mathara"
    }