<?php
$raw = '{
"lastUpdateId":4409859389,
"bids":[
["2.13000000","6472.90000000"],
["2.12900000","50106.20000000"],
["2.12800000","63127.60000000"],
["2.12700000","31495.40000000"],
["2.12600000","41493.30000000"]
],
"asks":[
["2.13100000","24755.90000000"],
["2.13200000","86227.50000000"],
["2.13300000","58302.20000000"],
["2.13400000","61187.90000000"],
["2.13500000","39494.50000000"]
]
}';
$data = json_decode($raw, true);
$bids = array_column($data["bids"], 1);
$asks = array_column($data["asks"], 1);
echo "Max bid: " . max($bids) ,PHP_EOL;
echo "Max ask: " . max($asks) ,PHP_EOL;
$str = '[id618892552|Victoria Tran]';
list($id, $name) = explode('|', trim($str, ']['));
$json = '
{
"program_id": "CC1234",
"offerings": [{
"offering_id": "RP445555",
"location_id": "B2222"
}],
"member_id": "10000-01",
"registration_type": "online",
"dry_run": false
}';
$php_array = json_decode($json, true);
var_export($php_array);
array (
'program_id' => 'CC1234',
'offerings' =>
array (
0 =>
array (
'offering_id' => 'RP445555',
'location_id' => 'B2222',
),
),
'member_id' => '10000-01',
'registration_type' => 'online',
'dry_run' => false,
)
<?php
$a=17;
if ($a < 0 ) {
if ($a % 2 === 1) {
echo "$a is negative odd";
} else {
echo "$a is negative even";
}
} else {
if ($a % 2 === 1) {
echo "$a is positive odd";
} else {
echo "$a is positive even";
}
}
function positive_or_negative($a) {
return ($a < 0 ) ? 'negative' : 'positive';
}
function odd_or_even($a) {
return ($a % 2 === 1 ) ? 'odd' : 'even';
}
echo "$a is "
. positive_or_negative($a)
. " "
. odd_or_even($a);
$con = mysqli_connect("localhost", "root", "", "dbname");
$result = mysqli_query($con, 'SELECT * FROM nomen');
$fp = fopen('file.csv', 'w');
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
fputcsv($fp, $row);
}
fclose($fp);
<?php
require "index.html";
header("Content-type: text/plain; charset=utf-8");
if (
$_SERVER['REQUEST_METHOD'] == "POST" &&
isset($_POST["name"]) &&
isset($_POST["message"])
) {
$to = "damirgaliev587@gmail.com";
$tema = "Форма обратной связи на PHP";
$message = "Ваше имя: " . $_POST["name"] . "<br>";
$message .= "E-mail: " . $_POST["email"] . "<br>";
$message .= "Номер телефона: " . $_POST["phone"] . "<br>";
$message .= "Сообщение: " . $_POST["message"] . "<br>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
mail($to, $tema, $message, $headers);
}
?>
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));
}
}
function group_days($block) {
$week = array('Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб');
$res = [];
foreach ($block as $id=>$day) {
if ($id == 0) {
$period = [$day];
} else {
if (array_search($day, $week)-array_search(end($period), $week)==1) {
$period[1] = $day;
} else {
$res[] = $period;
$period = [$day];
}
}
}
$res[] = $period;
$res = array_map(
function($el) {return implode('-', $el);},
$res
);
return implode(', ', $res);
}
$decoded = json_decode($result, true);
var_dump($decoded['success']);
$result = array_map(
function($el) {
return explode('-', $el); // превращаем каждую пару в массив
},
explode(',', $text) // разбиваем на пары по запятой
);
<?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
бла-бла-бла ...
если (пользователь с таким именем существует) {
сообщение об ошибке
переход на страницу регистрации
закончили скрипт
}
если (пароль не совпадает с подтверждением) {
сообщение об ошибке
переход на страницу регистрации
закончили скрипт
}
если (не смогли переместить файл с аватаркой) {
сообщение об ошибке
переход на страницу регистрации
закончили скрипт
}
сохраняем нового пользователя в базу данных
сообщение об успешной регистрации
переход на страницу логина
закончили скрипт
?>
<?php
require_once 'vendor/connect.php';
$login = mysqli_real_escape_string($connect, $_SESSION["login"]);
$query = mysqli_query($connect, "SELECT * FROM `users` WHERE login = '$login'");
$balance = mysqli_fetch_assoc($query);
echo 'Balance:' . $balance['balance']. PHP_EOL;
if(isset($_POST['click'])) {
mysqli_query($connect, "UPDATE `users` SET `balance`=`balance`+0.001 WHERE login = '$login'");
echo 'New balance:' . ($balance['balance'] + 0.001) . PHP_EOL;
}
?>
<?php
require_once 'vendor/connect.php';
/* prepare statement */
$stmt = $connect->prepare("SELECT `balance` FROM `users` WHERE login = ? ");
/* bind parameters for login */
$stmt->bind_param("s", $_SESSION["login"]);
/* execute query */
$stmt->execute();
$stmt->bind_result($balance);
/* fetch value */
$stmt->fetch();
/* free result to next use */
$stmt->free_result();
echo 'Balance:' . $balance . PHP_EOL;
if (isset($_POST['click'])) {
/* prepare statement */
$stmt = $connect->prepare("UPDATE `users` SET `balance`=`balance`+0.001 WHERE login = ? ");
/* bind parameters for login */
$stmt->bind_param("s", $_SESSION["login"]);
/* execute query */
$stmt->execute();
echo 'New balance:' . ($balance['balance'] + 0.001) . PHP_EOL;
}
?>
$stmt = $conn->prepare(
"UPDATE `sortlist`
SET
`count1` ='count1' + ? ,
`count2` ='count2' + ? ,
`count3` ='count3' + ?
WHERE `id`= ? ;"
);
$stmt->bind_param('iiis', $count1, $count2, $count3, $row);
foreach ($data as $value) {
list($row, $col) = explode("-", $value['id']);
$count1 = 0;
$count2 = 0;
$count3 = 0;
$column = "count" . intval($col);
$$column = $value["count"];
echo "$count1, $count2, $count3, $row";
$stmt->execute();
}
$prod = [
'x' => new Phone('Iphone X ', '$1500 ', 'Phone ', 'Apple ', 'A9 ', '1024 ', '1 ', '128gb ', 'Ios '),
'mon' => new Monitor('Abracadabra ', '$1500 ', 'Monitor ', 'DELL ', '24 ', '144ghz ', 'HDMI '),
'mon1' => new Monitor('ROG ', '$1500 ', 'Monitor ', 'ASUS ', '24 ', '144ghz ', 'HDMI ')
];
foreach($prod as $p) $p->show();
<?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]]));