<?php
$list = [
['number' => 3],
['number' => 2],
['number' => 45],
];
// 1st example
echo array_sum(array_column($list, 'number')), PHP_EOL;
// 2nd example
echo array_reduce($list, function ($carry, $item) {
return $carry += $item['number'];
}), PHP_EOL;
// 3rd example
array_walk($list, function ($item) use (&$sum) {
$sum += $item['number'];
});
echo $sum, PHP_EOL;
// 4th example
array_map(function ($item) use (&$sum2) {
$sum2 += $item['number'];
}, $list);
echo $sum2, PHP_EOL;
$allowedAttributes = ['src', 'href'];
foreach($elements as $element) {
if( $element->attributes ) {
for($i = $element->attributes->length; --$i >= 0;) {
if(!in_array($element->attributes->item($i)->name, $allowedAttributes)) {
$element->removeAttribute($element->attributes->item($i)->name);
}
}
}
}
echo $dom->saveHTML();
namespace testnamespace;
class Test {
public function __construct() {
echo 'Hi';
}
}
new \Test; // Fatal error: Uncaught Error: Class 'Test' not found
class_alias('\testnamespace\Test', 'GlobalTest');
new \GlobalTest; // Hi
$data = json_decode($data);
$data = array_column($data, 1);
$result = [];
for($i = 1, $size = count($data); $i < $size; ++$i) {
$result[$i] = $data[$i] - $data[$i - 1];
}
https://ideone.com/ZGf0iR > echo hash('sha256', 'bla-bla-bla');
> e29256c37ac614866b41c51eb8bf013d2f0b208988a21016e0abf6e81fc99c58
$html = <<<'HTML'
<article>
<p rel="#" src="#">aaaaaa</p>
<p href="#" bla="#" bla2="#">bbbbbb</p>
</article>
HTML;
$dom = new DOMDocument;
@$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DomXPath($dom);
$elements = $xpath->query('//article/*');
$allowedAttributes = ['src', 'href'];
foreach($elements as $element) {
for( $i = $element->attributes->length; --$i >= 0; ) {
if( ! in_array( $element->attributes->item($i)->name, $allowedAttributes ) ) {
$element->removeAttribute($element->attributes->item($i)->name);
}
}
}
echo $dom->saveHTML();
https://3v4l.org/UPrsr $date = DateTime::createFromFormat('Y-m-d H:i:s', '2018-09-12 19:00:23');
echo $date->format('d F Y');