Search code examples
phparraysjsoncodeigniterforeach

CI PHP: How to hold data in Array while using double Foreach Loop and turn it json encode?


I wanna get data from two table with no relation, i using foreach to fetch all data by selected key and gather data from two table into array. How do i hold data in array while i fetch two table using foreach? im using Codeigniter framework. here is the code:

$price = $this->pricing->get_by($hc); //get from pricing
foreach ($price as $ps){
   $promo = $this->discount->get_by($ps['key1'],$ps['key2']); //get from discount
   foreach ($promo as $k) {
      if ($k['keyA']!='percent') {
         $pricing = $ps['key3'] - $k['keyB'];   
         $data = array(
            'room_code'=>$ps['key1'],
            'price_code'=>$ps['key2'],
            'price'=>$ps['key3'],
            'discount_type'=>$k['keyA'],
            'disc'=>$k['keyB'],
            'finalprice'=>$pricing
          );
       } 
       elseif ($k['keyA']=='percent') {
          $pricing = $ps['key3'] - ($ps['key3'] * $k['keyB']/100);
          $data = array(
            'room_code'=>$ps['key1'],
            'price_code'=>$ps['key2'],
            'price'=>$ps['key3'],
            'discount_type'=>$k['keyA'],
            'disc'=>$k['keyB'],
            'finalprice'=>$pricing
          );                                
       }    
     print_r($data);
     // echo json_encode($data);
     }
}

And i get the relust like this:

Array
(
[room_code] => 1212    
[price_code] => P1
[price] => 500
[discount_type] => amount
[disc] => 20
[finalprice] => 480
)
Array
(
[room_code] => 1213
[price_code] => P2
[price] => 500
[discount_type] => percent
[disc] => 50
[finalprice] => 250
)

If i turn json the result like this:

{"room_code":"1212","price_code":"P1","price":"500","discount_type":"amount","disc":"20","finalprice":480}{"room_code":"1213","price_code":"P2","price":"500","discount_type":"percent","disc":"50","finalprice":250}

This is not valid json format, so please help..


Solution

  • You could do it like this:

    $price = $this->pricing->get_by($hc); //get from pricing
    $data_arr = array();
    foreach ($price as $ps){
       $promo = $this->discount->get_by($ps['key1'],$ps['key2']); //get from discount
       foreach ($promo as $k) {
          if ($k['keyA']!='percent') {
             $pricing = $ps['key3'] - $k['keyB'];   
             $data = array(
                'room_code'=>$ps['key1'],
                'price_code'=>$ps['key2'],
                'price'=>$ps['key3'],
                'discount_type'=>$k['keyA'],
                'disc'=>$k['keyB'],
                'finalprice'=>$pricing
              );
           } 
           elseif ($k['keyA']=='percent') {
              $pricing = $ps['key3'] - ($ps['key3'] * $k['keyB']/100);
              $data = array(
                'room_code'=>$ps['key1'],
                'price_code'=>$ps['key2'],
                'price'=>$ps['key3'],
                'discount_type'=>$k['keyA'],
                'disc'=>$k['keyB'],
                'finalprice'=>$pricing
              );                                
           }    
           $data_arr[] = $data;
         }
    }
    
    echo json_encode($data_arr);
    

    The result should be a valid JSON array:

    [{"room_code":"1212","price_code":"P1","price":"500","discount_type":"amount","disc":"20","finalprice":480},{"room_code":"1213","price_code":"P2","price":"500","discount_type":"percent","disc":"50","finalprice":250}]