Is there a function to extract a 'column' from an array in PHP?

前端 未结 14 1404
鱼传尺愫
鱼传尺愫 2020-11-21 07:33

I have an array of arrays, with the following structure :

array(array(\'page\' => \'page1\', \'name\' => \'pagename1\')
      array(\'page\' => \'pa         


        
相关标签:
14条回答
  • 2020-11-21 07:52
    if (!function_exists('array_column')) {
        function array_column($array,$column) {
        $col = array();
        foreach ($array as $k => $v) {
            $col[]=$v[$column];
        }
        return $col;
        }
    }
    

    This should work for php versions < 5.5 and degrade in case the function exist

    0 讨论(0)
  • 2020-11-21 07:53

    Well there is. At least for PHP > 5.5.0 and it is called array_column

    The PHP function takes an optional $index_keyparameter that - as per the PHP website - states:

    $index_key

    The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name

    In the answers here, i see a stripped version without the optional parameter. I needed it, so, here is the complete function:

    if (!function_exists('array_column')) {
        function array_column($array, $column, $index_key = null) {
            $toret = array();
            foreach ($array as $key => $value) {
                if ($index_key === null){
                    $toret[] = $value[$column];
                }else{
                    $toret[$value[$index_key]] = $value[$column];
                }
            }
            return $toret;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:55

    There is a built-in function actually, it's called array_column(...).

    Here is all you need to know about it : https://www.php.net/manual/fr/function.array-column.php

    0 讨论(0)
  • 2020-11-21 07:58

    You can extend the ArrayIterator class and override the method mixed current(void).

    class Foo extends ArrayIterator {
      protected $index;
      public function __construct($array, $index) {
        parent::__construct($array);
        $this->index = $index;
      }
    
      public function current() {
        $c = parent::current();
        return isset($c[$this->index]) ? $c[$this->index] : null;
      }
    }
    
    $a = array(
      array('page' => 'page1', 'name' => 'pagename1'),
      array('name' => '---'),
      array('page' => 'page2', 'name' => 'pagename2'),
      array('page' => 'page3', 'name' => 'pagename3')
    );
    
    $f = new Foo($a, 'page');
    foreach($f as $e) {
      echo $e, "\n";
    }
    

    prints

    page1
    
    page2
    page3
    
    0 讨论(0)
  • 2020-11-21 08:00

    As of PHP 5.5 you can use array_column():

    <?php
    $samples=array(
                array('page' => 'page1', 'name' => 'pagename1'),
                array('page' => 'page2', 'name' => 'pagename2'),
                array('page' => 'page3', 'name' => 'pagename3')
                );
    $names = array_column($samples, 'name');
    print_r($names);
    

    See it in action

    0 讨论(0)
  • 2020-11-21 08:02

    I wanted to post here, even if this is an old question, because it is still very relevant and many developers do not use PHP >= 5.5

    Let's say you have an array like this:

    Array
    (
        [files] => Array
            (
                [name] => Array
                    (
                        [0] => file 1
                        [1] => file 2
                        [2] => file 3
                    )
    
                [size] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                    )
    
                [error] => Array
                    (
                        [0] => abc
                        [1] => def
                        [2] => ghi
                    )
    
            )
    
    )
    

    and the output you want is something like this:

    Array
    (
        [0] => Array
            (
                [0] => file 1
                [1] => 1
                [2] => abc
            )
    
        [1] => Array
            (
                [0] => file 2
                [1] => 2
                [2] => def
            )
    
        [2] => Array
            (
                [0] => file 3
                [1] => 3
                [2] => ghi
            )
    
    )
    

    You can simply use the array_map() method without a function name passed as the first parameter, like so:

    array_map(null, $a['files']['name'], $a['files']['size'], $a['files']['error']);
    

    Unfortunately you cannot map the keys if passing more than one array.

    0 讨论(0)
提交回复
热议问题