php 不常用但實用函式

2021-10-12 13:39:35 字數 2283 閱讀 7339

rtrim() 從字串右側移除字元:

ltrim() - 移除字串左側的空白字元或其他預定義字元

trim() - 移除字串兩側的空白字元或其他預定義字元

<?php

$str = "hello world!";

echo $str . "

"; //hello world!

echo rtrim($str,"world!"); // hello

?>

把百分號(%)符號替換成乙個作為引數進行傳遞的變數:

<?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.

從陣列的第三個元素開始取出,並返回陣列中的其餘元素:

<?php

$a=array("red","green","blue","yellow","brown");

print_r(array_slice($a,2));

?>

//array ( [0] => blue [1] => yellow [2] => brown )

刪除陣列中的第乙個元素(red),並返回被刪除元素的值:

<?php

$a=array("a"=>"red","b"=>"green","c"=>"blue");

echo array_shift($a);//red

print_r ($a);//array ( [b] => green [c] => blue )

?>

在陣列頭部加入元素

<?php

$a=array("a"=>"red","b"=>"green");

array_unshift($a,"blue");

print_r($a);

?>

//array ( [0] => blue [a] => red [b] => green )

<?php

function myfunction($value,$key)

$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) );

$goodsnums = array_sum(array_column($goodslist, 'totalnums'));

$sellnums = array_sum(array_column($goodslist, 'sellnums'));

}unset($goodslist);

獲取陣列某一列的值

<?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);

?>

返回陣列中所有值的和(5+15+25):

<?php

$a=array(5,15,25);

echo array_sum($a);

?>

mysql不常用,但很實用的命令

設 資料庫名 master,master資料庫中的乙個表名 ent datapool 1 進入特定資料庫 例如 use master 2 顯示庫中的所有表名 例如 show master 3 檢視表中所有字段資訊 例如 desc ent datapool 4 查詢 指定 資料庫中含有某個欄位的表有哪...

jquery 不常用但重要的api

1.checkbox 的選中狀態 checkbox click function else 2.map jquery 中有兩個map map 函式用於處理當前jquery物件匹配的所有元素,並將處理結果封裝為新的陣列.一般使用get 得到這個陣列 簡單來說 就是將一組元素轉換為陣列 不論是否是元素陣...

C語言中不常用但非常有用的函式

1.sprintf 函式 int sprintf char buffer,const char format argument 一般有三個引數 緩衝區 你指定的格式 要處理的字串 與printf相比 sprinf 是將結果列印到緩衝區 乙個陣列 而printf 是直接將結果列印到螢幕。例子 把整數1...