imbalancez
@imbalancez
Веб-разработчик

Как подсчитать сумму по элементам/ключам массива?

Есть вот такой массив
$json = json_decode('{"Paladins":{"name":"Paladins","hours":"106","vac":false},"Dota 2":{"name":"Dota 2","hours":"603","vac":false},"Ballistic Protection":{"name":"Ballistic Protection","hours":0,"vac":false},"Black Squad":{"name":"Black Squad","hours":0,"vac":false},"Commander: Conquest of the Americas Gold":{"name":"Commander: Conquest of the Americas Gold","hours":0,"vac":false},"Counter-Strike: Global Offensive":{"name":"Counter-Strike: Global Offensive","hours":"140.1","vac":false},"Cuisine Royale":{"name":"Cuisine Royale","hours":0,"vac":false},"Darwin Project":{"name":"Darwin Project","hours":0,"vac":false},"East India Company Gold":{"name":"East India Company Gold","hours":0,"vac":false},"For Honor":{"name":"For Honor","hours":0,"vac":false},"For Honor - Public Test":{"name":"For Honor - Public Test","hours":0,"vac":false},"Fortified":{"name":"Fortified","hours":0,"vac":false},"Friday the 13th: Killer Puzzle":{"name":"Friday the 13th: Killer Puzzle","hours":0,"vac":false},"H1Z1":{"name":"H1Z1","hours":0,"vac":false},"H1Z1: Test Server":{"name":"H1Z1: Test Server","hours":0,"vac":false},"Half-Life 2: Deathmatch":{"name":"Half-Life 2: Deathmatch","hours":0,"vac":false},"Half-Life 2: Lost Coast":{"name":"Half-Life 2: Lost Coast","hours":"3.7","vac":false},"InfiniPicross":{"name":"InfiniPicross","hours":0,"vac":false},"One Finger Death Punch":{"name":"One Finger Death Punch","hours":0,"vac":false},"Overcast - Walden and the Werewolf":{"name":"Overcast - Walden and the Werewolf","hours":0,"vac":false},"Paladins - Public Test":{"name":"Paladins - Public Test","hours":0,"vac":false},"Pirates of Black Cove Gold":{"name":"Pirates of Black Cove Gold","hours":0,"vac":false},"Quake Champions":{"name":"Quake Champions","hours":0,"vac":false},"S.K.I.L.L. - Special Force 2":{"name":"S.K.I.L.L. - Special Force 2","hours":0,"vac":false},"Saints Row 2":{"name":"Saints Row 2","hours":0,"vac":false},"Save the Lamb":{"name":"Save the Lamb","hours":0,"vac":false},"Shoot Paint":{"name":"Shoot Paint","hours":0,"vac":false},"Sniper Fury":{"name":"Sniper Fury","hours":0,"vac":false},"Soldier of Failure":{"name":"Soldier of Failure","hours":0,"vac":false},"Team Fortress 2":{"name":"Team Fortress 2","hours":"2.6","vac":false},"The Last Hope":{"name":"The Last Hope","hours":0,"vac":false},"Totally Accurate Battlegrounds":{"name":"Totally Accurate Battlegrounds","hours":0,"vac":false},"Unforgiving Trials: The Space Crusade":{"name":"Unforgiving Trials: The Space Crusade","hours":0,"vac":false},"Unturned":{"name":"Unturned","hours":"0.7","vac":false},"World of Tanks Blitz":{"name":"World of Tanks Blitz","hours":"128.1","vac":false}}', true);


spoiler
xejFTfrERUSndDe31kSQDQ.png


Нужно подсчитать сумму

Если в массиве есть ключ "Counter-Strike: Global Offensive" тогда цена будет 60
Если в массиве есть ключ "PLAYERUNKNOWN'S BATTLEGROUNDS" тогда цена будет 75
А если есть "Counter-Strike: Global Offensive" и "PLAYERUNKNOWN'S BATTLEGROUNDS" то цена будет 85.

Как это все правильно сделать? Голова уже кипит, уже просто часов 10-12 сижу работаю, и глаза закрываются, уверен что решение очень простое, но в голову не приходит ничего толкового, лишь костыли.
  • Вопрос задан
  • 115 просмотров
Решения вопроса 1
dollar
@dollar
Делай добро и бросай его в воду.
$cs = array_key_exists("Counter-Strike: Global Offensive",$json);
$pu = array_key_exists("PLAYERUNKNOWN'S BATTLEGROUNDS",$json);
if ($cs && $pu) $price = 85;
elseif ($cs) $price = 60;
elseif ($pu) $price = 75;
Ответ написан
Пригласить эксперта
Ответы на вопрос 2
slo_nik
@slo_nik Куратор тега PHP
Не знаю как Вы храните данные, но надо использовать array_key_exists()
if (array_key_exists('Counter-Strike: Global Offensive', $json)) {
    echo "Массив содержит элемент 'Counter-Strike: Global Offensive'. И цена будет такой-то...";
}

Остальное доработайте сами.
Ответ написан
@LemonFox
stateless mind
$json = json_decode('{"Paladins":{"name":"Paladins","hours":"106","vac":false},"Dota 2":{"name":"Dota 2","hours":"603","vac":false},"Ballistic Protection":{"name":"Ballistic Protection","hours":0,"vac":false},"Black Squad":{"name":"Black Squad","hours":0,"vac":false},"Commander: Conquest of the Americas Gold":{"name":"Commander: Conquest of the Americas Gold","hours":0,"vac":false},"Counter-Strike: Global Offensive":{"name":"Counter-Strike: Global Offensive","hours":"140.1","vac":false},"Cuisine Royale":{"name":"Cuisine Royale","hours":0,"vac":false},"Darwin Project":{"name":"Darwin Project","hours":0,"vac":false},"East India Company Gold":{"name":"East India Company Gold","hours":0,"vac":false},"For Honor":{"name":"For Honor","hours":0,"vac":false},"For Honor - Public Test":{"name":"For Honor - Public Test","hours":0,"vac":false},"Fortified":{"name":"Fortified","hours":0,"vac":false},"Friday the 13th: Killer Puzzle":{"name":"Friday the 13th: Killer Puzzle","hours":0,"vac":false},"H1Z1":{"name":"H1Z1","hours":0,"vac":false},"H1Z1: Test Server":{"name":"H1Z1: Test Server","hours":0,"vac":false},"Half-Life 2: Deathmatch":{"name":"Half-Life 2: Deathmatch","hours":0,"vac":false},"Half-Life 2: Lost Coast":{"name":"Half-Life 2: Lost Coast","hours":"3.7","vac":false},"InfiniPicross":{"name":"InfiniPicross","hours":0,"vac":false},"One Finger Death Punch":{"name":"One Finger Death Punch","hours":0,"vac":false},"Overcast - Walden and the Werewolf":{"name":"Overcast - Walden and the Werewolf","hours":0,"vac":false},"Paladins - Public Test":{"name":"Paladins - Public Test","hours":0,"vac":false},"Pirates of Black Cove Gold":{"name":"Pirates of Black Cove Gold","hours":0,"vac":false},"Quake Champions":{"name":"Quake Champions","hours":0,"vac":false},"S.K.I.L.L. - Special Force 2":{"name":"S.K.I.L.L. - Special Force 2","hours":0,"vac":false},"Saints Row 2":{"name":"Saints Row 2","hours":0,"vac":false},"Save the Lamb":{"name":"Save the Lamb","hours":0,"vac":false},"Shoot Paint":{"name":"Shoot Paint","hours":0,"vac":false},"Sniper Fury":{"name":"Sniper Fury","hours":0,"vac":false},"Soldier of Failure":{"name":"Soldier of Failure","hours":0,"vac":false},"Team Fortress 2":{"name":"Team Fortress 2","hours":"2.6","vac":false},"The Last Hope":{"name":"The Last Hope","hours":0,"vac":false},"Totally Accurate Battlegrounds":{"name":"Totally Accurate Battlegrounds","hours":0,"vac":false},"Unforgiving Trials: The Space Crusade":{"name":"Unforgiving Trials: The Space Crusade","hours":0,"vac":false},"Unturned":{"name":"Unturned","hours":"0.7","vac":false},"World of Tanks Blitz":{"name":"World of Tanks Blitz","hours":"128.1","vac":false}}', true);

$keys = array_map('strtolower',array_keys($json));

$search = [
    "counter-strike: global offensive",
    "playerunknown's battlegrounds",
];

$prices = [
    0 => ['value' => ["counter-strike: global offensive"], 'price' => 60],
    1 => ['value' => ["playerunknown's battlegrounds"], 'price' => 75],
    2 => [
        'value' => [
            "counter-strike: global offensive",
            "playerunknown's battlegrounds"
        ],
        'price' => 85
    ]
];
$find = array_intersect($keys, $search);
$price = array_reduce($prices, function($price, $item) use ($find) {
    if (empty(array_diff($item['value'], $find))) {
        $price = $item['price'];
    }
    return $price;
}, 0);
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы