echo ($osName == 'Mac' || $osName == 'Linux') ? $auto_link[$osName] : $auto_link['Default'];
echo in_array($osName,['Mac', 'Linux']) ? $auto_link[$osName] : $auto_link['Default'];
foreach( $arr as $key=>$value ) {
if ($value[1]) {
printf("%s %s\n", $value[0], $value[2]);
}
}
// Перебор массива
foreach( $arr as $key=>$value ) {
if ($$key) {
printf("%s %s\n", $value[0], ucfirst($key));
}
}
<?php
$items = [
['ID' => 80433, 'post_title' => 'Консоль из нержавеющей 1'],
['ID' => 80428, 'post_title' => 'Комод из неражавеющей 2']
];
$categories = ['Консоль', 'Комод'];
$result = [];
foreach($categories as $category) {
$results[$category] = [];
foreach($items as $item) {
if (mb_strpos(mb_strtolower($item['post_title']), mb_strtolower($category)) !== false) {
$result[$category][] = $item;
}
}
}
print_r($result);
<?php
function cut($num, $remove_intervals) {
// sort remove intervals by first value
usort($remove_intervals, function($a, $b){return $a[0] <=> $b[0];});
// merge overlapped intervals
$remove = array_reduce(
$remove_intervals,
function($res, $el) {
$cnt = count($res)-1;
if ($el[0] <= $res[$cnt][1]) {
$res[$cnt][1] = max($el[1], $res[$cnt][1]);
} else {
$res[] = $el;
}
return $res;
},
[$remove_intervals[0]]
);
// build result array
$result = array_reduce(
$remove,
function($res, $el) use ($num) {
$cnt = count($res)-1;
if ($el[0] < $res[$cnt][1]) {
$res[$cnt][1] = $el[0];
if ($el[1] < $num) {
$res[] = [$el[1], $num];
}
}
return $res;
},
[[1, $num]]
);
var_export($result);
return $result;
}
print_r(cut(10000, [[30, 500], [700, 900], [800, 1000]]));
print_r(cut(10000, [[30, 500], [1200, 3700], [50, 700], [1000, 3000], [6000, 20000]]));
print_r(cut(1000, [[30, 500], [20, 7000], [10, 35]]));
$items = [
'school_table' => [
'name' => 'School Table',
'price' => 100
],
'school_chair' => [
'name' => 'School Chair',
'price' => 50
]
];
$item = $items[ 'school_table'];
echo $item['name'] . ' for ' . $item['price'] . ' money';
$values = ['AF', 'AL'];
$result = array_filter(
$countries,
function($el) use ($values) {
return in_array($el['iso'], $values);
}
);
sort($arr['2021.06.25']['type']);
print_r($arr);
Array
(
[2021.06.25] => Array
(
[type] => Array
(
[0] => 06:00:00
[1] => 13:20:00
[2] => 20:45:00
)
)
)
<?php
$results = [
['order_id' => 'first', 'id'=>1],
['order_id' => 'second', 'id'=>4],
['order_id' => 'third', 'id'=>7]
];
$filtered = array_filter(
$results,
function($el) {
return $el['id'] != 4;
}
);
var_export($filtered);