php 擷取數組中單列值的方法執行個體詳解

來源:互聯網
上載者:User
這篇文章主要介紹了PHP擷取數組中單列值的方法,結合執行個體形式分析了PHP5.5中array_column()函數的提示,需要的朋友可以參考下

本文執行個體講述了PHP擷取數組中單列值的方法。分享給大家供大家參考,具體如下:

PHP中擷取數組中單列的值如下:

利用PHP中的數組函數 array_column():返回數組中某個單列的值。(PHP 5.5+適用)

文法:

array_column(array,column_key,index_key);

參數:

array : 必需,規定必須為多維陣列;
column_key : 必需,需要返回的值的鍵名;可以是索引數組的列的整數索引,或者是關聯陣列的列的字串索引值。該參數也可以是 NULL,此時將返回整個數組(配合 index_key 參數來重設數組鍵的時候,非常有用)。
index_key : 可選。用作返回數組的索引/鍵的列。

執行個體:

從屬記錄集中取出 last_name 列,用相應的 "id" 列作為索引值:

<?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', 'id');print_r($last_names);?>

輸出:

Array( [5698] => Gates [4767] => Jobs [3809] => Zuckerberg)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.