在csv文件中转储数组时数据不匹配

时间:2014-11-15 03:31:48

标签: php arrays

我有7个由mysql提取的数组并且它正常工作每个数组都有9行是city_names但是其中一个数组只有7行,因为结果中的两个值为null。这会在csv中转储文件时导致混淆。我使用下面的代码。有没有办法按城市名称检查它而不使用foreach for eact array?

        for($i=0 ; $i <= $count-2 ; $i++)
        {
            $data[] = $all_restaurants_opr_no_temp_off[$i]['city_name'];

            $data[] = $all_restaurants_opr[$i]['total'];


            $data[] = $all_restaurants_opr_no_temp_off[$i]['total'];



            $data[] = $restaurants_opr_temp_off[$i]['total'];

                $data[] = $restaurants_opr_operations_closed[$i]['total'];

                $data[] = $restaurants_opr_automated[$i]['total'];

                $data[] = $restaurants_opr_automated_working[$i]['total'];

                $data[] = $restaurants_opr_online_payment[$i]['total'];

                $data[] = $restaurants_opr_online_payment_with_tempoff[$i]['total'];

                $data[] = $restaurants_opr_atleast_one_order[$i]['total'];

            fputcsv($fp, $data,",");
            unset($data);
            }


Array 1:
array(9) {
   [0]=>
  array(2) {
    ["city_name"]=>
    string(9) "Bangalore"
    ["total"]=>
    string(3) "687"
  }
  [1]=>
  array(2) {
    ["city_name"]=>
    string(9) "Hyderabad"
    ["total"]=>
    string(2) "16"
  }
  [2]=>
  array(2) {
    ["city_name"]=>
    string(6) "Mumbai"
    ["total"]=>
    string(3) "568"
  }
  [3]=>
  array(2) {
    ["city_name"]=>
    string(7) "Chennai"
    ["total"]=>
    string(3) "139"
  }
  [4]=>
  array(2) {
    ["city_name"]=>
    string(4) "Pune"
    ["total"]=>
    string(3) "232"
  }
  [5]=>
  array(2) {
    ["city_name"]=>
    string(9) "Ghaziabad"
    ["total"]=>
    string(2) "57"
  }
  [6]=>
  array(2) {
    ["city_name"]=>
    string(5) "Noida"
    ["total"]=>
    string(2) "77"
  }
  [7]=>
  array(2) {
    ["city_name"]=>
    string(9) "Faridabad"
    ["total"]=>
    string(1) "4"
  }
  [8]=>
  array(2) {
    ["city_name"]=>
    string(7) "Gurgaon"
    ["total"]=>
    string(3) "113"
  }
}
Array 2:
array(9) {
  [0]=>
  array(2) {
    ["city_name"]=>
    string(9) "Bangalore"
    ["total"]=>
    string(3) "674"
  }
  [1]=>
  array(2) {
    ["city_name"]=>
    string(9) "Hyderabad"
    ["total"]=>
    string(2) "16"
  }
  [2]=>
  array(2) {
    ["city_name"]=>
    string(6) "Mumbai"
    ["total"]=>
    string(3) "547"
  }
  [3]=>
  array(2) {
    ["city_name"]=>
    string(7) "Chennai"
    ["total"]=>
    string(3) "135"
  }
  [4]=>
  array(2) {
    ["city_name"]=>
    string(4) "Pune"
    ["total"]=>
    string(3) "202"
  }
  [5]=>
  array(2) {
    ["city_name"]=>
    string(9) "Ghaziabad"
    ["total"]=>
    string(2) "56"
  }
  [6]=>
  array(2) {
    ["city_name"]=>
    string(5) "Noida"
    ["total"]=>
    string(2) "77"
  }
  [7]=>
  array(2) {
    ["city_name"]=>
    string(9) "Faridabad"
    ["total"]=>
    string(1) "4"
  }
  [8]=>
  array(2) {
    ["city_name"]=>
    string(7) "Gurgaon"
    ["total"]=>
    string(3) "111"
  }
}
Array 3:(Problem is here)
array(6) {
  [0]=>
  array(2) {
    ["city_name"]=>
    string(9) "Bangalore"
    ["total"]=>
    string(2) "13"
  }
  [1]=>
  array(2) {
    ["city_name"]=>
    string(6) "Mumbai"
    ["total"]=>
    string(2) "21"
  }
  [2]=>
  array(2) {
    ["city_name"]=>
    string(7) "Chennai"
    ["total"]=>
    string(1) "4"
  }
  [3]=>
  array(2) {
    ["city_name"]=>
    string(4) "Pune"
    ["total"]=>
    string(2) "30"
  }
  [4]=>
  array(2) {
    ["city_name"]=>
    string(9) "Ghaziabad"
    ["total"]=>
    string(1) "1"
  }
  [5]=>
  array(2) {
    ["city_name"]=>
    string(7) "Gurgaon"
    ["total"]=>
    string(1) "2"
  }
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解100%的问题,但也许您可以测试每个值是否为null并在其中放置默认值(例如,对于此行,您可以使用上面的内容)你的for循环:

foreach($badArray as $b){
  $reformattedArray[$b['city_name']] = $b['total'];
}

然后在for循环中用这个替换坏数组行

data[] = is_null($reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']]) ? 'Default' : $reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']];