<?php
function formatTree($tree, $parent)
{
$tree2 = array();
foreach ($tree as $item) {
if ($item['parent_id'] == $parent) {
$tree2[$item['id']] = $item;
$tree2[$item['id']]['child'] = formatTree($tree, $item['id']);
}
}
return $tree2;
}
//for demo
$beforeTree = [
['id' => 1, 'parent_id' => 0],
['id' => 2, 'parent_id' => 1],
['id' => 3, 'parent_id' => 2],
['id' => 4, 'parent_id' => 0],
['id' => 5, 'parent_id' => 4],
['id' => 6, 'parent_id' => 4],
];
$afterTree = formatTree($beforeTree, 0);
var_dump($afterTree);
Tuesday, March 14, 2017
PHP format tree comments, categories, nested sets
Simple function for formatting tree. One cons - you need to pass all items (all comments of the post) to format them.
Monday, February 6, 2017
Clear Laravel configuration cache
php artisan config:clear
Wednesday, January 4, 2017
Redis stop Redis start
if you have properly installed redis as server just run
sudo systemctl restart redis
If not casually:
to stop redis
sudo systemctl restart redis
If not casually:
to stop redis
redis-cli shutdownto start redis
redis-server
Saturday, December 31, 2016
Laravel insert ignore check if inserted (affected rows for insert ignore)
Halo guys!
if you break your head for finding solution to check if row is inserted here is the way
The solution is simle - not use insert ignore. So we are catching exception instead of fatal erroring and know if it did or not.
You can even do query builder insert instead of raw query.
if you break your head for finding solution to check if row is inserted here is the way
$query = "INSERT INTO table (id) VALUES (?)";
try {
DB::insert($query, array($value));
return 1;
} catch (\Exception $e) {
return 0;
}
The solution is simle - not use insert ignore. So we are catching exception instead of fatal erroring and know if it did or not.
You can even do query builder insert instead of raw query.
Tuesday, December 27, 2016
Git error: Your local changes to the following files would be overwritten by merge
To fix this error type:
git reset --hard HEAD
Thursday, December 22, 2016
Nginx block default
server {
return 404;
}
Sunday, November 13, 2016
SQL select where join not existed; Select where can't join
SELECT t1.ID
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL
source: http://stackoverflow.com/a/4076157/1767461
Subscribe to:
Posts (Atom)