首頁  >  文章  >  後端開發  >  php 傳回數組中指定多列的方法

php 傳回數組中指定多列的方法

jacklove
jacklove原創
2018-06-15 17:34:452102瀏覽

php array_column 方法可以傳回數組中指定的一列,但不能傳回多列,本文將介紹array_column方法的使用,並用程式碼示範傳回數組中指定多列的方法。

1.array_column說明

array_column可以傳回數組中指定一列

array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )

參數說明:

#input
需要取出陣列列的多維數組。如果提供的是包含一組物件的數組,只有 public 屬性會被直接取出。為了也能取出 private 和 protected 屬性,類別必須實作 __get() 和 __isset() 魔術方法。

column_key
需要傳回值的列,它可以是索引數組的列索引,或是關聯數組的列的鍵,也可以是屬性名。也可以是NULL,此時會傳回整個陣列(配合index_key參數來重置陣列鍵的時候,非常管用)

index_key
作為傳回陣列的索引/鍵的列,它可以是該列的整數索引,或字串鍵值。
範例:
傳回數組中name列

<?php$arr = array(    array(&#39;id&#39;=>1001, &#39;name&#39;=>&#39;fdipzone&#39;, &#39;age&#39;=>18, &#39;profession&#39;=>&#39;programmer&#39;),    array(&#39;id&#39;=>1002, &#39;name&#39;=>&#39;terry&#39;, &#39;age&#39;=>19, &#39;profession&#39;=>&#39;designer&#39;),    array(&#39;id&#39;=>1003, &#39;name&#39;=>&#39;alex&#39;, &#39;age&#39;=>20, &#39;profession&#39;=>&#39;tester&#39;),
);$result = array_column($arr, &#39;name&#39;);
print_r($result);?>

輸出:

Array(
    [0] => fdipzone
    [1] => terry
    [2] => alex
)

2.返回數組中指定多列的方法

array_column方法可以返回數組中指定一列,但不能返回多列,因此寫了以下這個方法,支援返回數組中多列,參數調用與array_column相似。

<?php/**
 * 返回数组中指定多列
 *
 * @param  Array  $input       需要取出数组列的多维数组
 * @param  String $column_keys 要取出的列名,逗号分隔,如不传则返回所有列
 * @param  String $index_key   作为返回数组的索引的列
 * @return Array
 */function array_columns($input, $column_keys=null, $index_key=null){
    $result = array();    $keys =isset($column_keys)? explode(&#39;,&#39;, $column_keys) : array();    if($input){        foreach($input as $k=>$v){            // 指定返回列
            if($keys){                $tmp = array();                foreach($keys as $key){                    $tmp[$key] = $v[$key];
                }
            }else{                $tmp = $v;
            }            // 指定索引列
            if(isset($index_key)){                $result[$v[$index_key]] = $tmp;
            }else{                $result[] = $tmp;
            }
        }
    }    return $result;
}// 演示代码$arr = array(    array(&#39;id&#39;=>1001, &#39;name&#39;=>&#39;fdipzone&#39;, &#39;age&#39;=>18, &#39;profession&#39;=>&#39;programmer&#39;),    array(&#39;id&#39;=>1002, &#39;name&#39;=>&#39;terry&#39;, &#39;age&#39;=>19, &#39;profession&#39;=>&#39;designer&#39;),    array(&#39;id&#39;=>1003, &#39;name&#39;=>&#39;alex&#39;, &#39;age&#39;=>20, &#39;profession&#39;=>&#39;tester&#39;),
);echo &#39;指定返回列及索引列&#39;.PHP_EOL;$result = array_columns($arr, &#39;name,profession&#39;, &#39;id&#39;);
print_r($result);echo PHP_EOL.&#39;指定返回列,不指定索引列&#39;.PHP_EOL;$result = array_columns($arr, &#39;name,profession&#39;);
print_r($result);echo PHP_EOL.&#39;不指定返回列,指定索引列&#39;.PHP_EOL;$result = array_columns($arr, null, &#39;id&#39;);
print_r($result);echo PHP_EOL.&#39;不指定返回列,不指定索引列&#39;.PHP_EOL;$result = array_columns($arr);
print_r($result);?>

輸出:

指定返回列及索引列Array(
    [1001] => Array
        (
            [name] => fdipzone
            [profession] => programmer
        )
    [1002] => Array
        (
            [name] => terry
            [profession] => designer
        )
    [1003] => Array
        (
            [name] => alex
            [profession] => tester
        )
)
指定返回列,不指定索引列Array(
    [0] => Array
        (
            [name] => fdipzone
            [profession] => programmer
        )
    [1] => Array
        (
            [name] => terry
            [profession] => designer
        )
    [2] => Array
        (
            [name] => alex
            [profession] => tester
        )
)
不指定返回列,指定索引列Array(
    [1001] => Array
        (
            [id] => 1001
            [name] => fdipzone
            [age] => 18
            [profession] => programmer
        )
    [1002] => Array
        (
            [id] => 1002
            [name] => terry
            [age] => 19
            [profession] => designer
        )
    [1003] => Array
        (
            [id] => 1003
            [name] => alex
            [age] => 20
            [profession] => tester
        )
)
不指定返回列,不指定索引列Array(
    [0] => Array
        (
            [id] => 1001
            [name] => fdipzone
            [age] => 18
            [profession] => programmer
        )
    [1] => Array
        (
            [id] => 1002
            [name] => terry
            [age] => 19
            [profession] => designer
        )
    [2] => Array
        (
            [id] => 1003
            [name] => alex
            [age] => 20
            [profession] => tester
        )
)

本文解釋了php 傳回數組中指定多列的方法 ,更多相關內容請關注php中文網。

相關推薦:

JS取得存取裝置資訊的方法

#mysql secure-file-priv選項問題的解決方法

php 利用debug_backtrace方法追蹤程式碼呼叫

以上是php 傳回數組中指定多列的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn