Skip to content

Instantly share code, notes, and snippets.

@marlonfan
Last active October 14, 2017 07:30
Show Gist options
  • Save marlonfan/d4f6543a0021797d5f8bd97937c168fa to your computer and use it in GitHub Desktop.
Save marlonfan/d4f6543a0021797d5f8bd97937c168fa to your computer and use it in GitHub Desktop.
php常用代码片段
<?php
/**
* 给一个数组,将他填充到指定长度
* @param array $arr
* @param number $needNumber
* @return array
* @author marlon marlon@tfan.net
*/
function padArray($array, $needNumber)
{
$currentLength = count($arr);
$result = [];
if ($needNumber <= $currentLength) {
return array_slice($arr, 0, $needNumber);
}
for ($i = 0; $i < ceil($needNumber / $currentLength); $i++) {
$result = array_merge($result, $arr);
}
return array_slice($result, 0, $needNumber);
}
<?php
function generateTree($items){
$tree = array();
foreach($items as $item){
if(isset($items[$item['pid']])){
$items[$item['pid']]['son'][] = &$items[$item['id']];
}else{
$tree[] = &$items[$item['id']];
}
}
return $tree;
}
$items = array(
1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);
print_r(generateTree($items));
<?php
$session = ssh2_connect('your host ip', 22);
ssh2_auth_password($session, 'username', 'password');
$stream = ssh2_exec($session, 'php -i');
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
// Whichever of the two below commands is listed first will receive its appropriate output. The second command receives nothing
echo "Output: " . stream_get_contents($stream);
echo "Error: " . stream_get_contents($errorStream);
// Close the streams
fclose($errorStream);
fclose($stream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment