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

前端 未结 14 1407
鱼传尺愫
鱼传尺愫 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 08:04

    Similar to fuentesjrs solution, but a bit more generic using array_walk() with a custom callback:

    // Define the callback
    function extract_named_sub_elements(&$item, $key, $name) {
      $item = $item[$name];
    }
    
    // Test data
    $original = array(
      array('page' => 'page1', 'name' => 'pagename1'),
      array('page' => 'page2', 'name' => 'pagename2'),
      array('page' => 'page3', 'name' => 'pagename3'),
    );
    
    // Use a copy, as array_walk() operates directly on the passed in array
    $copy = $original;
    
    // Substitute 'name' with whatever element you want to extract, e.g. 'page'
    array_walk($copy, 'extract_named_sub_elements', 'name');
    
    print_r($copy);
    
    0 讨论(0)
  • 2020-11-21 08:04

    Yes, there is a php built-in function called array_column which does what you are looking for.

    You would call it something like $name_keys = array_column($array, 'name'); to get the result that you are looking for.

    Please refer to the following entry in the PHP manual for more details:

    http://php.net/manual/en/function.array-column.php

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

    I don't think there is any need to have a built in function for this. There may be an array in your those array.

    $samples=array(
                array('page' => 'page1', 'name' => 'pagename1'),
                array('page' => 'page2', 'name' => 'pagename2'),
                array('page' => 'page3', 'name' => 'pagename3')
                );
    
    $output1=array();
    $output2=array();
    foreach($samples as $sample){
        array_push($output1,$sample['name']);
        $output2[]=array_splice($sample,1);
    
    }
    
    print_r($output1);
    print_r($output2);
    

    in $output1 is the output what you want if you want only to remove the 'page' indexing' part then $output2.

    if you need all the values from the that array and indexes numerically the array then you can use

    $array_1=array_values($samples); 
    

    but what i understand, you didn't want this.

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

    With array_reduce:

    $names = array_reduce($array, function ($carry, $item) {
        return array_merge($carry, [$item['name']]);
    }, []);
    
    0 讨论(0)
  • 2020-11-21 08:07

    Here's a functional way of doing it:

    $data = array(
                array('page' => 'page1', 'name' => 'pagename1'),
                array('page' => 'page2', 'name' => 'pagename2'),
                array('page' => 'page3', 'name' => 'pagename3'));
    
    $result = array_map(create_function('$arr', 'return $arr["name"];'), $data);
    print_r($result);
    
    0 讨论(0)
  • 2020-11-21 08:11

    Just to extend on some of the answers here, as of PHP 5.5, array_column is what you want.

    It actually has a few possible uses.

    Using the sample array below, here are the different ways to use array_column.

    $a = array(
        array('id' => '1', 'name' => 'Joe'),
        array('id' => '2', 'name' => 'Jane')
    );
    

    Retrieving a single column as the array

    $b = array_column($a, 'name');
    

    Would give you. Notice the auto keys starting from 0, as per a normal array.

    $b[0] = 'Joe';
    $b[1] = 'Jane';
    

    Retrieving the full array with a column as the index.

    $c = array_column($a, NULL, 'id');
    

    Would result in the following.

    $c[1] = array('id' => '1', 'name' => 'Joe');
    $c[2] = array('id' => '2', 'name' => 'Jane');
    

    Notice how the column I selected as the third parameter becomes the key for each item and I get the full array by setting the second parameter to null.

    Of course, the final usage is to set both the 2nd and 3rd params.

    $d = array_column($a, 'name', 'id');
    

    Would give you the following.

    $d[1] = 'Joe';
    $d[2] = 'Jane';
    

    I personally use the full 3 params for creating select option lists. If I have a table with my options, I query the table and get the result and pass it into this to get a list with the key as the value and the label. This is a brilliant way for building info sets that need to intersect by the index as well.

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