【问题标题】:CI PHP: How to hold data in Array while using double Foreach Loop and turn it json encode?CI PHP:如何在使用双 Foreach 循环时将数据保存在数组中并将其转换为 json 编码?
【发布时间】:2019-08-10 16:27:38
【问题描述】:

我想从两个没有关系的表中获取数据,我使用 foreach 通过选定的键获取所有数据并将两个表中的数据收集到数组中。如何在使用 foreach 获取两个表时将数据保存在数组中?我正在使用 Codeigniter 框架。 这是代码:

$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);
     }
}

我得到这样的欲望:

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
)

如果我把json变成这样的结果:

{"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}

这不是有效的 json 格式,所以请帮忙..

【问题讨论】:

  • 你的预期输出是什么?
  • 如果你在循环中打印一个 json_encode ,它将产生一个 json 对象。这意味着您将获得多个 json 对象作为输出,没有外部键值对
  • @M.Hemant 我在转成 json 格式时会这样 [ {"room_code":"1212","price_code":"P1","price":"500","discount_type" :"ammount","disc":"20","finalprice":480}, {"room_code":"1213","price_code":"P2","price":"500","discount_type":"百分比","光盘":"50","finalprice":250} ]
  • 尝试在开头添加$result = [],然后将$data 推送到print_r 行的$result,然后将echo json_encode($result) 添加到末尾。
  • @AdarshMohan 如果我打印出循环,我只会在键值循环结束时得到 1 个值,如何在外部循环中获取所有值?

标签: php arrays json codeigniter foreach


【解决方案1】:

试试这个,
你的 $data 是重叠的,所以我只是将它转换为一个数组,所以所有数据都会存储在其中。

$price = $this->pricing->get_by($hc); //get from pricing
$mainData = array();
foreach ($price as $ps){
   $promo = $this->discount->get_by($ps['key1'],$ps['key2']); //get from discount
 if(!empty($promo)){
   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') {
          $price = $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
          );                                
       }
       array_push($mainData,$data);    
    }
  }
}

【讨论】:

  • 这不起作用,因为它将打印一个不断增长的数组作为每个循环的输出。
  • @AdarshMohan 现在正确吗?
  • @Koala_Yeung 已经解决的问题,感谢您的帮助
【解决方案2】:

你可以这样做:

$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);

结果应该是一个有效的 JSON 数组:

[{"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}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-01
    • 2014-08-26
    • 2019-04-17
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 2023-03-17
    • 2022-01-22
    相关资源
    最近更新 更多