Nested foreach()

前端 未结 4 2015
面向向阳花
面向向阳花 2021-01-08 00:56

I have the following array:

Array ( 
  [1] => Array ( 
    [spubid] => A00319 
    [sentered_by] => pubs_batchadd.php
    [sarticle] => Lateral m         


        
4条回答
  •  广开言路
    2021-01-08 01:14

    Just for fun. If you really want to avoid loops, try this:

    // Pre PHP 5.3:
    
    function cb2($e)
    {
        return $e['slast'] . ', ' . $e['sfirst'];
    }
    
    function cb1($e)
    {
        $authors = array_map('cb2', $e['authors']);
        echo implode('; ', $authors) . ":
    \n" . $e['sarticle'] . "
    \n"; } array_walk($data, 'cb1'); // PHP 5.3 (untested): array_walk($data, function($e) { $authors = array_map(function($e) { return $e['slast'] . ', ' . $e['sfirst']; }, $e['authors']); echo implode('; ', $authors) . ":
    \n" . $e['sarticle'] . "
    \n"; });

提交回复
热议问题