/**
* Return client ip if find, or null
* @return string|null
*/
function getUserIp()
{
static $ip;
if ( !is_null($ip) )
{
return $ip;
}
$candidates = [
@$_SERVER['HTTP_CLIENT_IP'],
@$_SERVER['HTTP_X_FORWARDED_FOR'],
@$_SERVER['REMOTE_ADDR']
];
foreach ($candidates as $candidate)
{
if ( \filter_var($candidate, \FILTER_VALIDATE_IP) )
{
$ip = $candidate;
break;
}
}
return $ip;
}
var_dump(getUserIp());
/**
* Шаг 1 - разворачиваем $users в плоский массив
* Если есть возможность получить его сразу ассоциативным,
* то это было бы лучше
*/
$usersAssoc = [];
foreach ($users as $user)
{
$usersAssoc[ $user['id'] ] = $user['name'];
}
/**
* Шаг 2 - выполяем группирующее преобразование наших $punch
*/
$result = [];
foreach ($punch as $time)
{
if ( !array_key_exists($time['user_id'], $usersAssoc) )
{
// В punch есть массив, который относится к пользователю
// о окотором мы не знаем
continue;
}
$result[ $usersAssoc[ $time['user_id'] ] ][ $time['id'] ] = $time;
}
echo "<pre>";
var_dump($result);
echo "</pre>";
$uncomingString = 'W+I4zLZyeJDAhQMe33tYRcXC+Falj9baMfjJw5yOijaPNHDYcnNlRlTGm3rvA6cn2hXMmW4ZKEyTphwzNMQlOlnNiCHK2LLpJLgXK6JHKuTYeParwqCud5Vn2dZXpxctrQf8OkbCqF12u0GcBR2or2HFBrEV13Jd5YAX4bRbUBk=';
function forceUnicode( $str = '' )
{
$result = '';
for( $i=0,$length=strlen($str); $i<$length; $i++)
{
$result .= "\\u".str_pad(bin2hex($str[$i]), 4, '0', STR_PAD_LEFT);
}
return $result;
}
echo forceUnicode($uncomingString);
/*
Выведет:
\u0057\u002b\u0049\u0034\u007a\u004c\u005a\u0079\u0065\u004a\u0044\u0041\u0068\u0051\u004d\u0065\u0033\u0033\u0074\u0059\u0052\u0063\u0058\u0043\u002b\u0046\u0061\u006c\u006a\u0039\u0062\u0061\u004d\u0066\u006a\u004a\u0077\u0035\u0079\u004f\u0069\u006a\u0061\u0050\u004e\u0048\u0044\u0059\u0063\u006e\u004e\u006c\u0052\u006c\u0054\u0047\u006d\u0033\u0072\u0076\u0041\u0036\u0063\u006e\u0032\u0068\u0058\u004d\u006d\u0057\u0034\u005a\u004b\u0045\u0079\u0054\u0070\u0068\u0077\u007a\u004e\u004d\u0051\u006c\u004f\u006c\u006e\u004e\u0069\u0043\u0048\u004b\u0032\u004c\u004c\u0070\u004a\u004c\u0067\u0058\u004b\u0036\u004a\u0048\u004b\u0075\u0054\u0059\u0065\u0050\u0061\u0072\u0077\u0071\u0043\u0075\u0064\u0035\u0056\u006e\u0032\u0064\u005a\u0058\u0070\u0078\u0063\u0074\u0072\u0051\u0066\u0038\u004f\u006b\u0062\u0043\u0071\u0046\u0031\u0032\u0075\u0030\u0047\u0063\u0042\u0052\u0032\u006f\u0072\u0032\u0048\u0046\u0042\u0072\u0045\u0056\u0031\u0033\u004a\u0064\u0035\u0059\u0041\u0058\u0034\u0062\u0052\u0062\u0055\u0042\u006b\u003d
*/
if (isset($brand)){
if ( !empty($brand) ) {
$query = "SELECT * FROM products WHERE brand = '".$_POST["category"]."' ";
array(2) {
["a"]=>
string(1) "0"
["b"]=>
string(1) "5"
}
$result = [];
$a = (int) $_POST['a'];
$b = (int) $_POST['b'];
$c = array_filter( (array) $_POST['day'] );
if ( !empty($c) )
{
$c = implode(',', $_POST['day']);
$in = str_repeat('?,', count($c) - 1) . '?';
$sql = "SELECT * FROM table WHERE day IN ($in) ASC LIMIT :a, :b";
$stmt = $pdo->prepare($sql);
$stmt->execute($с, [$a, $b]);
$data = $stmt->fetchAll();
$result = $data;
}
echo json_encode($result);
<form id="filter">
<input type='hidden' name='a' value='0' />
<input type='hidden' name='b' value='5' />
<input type="checkbox" name="day[]" value="1">
<input type="checkbox" name="day[]" value="2">
<input type="checkbox" name="day[]" value="3">
</form>
$.ajax({
url: 'index.php',
method: 'POST',
data: $("#filter").serialize()
});
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
$editServiceById = $services->editServiceById($_GET['service_id'], $_POST);
}
// Display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Log values
echo "<pre>";
var_dump($_SERVER['REQUEST_METHOD'], $_POST['edit'], isset($_POST['edit']));
echo "</pre>";
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
$editServiceById = $services->editServiceById($_GET['service_id'], $_POST);
}
/**
* Prepare array for our job
*/
$arrayOne = [
0 => 80,
1 => 80,
2 => 90,
];
$arrayTwo = [
0 => [
0 => 190,
1 => 195,
],
1 => [
0 => 200,
],
2 => [
0 => 190,
1 => 195,
],
];
$arrayThree = [
0 => 1,
1 => 2,
2 => 3,
];
/**
* @var array Array with actual data in our structure
*/
$arResult = [];
foreach ( $arrayTwo as $iKeyOne => $arrayTwoElement )
{
// if in second array exist unknown key
if ( !array_key_exists($iKeyOne, $arrayOne) )
{
continue;
}
// if in third array exist unknown key
if ( !array_key_exists($iKeyOne, $arrayThree) )
{
continue;
}
// if in second array no elements
if ( empty($arrayTwoElement) || !is_array($arrayTwoElement) )
{
continue;
}
/* @var integer Key for first depth level */
$iLevelKey = (int) $arrayOne[ $iKeyOne ];
/* @var integer Value for second depth level */
$iLevelValue = (int) $arrayThree[ $iKeyOne ];
foreach ($arrayTwoElement as $k => $v)
{
$arResult[ $iLevelKey ][ $v ] = $iLevelValue;
}
}
var_dump($arResult);
/*
Will display:
array(2) {
[80]=>
array(3) {
[190]=> int(1)
[195]=> int(1)
[200]=> int(2)
}
[90]=>
array(2) {
[190]=> int(3)
[195]=> int(3)
}
}
*/
encrypt($text, $m);
return encrypt($text, $m);
function encrypt($text, $n)
{
if ( $n < 1 )
{
return $text;
}
$str1 = $str2 = "";
for($i = 0, $len = strlen($text); $i < $len; $i++)
{
if ( $i%2 == 0 )
{
$str2 .= $text[$i];
}
else
{
$str1 .= $text[$i];
}
}
return encrypt( $str1.$str2, ($n-1));
}
echo encrypt('This is a test!', 2);
public static function getTags(array $requestTags): array
{
$tags = [];
foreach ($requestTags as $tag) {
if (is_numeric($tag)) {
$tags[] = $tag;
} else {
$newTag = Tag::upsert(['name' => $tag]);
$tags[] = $newTag->id;
}
}
return $tags;
}
function mapTags( array &$value )
{
if ( is_numeric($value) )
{
return $value;
}
$newTag = Tag::upsert(['name' => $tag]);
return $newTag->id;
}
public static function getTags( array $requestTags ): array
{
return array_map('mapTags', $requestTags);
}
public static function getTags($requestTags): array
{
$tags = [];
if ( !$requestTags )
{
return $tags;
}
foreach ($requestTags as $tag) {
if (is_numeric($tag)) {
$tags[] = $tag;
} else {
$newTag = Tag::upsert(['name' => $tag]);
$tags[] = $newTag->id;
}
}
return $tags;
}
public static function getTags(array $requestTags): array
{
if ( empty($requestTags) )
{
return $requestTags;
}
$existedTags = array_filter($requestTags, 'is_numeric');
$newTags = array_diff($requestTags, $existedTags);
/**
* Тут единый batch-метод который отправляет
* 1 запрос на все теги сразу.
* В results - массив Tag, обработанных batch-методом
*/
foreach ($results as $tag)
{
$existedTags[] = $tag->id;
}
return array_unique($existedTags);
}
<div class="currency">
<ul>
<? foreach ($obj as $element):?>
<? $color = ($element['percent_change_24h'] > 0) '#4ac06a' : '#ff8d8d'; ?>
<? $plus = ($element['percent_change_24h'] > 0) '+' : ''; ?>
<li>
<i class="cc <?=$element["symbol"];?> iconsi" title="<?=$element["name"];?>"></i>
<div class="block_coin">
<span class="coin_name"><?=$element["name"];?></span>
<span style="color: '<?=$color;?>';" class="coin_price"><?=$element['price_usd'];?></span>
<span style="color: '<?=$color;?>';" class="coin_change">(<?=$plus;?><?=$element["percent_change_24h"];?>%)</span>
</div>
</li>
<? endforeach; ?>
</ul>
</div>