$res = array_filter(
$arr,
fn($el)=>!in_array($el["uniq_id"], $filter)
);
<?php
$input = 187;
$output = 5;
$buf=log ( intdiv($input, $output ),2 );
echo(round ( $buf, 0 , PHP_ROUND_HALF_UP ) );
SELECT * FROM `test` WHERE `time` >= (now() - INTERVAL 10 MINUTE);
$json = '{
"result": {
"Random ItemName": [
{
"user": "user1",
"time_upd": "2020-12-09 12:25:03"
}, {
"user": "user2",
"time_upd": "2020-12-09 12:25:03"
}, {
"user": "user3",
"time_upd": "2020-12-09 12:25:03"
}
]
}
}';
$data = json_decode($json, true);
$i = 0; // меняется в цикле
$user_name = $data['result']['Random ItemName'][$i]['user'];
$time_upd = $data['result']['Random ItemName'][$i]['time_upd'];
$user_name = $data['result'][array_key_first($data['result'])][$i]['user'];
$time_upd = $data['result'][array_key_first($data['result'])][$i]['time_upd'];
foreach ($data['result'] as $arrayKey => $arrayValue) {
foreach ($arrayValue as $arraySubvalue) {
$user_name = $arraySubvalue['user'];
$time_upd = $arraySubvalue['time_upd'];
// …
}
}
$encodedData = '{"result":[{"id":"103513","user":"user_repeat","time_upd":"2020-12-09 08:45:02"},{"id":"103517","user":"user_repeat","time_upd":"2020-12-09 08:45:02"},{"id":"103522","user":"user_no_repeat","time_upd":"2020-12-09 08:45:02"}]}';
$decodedData = json_decode($encodedData, true);
$userRepeats = [];
foreach ($decodedData['result'] as $info) {
if (!array_key_exists($info['user'], $userRepeats)) {
$userRepeats[$info['user']] = 0;
}
$userRepeats[$info['user']]++;
}
arsort($userRepeats);
$mexRepeatsUser = [key($userRepeats)=>current($userRepeats)];
var_dump($userRepeats);
var_dump($mexRepeatsUser);
$out_users = [];
foreach($array_decode['response'] as $user)
$out_users[$user['id']]['user'] = $user['user'];
$out_users = [];
foreach($array_decode['response'] as &$user) // обратите внимание на доступ по ссылке & к элементу массива
{
$out_users[$user['id']]['user'] = $user['user'];
unset($user); //удаляем элемент из массива $array_decode['response'], чтобы не росло использование памяти.
}
const str = `{"response": {"one": {"count": "2","total": "13700","rest": "31500","down": "2500"},"two": {"count": "1","total": "25000","rest": "50000","down": ""},"three": {"count": "2","total": "50000","rest": "5000","down": ""}}}`
const obj = JSON.parse(str);
let sum = 0;
for (let key in obj.response) {
sum += +obj.response[key].count;
}
console.log(sum); //5
<?php
$values = [
['value' => 'One', 'weight' => 20],
['value' => 'Two', 'weight' => 30],
['value' => 'Three', 'weight' => 50]
];
function randByWeight(array $arr) {
$max = 0;
$result = [];
foreach($arr as $value) {
$rand = pow((mt_rand() / (mt_getrandmax() + 1)), 1/$value['weight']);
if ($rand > $max) {
$max = $rand;
$result = $value;
}
}
return $result;
}
// Например: 'Three' выпадет в 50% случаев, тк его вес -- половина от суммы всех весов
// Например: 'Two' выпадет в 30% случаев, тк его вес -- 30% от суммы всех весов
var_dump(randByWeight($values));
SELECT * FROM table ORDER BY POWER(random(), 1/weight) DESC LIMIT 1
<?php
$values = [
['value' => 'One', 'weight' => 20],
['value' => 'Two', 'weight' => 30],
['value' => 'Three', 'weight' => 50]
];
function randByWeight(array $items) {
$sum = array_reduce($items, function(int $acc, array $item): int {
return $acc += $item['weight'];
}, 0);
$rand = (mt_rand() / (mt_getrandmax() + 1)) * $sum;
foreach($items as $item) {
if($rand < $item['weight']) {
return $item;
}
$rand -= $item['weight'];
}
}
// Например: 'Three' выпадет в 50% случаев, тк его вес -- половина от суммы всех весов
// Например: 'Two' выпадет в 30% случаев, тк его вес -- 30% от суммы всех весов
var_dump(randByWeight($values));