1.trim
rtrim() 从字符串右侧移除字符:
ltrim() - 移除字符串左侧的空白字符或其他预定义字符
trim() - 移除字符串两侧的空白字符或其他预定义字符
<?php
$str = "Hello World!";
echo $str . "<br>"; //Hello World!
echo rtrim($str,"World!"); // Hello
?>
2.sprintf
把百分号(%)符号替换成一个作为参数进行传递的变量:
<?php
$number = 2;
$str = "Shanghai";
$txt = sprintf("There are %u million cars in %s.",$number,$str);
echo $txt;
?>
//There are 2 million cars in Shanghai.
3.array_slice
从数组的第三个元素开始取出,并返回数组中的其余元素:
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>
//Array ( [0] => blue [1] => yellow [2] => brown )
4.array_shift
删除数组中的第一个元素(red),并返回被删除元素的值:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);//red
print_r ($a);//Array ( [b] => green [c] => blue )
?>
5.array_unshift
在数组头部加入元素
<?php
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
print_r($a);
?>
//Array ( [0] => blue [a] => red [b] => green )
6.array_walk
<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"myfunction");
?>
//结果
The key a has the value red
The key b has the value green
The key c has the value blue
发
if ($goodsList) {
array_walk($goodsList, function ($goods) use (&$goodsListArr) {
$giftId = get_property($goods, 'giftId');
$giftSpecId = get_property($goods, 'giftSpecId');
$gift = GiftLogic::getInstance()->getDetail($giftId);
$giftSpec = GiftLogic::getInstance()->getGiftSpecDetail($giftSpecId);
$goodsListArr[] = [
'idCode' => IdCodeUtil::encode(get_property($goods, 'id')),
'giftName' => get_property($gift, 'name'),
];
});
$goodsNums = array_sum(array_column($goodsList, 'totalNums'));
$sellNums = array_sum(array_column($goodsList, 'sellNums'));
}
unset($goodsList);
7.array_column
获取数组某一列的值
<?php
// 表示由数据库返回的可能记录集的数组
$a = array(
array(
'id' => 5698,
'first_name' => 'Bill',
'last_name' => 'Gates',
),
array(
'id' => 4767,
'first_name' => 'Steve',
'last_name' => 'Jobs',
),
array(
'id' => 3809,
'first_name' => 'Mark',
'last_name' => 'Zuckerberg',
)
);
$last_names = array_column($a, 'last_name');
print_r($last_names);
?>
8.array_sum
返回数组中所有值的和(5+15+25):
<?php
$a=array(5,15,25);
echo array_sum($a);
?>