PHP使用方括号语法来对平面数组中的列进行子集化

时间:2016-10-09 16:03:48

标签: php arrays

我有一个包含"平面数据的数组"有几列。为了在smarty中创建一个循环数组,我需要从一列中提取唯一值。所以我正在测试并探索方括号语法,请参阅my previous post

数组(它只是一个小提取,你可以是几个indcode重复):

  0 => Array 
    indcode => "A00"
    indlabel => "Code label text"
    description => "More explanations"
  1 => Array 
    indcode => "NA0"
    indlabel => "Un-classified A"
    description => "Un-classified A"
  2 => Array (3)
    indcode => "A01"
    indlabel => "Code Label text"
    description => "More explanations"
  3 => Array (3)
    indcode => "A02"
    indlabel => "Code label text"
    description => "More explanations"

我想得到什么(保持独特的价值观):

0 => "A00"
1 => "NA0"
2 => "A01"
3 => "A02"

到目前为止我得到了什么:

foreach ($result as $row) {
  (... other instructions ...)
  $indlist[$row['indcode']] = "CodeForLoop";
}
$indlist = array_keys($HGlist);

这很好地从一个新的平面数组列中提取唯一值,但我想知道是否有其他方法可以做到这一点,特别是我想知道是否在= "CodeForLoop"部分我可以以某种方式添加一个计数器,以便稍后在array_flip()上使用。此外,与其他方法相比,这是否快速?我想我会经常使用这种结构......

我来自R所以对我来说括号表示子集总是一件好事;)

2 个答案:

答案 0 :(得分:1)

您可以拥有array_column功能

的解决方案

关注此网址http://php.net/manual/en/function.array-column.php

$my_array  =  0 => Array 
        indcode => "A00"
        indlabel => "Code label text"
        description => "More explanations"
      1 => Array 
        indcode => "NA0"
        indlabel => "Un-classified A"
        description => "Un-classified A"
      2 => Array (3)
        indcode => "A01"
        indlabel => "Code Label text"
        description => "More explanations"
      3 => Array (3)
        indcode => "A02"
        indlabel => "Code label text"
        description => "More explanations"

    you want first column of an array so you need to call function with proper argument like below

    $indcodes = array_column($my_array, 'indcode');
    print_r($indcodes);

if you need unique array then use array_unique($indcodes) 

if your php version is old then use below custom function

<?php
if (! function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null) {
        $array = array();
        foreach ($input as $value) {
            if ( ! isset($value[$columnKey])) {
                trigger_error("Key \"$columnKey\" does not exist in array");
                return false;
            }
            if (is_null($indexKey)) {
                $array[] = $value[$columnKey];
            }
            else {
                if ( ! isset($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not exist in array");
                    return false;
                }
                if ( ! is_scalar($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not contain scalar value");
                    return false;
                }
                $array[$value[$indexKey]] = $value[$columnKey];
            }
        }
        return $array;
    }
}
?>
    as this explained at here : https://github.com/ramsey/array_column/blob/master/src/array_column.php

我希望这会有所帮助!

答案 1 :(得分:0)

尝试下面的代码段。 Quick-test-here

<?php

  $array = array(
    array(
        "indcode"     => "A00",
        "indlabel"    => "Code label text",
        "description" => "More explanations",
        ),
    array(
        "indcode"     => "NA0",
        "indlabel"    => "Un-classified A",
        "description" => "Un-classified A",
        ),
    array(
        "indcode"     => "A01",
        "indlabel"    => "Code label text",
        "description" => "More explanations",
        ),
    array(
        "indcode"     => "A02",
        "indlabel"    => "Code label text",
        "description" => "More explanations",
        ),
  );



    $result   = array();
    foreach($array as $k=>$v){
      $result[]  = $v["indcode"];
    }

    var_dump($result);
    //YIELDS::
    array (size=4)
      0 => string 'A00' (length=3)
      1 => string 'NA0' (length=3)
      2 => string 'A01' (length=3)
      3 => string 'A02' (length=3)