$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);
}
);
<?php
$text = "Не следует, однако, забывать о том, что начало [повседневной] работы по [формированию позиции] влечет за собой [процесс внедрения и [модернизации] системы обучения кадров!";
preg_match_all("/\[(\S+)\]/", $text, $matches);
var_dump($matches[1]);
<?php
$str1 = 'Привет';
$str2 = 'test';
$max_len = max(mb_strlen($str1),mb_strlen($str2));
echo str_pad(
$str1,
$max_len,
" ",
STR_PAD_BOTH
) . PHP_EOL ;
echo str_pad(
$str2,
$max_len,
" ",
STR_PAD_BOTH
) . PHP_EOL ;
<?php
$string = 'This is my habr';
$pattern = '/(\w+ \w+)$/i';
$replacement = '<a href="habr.com">${1}<a>';
echo preg_replace($pattern, $replacement, $string);
$last_city = ''; // initial empty value
foreach($cities as $city) {
if ($last_city != '') { // if value not empty show transit option
echo '<option value="'.$last_city.'-'.$city['name'].'">'.$last_city.'-'.$city['name'].'</option>'. PHP_EOL;
}
echo '<option value="'.$city['name'].'">'.$city['name'].'</option>' . PHP_EOL;
$last_city = $city['name']; // store value for next iteration
}
<?php
header( 'Content-type: text/html; charset=utf-8' );
echo 'Begin ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
echo $i . '<br />';
flush();
ob_flush();
sleep(1);
}
echo 'End ...<br />';
?>
<?php
$a = 717.83;
$b = 701.26;
$percent = (($a - $b) / $b) * 100;
echo $percent;
if ($percent >3 ) {
echo ' Percent more then 3';
} else {
echo ' Percent less or equal then 3';
}
$stmt = $mysqli->prepare("INSERT INTO coins_info_table (coin_name_id) VALUES (?)");
$stmt->bind_param("s", $id);
foreach ($coin_name_id as $id) {
$stmt->execute();
}
SELECT
`authors`.`id`, `authors`.`last_name`, `authors`.`first_name`, COUNT(*) `author_count`
FROM `bookissue`
JOIN `books` ON `books`.`id` = `bookissue`.`idBook`
JOIN `authors` ON `authors`.`id` = `books`.`idAuthor`
WHERE YEAR(`bookissue`.`data`) = ?
GROUP BY `authors`.`id`, `authors`.`last_name`, `authors`.`first_name`
ORDER BY `author_count` DESC
LIMIT 1
<?php
function saveEmail($contactId, $email){
global $pdo;
try {
$stmt = $pdo->prepare("
INSERT INTO emails(contact_id, email)
VALUES (:contact_id, :email)");
$stmt-> bindValue(':contact_id', $contactId);
$stmt-> bindValue(':email', $email);
return $stmt-> execute();
} catch (PDOException $Exception ) {
print_r($Exception);
return false;
}
}
saveEmail(166, 'no@mail.com');
<?php
$query = "select * from equipment";
$stmt = $pdo->prepare($query);
$stmt->execute();
$equipments = $stmt->fetchAll(PDO::FETCH_ASSOC);
$result = array_reduce(
$equipments,
function($res, $el) {
if(!is_array($res[$el["equipment_id"]])) {
$res[$el["equipment_id"]] = [
"equipment_id" => 141491,
"speed" => []
];
}
array_push(
$res[$el["equipment_id"]]["speed"],
[
"datetime" => $el["datetime"],
"value" => $el["speed"],
"distance" => $el["distance"],
]
);
return $res;
},
[]
);
var_export(array_values($result));
<?php
$query = "select
equipment_id,
json_arrayagg(
json_object(
'datetime', `datetime`,
'value', `speed`,
'distance', `distance`
)
) speed
from equipment
group by equipment_id;";
$stmt = $pdo->prepare($query);
$stmt->execute();
$equipments = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_export($equipments);
<?php
$text = 'Это первая строка текста.
А это вторая строка.
А здесь третья строка.';
// split to rows
$rows=explode(PHP_EOL, $text);
//get first 2 rows
$first2rows = array_slice($rows, 0, 2);
//implode to new text
$newtext = implode(PHP_EOL,$first2rows);
var_export($newtext);
<?php
$operator = "Borat";
// Check if operator exists
$stmt = $pdo->prepare(
"SELECT id_operator FROM operators WHERE name_operator =:name_operator;"
);
$stmt->execute(["name_operator" => $operator]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
// If not exists store new record
if (!$result) {
$sth = $pdo->prepare(
"INSERT INTO operators (name_operator) VALUES (:name_operator);"
);
$sth->execute(["name_operator" => $operator]);
$id_operator = $pdo->lastInsertId();
} else {
$id_operator = $result["id_operator"];
}
echo "id_operator: $id_operator";
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
)
)
)
foreach($arClinics as $id => $Clinic) {
$arClinics[$id] = array_filter(
$Clinic,
function($key) {
return $key == 'ID' || $key == 'PROPERTY_SPECIALIZATIONS_VALUE';
},
ARRAY_FILTER_USE_KEY
);
}
$check_alaliable_amount = 'select * from users where id = ?';
$stmt = $mysqli->prepare($check_alaliable_amount);
$stmt->bind_param("i", $client);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
print_r($user);
if ($user['amount'] >= $summa) {
/* Start transaction */
$mysqli->begin_transaction();
try {
$stmt = $mysqli->prepare("update users set amount=(amount-?) where id=?;");
$stmt->bind_param('ii', $summa, $client);
$stmt->execute();
$stmt = $mysqli->prepare("update users set amount=(amount+?) where id=?;");
$stmt->bind_param('ii', $summa, $shop);
$stmt->execute();
$mysqli->commit();
} catch (mysqli_sql_exception $exception) {
$mysqli->rollback();
throw $exception;
}
}