$db->beginTransaction();
if ($query->execute()) {
if ($query->execute()) {
$db->commit();
return true;
}
}
$db->rollback();
return false;
<?php
function parse($text) {
$lines = explode("\n", $text);
$result = [];
$switched = false;
$i = 0;
foreach($lines as $line) {
// Add item
if (count($matches = preg_split('/->/', $line, -1, PREG_SPLIT_NO_EMPTY)) == 2) {
if (!isset($result[$i])) {
$result[$i] = [];
}
$result[$i][$matches[0]] = $matches[1];
$switched = false;
} else if (!$switched) {
$i++;
$switched = true;
}
}
return $result;
}
$text = "
_wpcf7->104
_wpcf7_version->4.7
_wpcf7_locale->ru_RU
_wpcf7_unit_tag->wpcf7-f104-p157-o3
_wpnonce->2b8273a977
your-name->Krisan Lang
menu-968->Firma
text-463->Best OÜ
tel-179->58813468606
your-email->kridgfgstjan@best5apartments.ee
November 29, 2017, 18:07:14
_wpcf7->104
_wpcf7_version->4.7
_wpcf7_locale->ru_RU
_wpcf7_unit_tag->wpcf7-f104-p67-o1
_wpnonce->2b8273a977
your-name->Liis dfValma
menu-968->Eraisik
text-463->
tel-179->5396643367
your-email->liisvalma2401@gmail.com
";
print_r(parse($text));
/<([a-z]+)>(?<!<p>).+(?!(?:<\/p>|<\/\1>))\{PAGEBREAK\}.+<\/\1>/
preg_replace("/(?P<start><([a-z]+)>(?<!<p>).+(?!(?:<\/p>|<\/\2>)))\{PAGEBREAK\}(?P<end>.+<\/\2>)/", "\1 \3 {PAGEBREAK}", $input_lines);
<?php
$array = [
[
'player_id' => 1,
'count_goals' => 1
],
[
'player_id' => 1,
'count_goals' => 1
],
[
'player_id' => 1,
'count_goals' => 1
],
[
'player_id' => 1,
'count_goals' => 1
],
];
function filter($array)
{
$result = [];
foreach($array as $key => $value) {
if (array_key_exists($value['player_id'], $result)) {
$result[$value['player_id']]['count_goals'] += $value['count_goals'];
} else {
$result[$value['player_id']] = $value;
}
}
return array_values($result);
}
print_r(filter($array));
<?php
$a = [
'color' => ['red', 'blue', 'orange'],
'size' => ['10-12', '12-14', '13-13'],
'somekey' => ['val1', 'val2', 'val3'],
];
function addValue(&$arr, $key, $val)
{
if (!count($arr)) {
$arr[] = [$key => $val];
return;
}
foreach($arr as $num => $values) {
$arr[$num][$key] = $val;
}
}
$b = [];
foreach($a as $key => $values) {
$copyB = $b;
$newB = [];
foreach($values as $value) {
$currentB = $copyB;
addValue($currentB, $key, $value);
$newB = array_merge($newB, $currentB);
}
$b = $newB;
}
print_r($b);
// Steam api key
SteamPlayer::$API_KEY = 'api key';
// Массив идентификаторов стима
$steamIDs = [
'xxxxxxxxxxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxx'
];
// Отдает коллекцию объектов SteamPlayer
$SteamPlayerCollection = SteamPlayer::Create($steamIDs);
// Получаем массив инстансов SteamPlayer[] и выводим ссылку на большой аватара у каждого
foreach($SteamPlayerCollection->get() as $player){
echo $player->avatar(SteamPlayer::AVATAR_LARGE);
}
<?php
class a {
public $a = 1;
public $b = 2;
}
$obj = new a();
foreach ($obj as $value) {
echo $value.'|';
}
//1|2
?>
<?php
class MyIterator implements Iterator
{
private $var = array();
public function __construct($array)
{
if (is_array($array)) {
$this->var = $array;
}
}
public function rewind()
{
echo "перемотка в начало\n";
reset($this->var);
}
public function current()
{
$var = current($this->var);
echo "текущий: $var\n";
return $var;
}
public function key()
{
$var = key($this->var);
echo "ключ: $var\n";
return $var;
}
public function next()
{
$var = next($this->var);
echo "следующий: $var\n";
return $var;
}
public function valid()
{
$key = key($this->var);
$var = ($key !== NULL && $key !== FALSE);
echo "верный: $var\n";
return $var;
}
}
$values = array(1,2,3);
$it = new MyIterator($values);
foreach ($it as $a => $b) {
print "$a: $b\n";
}
?>