'page1','name'=>'pagename1')array('page'=>'page2','name'=>'pagename2 ')array('page'=>'page3','name'=>'pagename3')) Is there a built-in function that returns a new array containing only the "name" key value? So I"> Is there a way to retrieve specific "columns" from a PHP array?-PHP Chinese Network Q&A
Is there a way to retrieve specific "columns" from a PHP array?
P粉423694341
P粉423694341 2023-10-21 00:01:53
0
2
629

I have an array of arrays with the following structure:

array(array('page' => 'page1', 'name' => 'pagename1') array('page' => 'page2', 'name' => 'pagename2') array('page' => 'page3', 'name' => 'pagename3'))

Is there a built-in function that returns a new array containing only the "name" key value? So I would get:

array('pagename1', 'pagename2', 'pagename3')


P粉423694341
P粉423694341

reply all (2)
P粉514001887

Starting inPHP 5.5you can usearray_column():

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

View actual operation

    P粉042455250

    Why does it have to be a built-in function? No, no, write it yourself.

    This is a nice and simple approach compared to others in this thread.

    $namearray = array(); foreach ($array as $item) { $namearray[] = $item['name']; }

    In some cases, if the key is not named, you can do this

    $namearray = array(); foreach ($array as $key => $value) { $namearray [] = $value; }
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!