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

前端 未结 14 1374
鱼传尺愫
鱼传尺愫 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: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.

提交回复
热议问题