Blogger Information
Blog 22
fans 0
comment 0
visits 21730
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
二维数组排序、常用字符串函数(8月27日作业)
岑勋的博客
Original
924 people have browsed it

用usort()对二维数组排序


实例

<?php
/**
 * usort()二维数组的排序
 */
$city = [
    ['city' => '东莞', 'province' => '广东'],
    ['city' => '合肥', 'province' => '安徽'],
    ['city' => '南京', 'province' => '江苏'],
    ['city' => '泰安', 'province' => '山东'],
];
usort($city, function ($arr1, $arr2) {
   return strcmp($arr1['city'],$arr2['city']);
});
echo '<pre>';
echo var_export($city,true);

运行实例 »

点击 "运行实例" 按钮查看在线实例

常用字符串查找函数:substr(),strstr(),strpos()


实例

<?php
/**
 *  substr(),strstr(),strpos()函数
 */

//substr($str,$offset,[$length]) 根据位置查询子字符串
$str = 'I want to learn PHP well.';
echo substr($str,16).'<br>'; //输出:PHP well.
echo substr($str,16,-3).'<br>';  //输出:PHP
echo substr($str,-5).'<br>'; //输出:well.
echo substr($str,-5,1).'<br>'; //输出:w
echo substr($str,-5,-1).'<hr>'; //输出:well

//strstr($str,$str2,bool) 根据某一内容来查询,参数二为
$str = 'I want to learn PHP well';
echo strstr($str,'l').'<br>'; //输出:learn PHP well
echo strstr($str,'l',true).'<hr>'; //输出:I want to

//strpos($str,$str2,$offset) 查找字符串首次出现的位置
$str = 'I want to learn PHP well';
echo strpos($str,'l').'<br>'; //返回10

运行实例 »

点击 "运行实例" 按钮查看在线实例

常用字符串替换函数:str_replace()、substr_replace()

实例

<?php
/**
 * 字符串替换函数
 * str_replace()  substr_replace()
 */

/* str_replace($search,$replace,$str) */
//一般替换
$str = 'It\'s a wonderful day';
echo str_replace('wonderful','busy',$str).'<br>';
//一个替换多个
$str = 'I like the PHP,I\'m going to learn PHP';
echo str_replace(['like','going to'],'love',$str).'<br>';
//多个替换多个,一一对应
echo str_replace(['I','PHP'],['You','javacript'],$str).'<br>';
//删除字符串
$str = 'It\'s a wonderful day';
echo str_replace('wonderful','',$str);
echo '<hr>';

/* substr_replace($str,$place,$start,[$length]) */
//全部替换
$str = 'I study PHP on PHP Chinese website';
echo substr_replace($str,'I\'m going to learn PHP',0).'<br>';
//部分替换
echo substr_replace($str,'javascript',8,3).'<br>'; //不指明长度则后面内容删除
//删除内容
echo substr_replace($str,'',12).'<br>';
echo substr_replace($str,'',15,3).'<br>';
//插入内容
echo substr_replace($str,'hefei ',15,0);

运行实例 »

点击 "运行实例" 按钮查看在线实例

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post