多列取值array_columns(array $input, ?array $column_keys, $index_key = null)

/**
 * It plucks parts of columns for each item in array, possibly indexed by a custom column.
 * 
 * @param array $input
 * @param ?array $column_keys default null
 * @param string $index_key default null
 * @return array
 */
function array_columns(array &$input, ?array $column_keys = null, $index_key = null) 
{
  if (empty($input)) {
    return [];
  }
  if (empty($column_keys) && !isset($index_key)) {
    return $input;
  }
  if (empty($column_keys)) {
    return array_combine(
      array_column($input, $index_key),
      array_values($input)
    );
  }

  $cols = array_flip($column_keys);
  $item_type = is_array($input[0]) ? 'array' : (
    is_object($input[0]) ? 'object' : null
  );
  $vals = array_map(function($item) use($cols, $item_type){
    $new = null;
    // $item_type = gettype($item);
    if ($item_type == 'array') {
      $new = array_filter($item, function($key) use($cols){
        return isset($cols[$key]);
      }, ARRAY_FILTER_USE_KEY);
    }
    if ($item_type == 'object') {
      $new = new \stdClass();
      foreach ($item as $attr => $value) {
        if (isset($cols[$attr])) {
          $new->$attr = $value;
        }
      }
    }
    return $new;
  }, $input);

  if (!isset($index_key)) {
    return $vals;
  }
  
  $keys = array_column($input, $index_key);
  return array_combine($keys, $vals);
}
    $arr = array(    
      array('id'=>1001, 'name'=>'carter', 'age'=>40, 'profession'=>'dvlp'),    
      array('id'=>1002, 'name'=>'hedy', 'age'=>23, 'profession'=>'qa'),    
      array('id'=>1003, 'name'=>'nora', 'age'=>30, 'profession'=>'qa'),
    );
    $arr = array_map(function($item){return (object)$item;}, $arr);

    var_dump(array_columns($arr, ['name', 'profession']));
    var_dump(array_columns($arr, ['name', 'profession'], 'age'));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?php // 连接数据库 $conn = new mysqli("localhost", "root", "123456", "wyya"); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询五个歌单的表 $tables = array(); $result = mysqli_query($conn, "SHOW TABLES LIKE '%_list'"); if ($result->num_rows > 0) { while ($row = mysqli_fetch_array($result)) { $tables[] = $row[0]; } } // 获的歌单表 $tableName = isset($_GET["table"]) ? $_GET["table"] : ""; $data = array(); if (!empty($tableName)) { $result = mysqli_query($conn, "SELECT * FROM $tableName"); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $data[] = $row; } } } ?> <!DOCTYPE html> <html> <head> <title>网易云音乐歌单</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #f2f2f2; } th { background-color: #4CAF50; color: white; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; z-index: 1; } .dropdown:hover .dropdown-content { display: block; } </style> </head> <body> <h2>网易云音乐歌单</h2> <div class="dropdown"> <a href="#">分类</a> <div class="dropdown-content"> <?php foreach ($tables as $table) { ?> <a href="?table=<?php echo $table; ?>"><?php echo str_replace("_list", "", $table); ?></a> <?php } ?> </div> </div> <?php if (!empty($tableName)) { ?> <table> <tbody> <tr> <?php $columns = mysqli_query($conn, "SHOW COLUMNS FROM $tableName"); if ($columns->num_rows > 0) { while ($column = mysqli_fetch_array($columns)) { ?> <th><?php echo $column["Field"]; ?></th> <?php } } ?> </tr> <?php foreach ($data as $row) { ?> <tr> <?php foreach ($row as $value) { ?> <td><?php echo $value; ?></td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php } ?> </body> </html> <?php // 关闭连接 $conn->close(); ?>修改代码,将one修改成华语,two修改成欧美,three修改成日语,four修改成韩语,five修改成粤语
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值