2016-06-27 64 views
-4

查詢:如何在php中定製數組?

SELECT t1.`id`, t1.`year` as service_year, t5.`month` as service_month, t6.id as country_id, t6.`country`, t7.`id` as mobile_operator_id, t7.`mobile_operator`, t1.`service_id`, t1.`gross_revenue_actual`, t1.`content_partner_share` 
, t2.`service` 
, t4.`collected_at`, t4.`id` as invoice_id 
FROM `PaymentCollections` t1 
JOIN `Services` t2 ON t1.`service_id` = t2.`id` 
JOIN `PaymentCollectionInvoiceItems` t3 ON t3.`payment_collection_id` = t1.`id` 
JOIN `PaymentCollectionInvoices` t4 ON t4.`id` = t3.`invoice_id` 
JOIN `Months` t5 ON t5.`id` = t1.`month` 
JOIN `Countries` t6 ON t6.`id` = t2.`country_id` 
JOIN `MobileOperators` t7 ON t7.`id` = t2.`mobile_operator_id` 
WHERE t1.`payment_collection_status_id` = 18 
GROUP BY t1.`year`, t1.`month` ORDER BY t1.`year` DESC, t1.`month` ASC 

當運行上述典型的查詢然後發現的數組:

[0] => stdClass Object 
    (
     [id] => 5132 
     [service_year] => 2016 
     [service_month] => April 
     [country_id] => 116 
     [country] => Kenya 
     [mobile_operator_id] => 3 
     [mobile_operator] => Airtel 
     [service_id] => 16 
     [gross_revenue_actual] => 11500.000000 
     [content_partner_share] => 1000.000000 
     [service] => Christianity Portal 
     [collected_at] => 2016-06-24 09:59:19 
     [invoice_id] => 105 
    ) 

[1] => stdClass Object 
    (
     [id] => 4982 
     [service_year] => 2016 
     [service_month] => May 
     [country_id] => 116 
     [country] => Kenya 
     [mobile_operator_id] => 3 
     [mobile_operator] => Airtel 
     [service_id] => 16 
     [gross_revenue_actual] => 11500.000000 
     [content_partner_share] => 1000.000000 
     [service] => Christianity Portal 
     [collected_at] => 2016-06-24 10:02:21 
     [invoice_id] => 106 
    ) 

[2] => stdClass Object 
    (
     [id] => 4732 
     [service_year] => 2016 
     [service_month] => June 
     [country_id] => 116 
     [country] => Kenya 
     [mobile_operator_id] => 3 
     [mobile_operator] => Airtel 
     [service_id] => 16 
     [gross_revenue_actual] => 11000.000000 
     [content_partner_share] => 1000.000000 
     [service] => Christianity Portal 
     [collected_at] => 2016-06-24 13:13:42 
     [invoice_id] => 114 
    ) 

[3] => stdClass Object 
    (
     [id] => 5522 
     [service_year] => 2015 
     [service_month] => December 
     [country_id] => 51 
     [country] => Congo(Republic of) 
     [mobile_operator_id] => 31 
     [mobile_operator] => MTN 
     [service_id] => 56 
     [gross_revenue_actual] => 11500.000000 
     [content_partner_share] => 1000.000000 
     [service] => Music On Demand 
     [collected_at] => 2016-06-24 11:26:09 
     [invoice_id] => 101 
    ) 

通過使用上述陣列,如果service_yearcountrymobile_operatorservice是相同的,但月不同,那麼我想創建如下的新陣列

$finalarray = { 
0 = { 
[id] => 4732, 
[service_year] => 2016, 
[service] => Christianity Portal, 
[country] => Kenya, 
[mobile_operator] => Airtel, 
array{ 
array[April] { 
[gross_revenue_actual] => 11500.000000 
[content_partner_share] => 1000.000000 
[collected_at] => 2016-06-24 10:02:21 
}, 
array[May] { 
    [gross_revenue_actual] => 11000.000000 
    [content_partner_share] => 1000.000000 
    [collected_at] => 2016-06-24 13:13:42 
    }, 
    array[June] { 
     [gross_revenue_actual] => 11000.000000 
     [content_partner_share] => 1000.000000 
     [collected_at] => 2016-06-24 13:13:42 
     }, 
    } 
} 
1 = { 
    [id] => 5522 
    [service_year] => 2015 
    [service_month] => December 
    [country_id] => 51 
    [country] => Congo(Republic of) 
    [mobile_operator_id] => 31 
    [mobile_operator] => MTN 
    [service_id] => 56 
    [gross_revenue_actual] => 11500.000000 
    [content_partner_share] => 1000.000000 
    [service] => Music On Demand 
    [collected_at] => 2016-06-24 11:26:09 
    [invoice_id] => 101 
} 

}

如何在使用Laravel 5框架時執行此操作?是否有可能修改我的查詢來獲得這種類型的數組?

我欣賞所有提示和回覆。謝謝你。

+0

這個問題似乎是寫一個SQL查詢。所以,我寧願你先在SQL社區中提出這個問題。據我所知,你需要在SQL查詢中使用GROUP BY子句來分組月份,年份,服務或任何你想要的。只有基於查詢結果,您需要決定是否要在PHP中實現查詢。 – Perumal

+0

感謝您的建議,在這裏我通過編輯我的問題添加查詢。 –

+0

你需要從返回的數組中刪除Stdclass對象,我猜? –

回答

3

可以解析在以下方式PHP數組,

輸入: -

$result = array('0' => 
     array(
      'id' => 5132, 
      'service_year' => 2016, 
      'service_month' => 'April', 
      'country_id' => 116, 
      'country' => 'Kenya', 
      'mobile_operator_id' => 3, 
      'mobile_operator' => 'Airtel', 
      'service_id' => 16, 
      'gross_revenue_actual' => 11500.000000, 
      'content_partner_share' => 1000.000000, 
      'service' => 'Christianity Portal', 
      'collected_at' => '2016-06-24 09:59:19', 
      'invoice_id' => 105 
     ), 
     '1' => 
     array(
      'id' => 4982, 
      'service_year' => 2016, 
      'service_month' => 'May', 
      'country_id' => 116, 
      'country' => 'Kenya', 
      'mobile_operator_id' => 3, 
      'mobile_operator' => 'Airtel', 
      'service_id' => 16, 
      'gross_revenue_actual' => 11500.000000, 
      'content_partner_share' => 1000.000000, 
      'service' => 'Christianity Portal', 
      'collected_at' => '2016-06-24 10:02:21', 
      'invoice_id' => 106 
     ), 
     '2' => 
     array(
      'id' => 4732, 
      'service_year' => 2016, 
      'service_month' => 'June', 
      'country_id' => 116, 
      'country' => 'Kenya', 
      'mobile_operator_id' => 3, 
      'mobile_operator' => 'Airtel', 
      'service_id' => 16, 
      'gross_revenue_actual' => 11000.000000, 
      'content_partner_share' => 1000.000000, 
      'service' => 'Christianity Portal', 
      'collected_at' => '2016-06-24 13:13:42', 
      'invoice_id' => 114, 
     ), 
     '3' => 
     array(
      'id' => 5522, 
      'service_year' => 2015, 
      'service_month' => 'December', 
      'country_id' => 51, 
      'country' => 'Congo(Republic of)', 
      'mobile_operator_id' => 31, 
      'mobile_operator' => 'MTN', 
      'service_id' => 56, 
      'gross_revenue_actual' => 11500.000000, 
      'content_partner_share' => 1000.000000, 
      'service' => 'Music On Demand', 
      'collected_at' => '2016-06-24 11:26:09', 
      'invoice_id' => 101, 
      )); 

這是PHP代碼ü需要使用解析上述陣列以獲得所需的結果。

<?php 

$arraycheck = array(); 
$finalArray = array(); 
$counter = 0; 
foreach ($result as $key => $values) { 
    $flag = 0; 
    $id = 0; 
    $month = 0; 
    foreach ($arraycheck as $k => $v) { 
     if ($v['service_year'] == $values['service_year'] && 
       $v['country'] == $values['country'] && 
       $v['mobile_operator'] == $values['mobile_operator'] && 
       $v['service'] == $values['service']) { 
      $flag = 1; 
      $id = $k; 
      $month = $v['service_month']; 
      break; 
     } 
    } 

    if ($flag == 0) { 
     $arraycheck[$values['id']]['service_year'] = $values['service_year']; 
     $arraycheck[$values['id']]['country'] = $values['country']; 
     $arraycheck[$values['id']]['mobile_operator'] = $values['mobile_operator']; 
     $arraycheck[$values['id']]['service'] = $values['service']; 
     $arraycheck[$values['id']]['service_month'] = $values['service_month']; 
     $finalArray[$values['id']] = $values; 
    } 
    if ($flag == 1 && !empty($id)) { 
     $finalArray[$id][$month]['gross_revenue_actual'] = $finalArray[$id]['gross_revenue_actual']; 
     $finalArray[$id][$month]['content_partner_share'] = $finalArray[$id]['content_partner_share']; 
     $finalArray[$id][$month]['collected_at'] = $finalArray[$id]['collected_at']; 
     $finalArray[$id][$values['service_month']]['gross_revenue_actual'] = $values['gross_revenue_actual']; 
     $finalArray[$id][$values['service_month']]['content_partner_share'] = $values['content_partner_share']; 
     $finalArray[$id][$values['service_month']]['collected_at'] = $values['collected_at']; 
     unset($finalArray[$id]['gross_revenue_actual']); 
     unset($finalArray[$id]['content_partner_share']); 
     unset($finalArray[$id]['collected_at']); 
     unset($finalArray[$id]['service_month']); 
    } 
    $counter++; 
} 
echo "<pre>"; 
print_r($finalArray); 
die; 
?> 

OUTPUT: - 輸出會是這樣

Array 
(
    [5132] => Array 
     (
      [id] => 5132 
      [service_year] => 2016 
      [country_id] => 116 
      [country] => Kenya 
      [mobile_operator_id] => 3 
      [mobile_operator] => Airtel 
      [service_id] => 16 
      [service] => Christianity Portal 
      [invoice_id] => 105 
      [April] => Array 
       (
        [gross_revenue_actual] => 
        [content_partner_share] => 
        [collected_at] => 
       ) 

      [May] => Array 
       (
        [gross_revenue_actual] => 11500 
        [content_partner_share] => 1000 
        [collected_at] => 2016-06-24 10:02:21 
       ) 

      [June] => Array 
       (
        [gross_revenue_actual] => 11000 
        [content_partner_share] => 1000 
        [collected_at] => 2016-06-24 13:13:42 
       ) 

     ) 

    [5522] => Array 
     (
      [id] => 5522 
      [service_year] => 2015 
      [service_month] => December 
      [country_id] => 51 
      [country] => Congo(Republic of) 
      [mobile_operator_id] => 31 
      [mobile_operator] => MTN 
      [service_id] => 56 
      [gross_revenue_actual] => 11500 
      [content_partner_share] => 1000 
      [service] => Music On Demand 
      [collected_at] => 2016-06-24 11:26:09 
      [invoice_id] => 101 
     ) 

) 

我希望這將幫助你