数组的操作集合

原创 2018-11-07 15:34:42 308
摘要:<style>     h2,h3,h4{text-align: center;}     h2{border-bottom: 1px solid #dddddd;padding-bottom: 20px;border-bottom: 1px&nb
<style>
    h2,h3,h4{text-align: center;}
    h2{border-bottom: 1px solid #dddddd;padding-bottom: 20px;border-bottom: 1px solid #dddddd;padding-bottom: 20px;}
    h3{color: darkgreen;}
    p{font-weight: bold;color: deeppink;}
    .box{display: flex;}
    .div1{width: 33.3%;border-right: 1px solid #ddd;padding: 10px;}
</style>
<?php

echo '<h2>数组常用操作:</h2>';
echo '<div class="box"><div class="div1"><h3>数组基础操作:</h3>';

$num = [12,1,43,7,9,3,76,34,98];
$per = ['name'=>'莎莎','age'=>23,'salary'=>4500];
$pers = [
    ['name'=>'莎莎','age'=>23,'salary'=>4500],
    ['name'=>'哈哈','age'=>21,'salary'=>2300],
    ['name'=>'菲菲','age'=>25,'salary'=>5000],
    ['name'=>'安安','age'=>18,'salary'=>2000],
];
echo '<pre><p>array_values返回值组成的数组</p>'.var_export(array_values($per),true);
echo '<pre><p>array_keys返回键组成的数组</p>'.var_export(array_keys($per),true);
echo '<pre><p>array_column获取salary列,以name列为键名</p>'.var_export(array_column($pers,'salary','name'),true);
echo '<pre><p>array_flip键值对调</p>'.var_export(array_flip($per),true);
echo '<pre><p>array_reverse数组翻转</p>'.var_export(array_reverse($per),true);
echo '</div>';

echo '<div class="div1"><h3>数组的基础操作:</h3>';
$num1 = [12,7,1,43,7,9,3,76,12,34,98];
$per1 = ['name'=>'莎莎','age'=>23,'salary'=>4500];
echo '<pre><p>array_chunk将数组分割成若干个小数组:</p>'.var_export(array_chunk($num1,3),true);
echo '<pre><p>array_pad将数组填充到指定位数:</p>'.var_export(array_pad($num1,15,666),true);
echo '<pre><p>array_unique删除重复元素:</p>'.var_export(array_unique($num1),true);
sort($num1);
echo '<pre><p>sort数组升序排序:</p>'.var_export($num1,true);
rsort($num1);
echo '<pre><p>rsort数组降序排序:</p>'.var_export($num1,true);
echo '</div>';

echo '<div class="div1"><h3>数组的其他操作:</h3>';
$num3 = range(1,100,10);
$fg = range('a','z',4);
shuffle($num3);
$num5 = array_rand($num3,5);
$num5 = array_merge($num3,$fg);
echo '<pre><p>range生成的字母数组:</p>'.var_export($fg,true);
echo '<pre><p>shuffle打乱数组:</p>'.var_export($num,true);
echo '<pre><p>array_rand随机抽取5个:</p>'.var_export($num5,true);
echo '<pre><p>array_merge合并打乱后的数组和生成的字母数组:</p>'.var_export($num5,true);

echo '</div></div>';


批改老师:韦小宝批改时间:2018-11-07 15:42:02
老师总结:写的很不错哦!下次记得加点自己的理解进去哦!继续加油吧!

发布手记

热门词条