php數組操作執行個體三_PHP教程

來源:互聯網
上載者:User
本文章主要針對數組交集、判斷鍵是否存在於數組中、合并兩個數組、資料排序、增加數組,刪除數組元素、隨機抽取數組元素等執行個體。

本文章主要針對數組交集、判斷鍵是否存在於數組中、合并兩個數組、資料排序、增加數組,刪除數組元素、隨機抽取數組元素等執行個體。
*/
$array1=array("a"=>"green","red","blue");
$array2=array("b"=>"green","yellow","red");
$result=array_intersect($array1,$array2);//計算交集並賦值
print_r($result);

//

$search_array=array('first'=>1,'second'=>4); //定義數組
if(array_key_exists('first',$search_array)) //判斷鍵是否存在於數組中
{
echo "the 'first' element is in the array"; //輸出相應資訊
}

//

$ar1=array("color"=>array("favorite"=>"red"),5);
$ar2=array(10,"color"=>array("favorite"=>"green","blue"));
$result=array_merge_recursive($ar1,$ar2); //合并兩個數組返回一個結果數組
print_r($result); //輸出結果

//

$array1=array("color"=>"red",2,4);
$array2=array("a","b","color"=>"green","shape"=>"trapezoid",4);
$result=array_merge($array1,$array2); //合并兩個數組
print_r($result); //輸出結果內容

//

$ar1=array("10",100,100,"a"); //定義數組1
$ar2=array(1,3,"2",1); //定義數組2
array_multisort($ar1,$ar2); //排序2個數組
var_dump($ar1); //列印排序後的結果
var_dump($ar2); //列印排序後的結果

//
$stack=array("orange","banana","apple","rasp教程berry"); //定義一個數組
$fruit=array_pop($stack); //最後一個元素出棧
print_r($stack); //顯示結果

//

$stack=array("orange", "banana"); //定義原始數組
array_push($stack,"apple","raspberry"); //執行壓棧操作
print_r($stack); //顯示結果,將返回四個值

//

srand((float)microtime()*10000000); //為隨機數發生器播種
$input=array("neo","morpheus","trinity","cypher","tank"); //定義原始數組
$rand_keys=array_rand($input,2); //隨機挑選數組元素
print $input[$rand_keys[0]]."n"; //分別顯示隨機挑選的數組元素
print $input[$rand_keys[1]]."n";


http://www.bkjia.com/PHPjc/445396.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445396.htmlTechArticle本文章主要針對數組交集、判斷鍵是否存在於數組中、合并兩個數組、資料排序、增加數組,刪除數組元素、隨機抽取數組元素等執行個體。 本...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.