[英]PHP Shopware Rest API - How to sum specific Keyvalues of Arrays in an Array
我想計算並顯示一些網店(平台:Shopware)的營業額,按月過濾,沒有取消訂單。 我已經連接到網上商店,我已經收到了所有訂單,按月排序,過濾掉取消的訂單。 現在我將所有相關訂單推送到一個名為 $october 的新數組中。 在 $october 中,每個訂單本身就是一個包含許多鍵和值的數組。 我只需要將所有“invoiceAmount”值匯總在一起,就可以顯示本月的營業額。 我嘗試了很多變體(已評論),我得到的最好的結果是 $sum 的初始化為“0”......但我對 php 也很陌生。 請幫幫我:-)
<?php
include_once ('api.php');
$url = xxxxx;
$user = xxxxx;
$key = xxxxx;
$client = new ApiClient($url,$user,$key);
$orders = $client->get('orders');
$october = [];
echo "<h1>===================month Array==============</h1>";
foreach ($orders as $order) {
foreach ($order as $field) {
if ($field["orderStatusId"]!==4) {
$mystring = $field[orderTime];
$findme = '-10-';
$pos = strpos($mystring, $findme);
if ($pos !=null) {
print_r($field);
array_push($october,$field);
}
}
}
}
echo "<script>";
echo 'console.log('.json_encode($october).')';
echo "</script>";
print_r($october);
echo("<h1>===================Sum of month==============</h1>");
$sumOctober = 0;
foreach ($october as $field["invoiceAmount"]=>$value) {
$sumOctober+= $value;
}
/*
$sumOctober = 0;
foreach ($october as $order) {
foreach ($order as $field["invoiceAmount"]=>$value) {
$sumOctober+= $value;
}
}
*/
echo($sumOctober);
/*
$arr = $october;
$sum = 0;
foreach ($arr as $order){
foreach ($order as $field["invoiceAmount"]=>$value) {
$sum += $value;
}
}
*/
/*
$arr = $october($orders($key));
$sum = 0;
array_walk_recursive($arr, function($val, $key) use (&$sum) {
$sum += $val;
});
var_dump($sum);
echo "<h1>Die Summe Oktober ist:</h1>";
echo ($sum);
*/
?>
這完全有幫助,不知道為什么那個人馬上刪除了他的答案......但是謝謝!
$sumOctober = 0;
foreach ($october as $oct) {
$sumOctober+= $oct["invoiceAmount"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.