<?php
error_reporting(-1);
ini_set('display_errors', 1);
$json_data = <<<JSON
{"messages": [
{
"id": 1,
"customer": "Dr. Kane Hill",
"customer_id": 1,
"created_at": "2024-11-19 11:18:57",
"text": "Hello, how are you?"
},
{
"id": 2,
"customer": "Dr. Kane Hill",
"customer_id": 1,
"created_at": "2024-11-19 11:20:57",
"text": "I am good, thanks! How about you?"
},
{
"id": 3,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:21:57",
"text": "Hey, what time is it?"
},
{
"id": 4,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:22:57",
"text": "It is 3 PM."
},
{
"id": 5,
"customer": "Shad Leffler",
"customer_id": 3,
"created_at": "2024-11-19 11:23:57",
"text": "Did you finish the project?"
},
{
"id": 6,
"customer": "Shad Leffler",
"customer_id": 3,
"created_at": "2024-11-19 11:24:57",
"text": "New mesh"
},
{
"id": 7,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:26:57",
"text": "Not bed?"
},
{
"id": 8,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:27:57",
"text": "Cool?"
}
]}
JSON;
$messages = json_decode($json_data, true)['messages'];
$messages2 = [];
$messageCounter = 0;
foreach($messages as $row) {
$key = $row['customer_id'];
if(!isset($messages2[$key])) {
$messages2[$key] = [
'customer' => $row['customer'],
'message_id' => (++$messageCounter),
'customer_id' => $row['customer_id'],
'messages' => [],
];
}
$messages2[$key]['messages'][] = [
'id' => $row['id'],
'created_at' => $row['created_at'],
'text' => $row['text'],
];
}
$messages2 = array_values($messages2);
echo json_encode($messages2, JSON_PRETTY_PRINT);
//TRANSLIT
или //IGNORE
₽
или ₽
. Естественно если при отображении данных делается escaping для html (html_entity_encode / htmlspecialchars) это надо будет учесть чтобы &
не превратился в &
echo json_encode(['url' => $response['confirmation']['confirmation_url']]);
exit;
window.location
<?php
class Product
{
public function __construct(
public int $id,
public ?int $parentId = null
) {
}
public function getParentId(): ?int
{
return $this->parentId;
}
}
$obj = new Product(1);
if (!empty($obj->getParentId())) {
echo 'ok';
}
public $parentId;
ошибки не будет. Всё как и написано в тексте ошибки, нельзя обратиться к типизированному свойству до его инициализации, а значит нужно либо задать значение по умолчанию при описании свойства, либо через конструктор, либо вызвав setter.<?php
$input = [
'28.06.2024' => ['Петров Петр Петрович', 'Петров Петр Петрович'],
'03.07.2024' => ['Петров Петр Петрович', 'Иванов Иван Иванович', 'Петров Петр Петрович', 'Иванов Иван Иванович'],
'02.07.2024' => ['Петров Петр Петрович', 'Иванов Иван Иванович'],
'01.07.2024' => ['Иванов Иван Иванович', 'Петров Петр Петрович', 'Иванов Иван Иванович'],
'26.06.2024' => ['Петров Петр Петрович', 'Петров Петр Петрович'],
'04.07.2024' => ['Иванов Иван Иванович']
];
$names = [];
foreach($input as $rows) {
foreach($rows as $name) {
$names[$name] = $name;
}
}
$names = array_values($names);
var_dump($names[0]);
var_dump($names[1]);
<?php
$pathUrl = dirname(__DIR__, 1) . "/orders/";
$filename = 'listOrderFiles.txt';
$lines = [];
//считываем все строки файла в массив
$handle = fopen($pathUrl . $filename, "r");
while (($line = fgets($handle)) !== false) {
$lines[] = $line;
}
fclose($handle);
//удаляем первую строку
unset($lines[0]);
//записываем всё оставшееся в файл
$handle = fopen($pathUrl . $filename, "w");
foreach($lines as $line) {
fwrite($handle, $line);
}
fclose($handle);
<?php
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
//один ___, два ___, много ___
$string = array(
'y' => ['год', 'года', 'лет'],
'm' => ['месяц', 'месяца', 'месяцев'],
'w' => ['неделя', 'недели', 'недель'],
'd' => ['день', 'дня', 'дней'],
'h' => ['час', 'часа', 'часов'],
'i' => ['минута', 'минуты', 'минут'],
's' => ['секунда', 'секунды', 'секунд'],
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . pluralize($diff->$k, $v);
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' назад' : 'только что';
}
function pluralize($n, array $forms)
{
return
$n%10==1 && $n%100!=11
? $forms[0] :
( $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20)
?$forms[1] : $forms[2]
);
}
?>
<?php echo time_elapsed_string('2019-04-22 00:22:35'); ?>
$token = htmlspecialchars(isset($_POST['token']));
$message = htmlspecialchars(isset($_POST['message']));
$subtopic_id = intval(isset($_POST['subtopic_id']));
<?php
$token = htmlspecialchars($_POST['token'] ?? '');
$message = htmlspecialchars($_POST['message'] ?? '');
$subtopic_id = intval($_POST['subtopic_id'] ?? 0);
Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide. Alternatives to this function include:
with recursive cte as (
select
'2024-04-16' dt,
'2024-05-16' end_dt
union all
select dt + interval 1 day, end_dt from cte where dt < end_dt
)
select c.dt, count(t.id)
from cte c
left join sells t on DATE(t.datetime) = c.dt
group by c.dt
media Array of InputMediaAudio, InputMediaDocument, InputMediaPhoto and InputMediaVideo
* Request::sendMediaGroup([
* 'media' => [
* new InputMediaPhoto(['media' => Request::encodeFile($local_photo_1)]),
* new InputMediaPhoto(['media' => Request::encodeFile($local_photo_2)]),
* new InputMediaVideo(['media' => Request::encodeFile($local_video_1)]),
* ],
* ]);
* and even
* Request::sendMediaGroup([
* 'media' => [
* new InputMediaPhoto(['media' => $local_photo_1]),
* new InputMediaPhoto(['media' => $local_photo_2]),
* new InputMediaVideo(['media' => $local_video_1]),
* ],
* ]);
<?php
$html = '<select name="appointments[consulate_appointment][facility_id]" id="appointments_consulate_appointment_facility_id" class="required"><option value="" label=" "></option>
<option data-collects-biometrics="false" value="89">Calgary</option>
<option data-collects-biometrics="false" value="90">Halifax</option>
<option data-collects-biometrics="false" value="91">Montreal</option>
<option data-collects-biometrics="false" value="92">Ottawa</option>
<option data-collects-biometrics="false" value="93">Quebec City</option>
<option data-collects-biometrics="false" value="94">Toronto</option>
<option data-collects-biometrics="false" selected="selected" value="95">Vancouver</option></select>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$result = $xpath->query("//select/option[@selected='selected']");
$value = $result->item(0)->getAttribute('value');
var_dump($value); //string(2) "95"