/**
* 取多维数据中某字段的值
* @param array $array 数据源数组
* @param string|array $field 要获取的字段
* @return array 结果数组
*/
function array_muliti_field($array, $field)
{
$resp = [];
foreach($array as $k => $v) {
if(is_array($field)) {
foreach($field as $f) {
if(isset($v[$f]) && $v[$f] !== null) {
$resp[$f][$v[$f]] = $v[$f];
}
}
} elseif(isset($v[$field]) && $v[$field] !== null){
$resp[] = $v[$field];
}
}
return $resp;
}/**
* 将多维数组中的某一个元素作为键名,重新生成数组
*
* $array = [
* 0 => ['id'=>10, 'title'=>'t10'],
* 1 => ['id'=>11, 'title'=>'t11']
* ];
*
* $res = array_set_key($array, 'id');
*
* 结果如下:
* $res = [
* 10 => ['id'=>10, 'title'=>'t10'],
* 11 => ['id'=>11, 'title'=>'t11']
* ];
*
* @param array $array 要处理的数组
* @param string $key 作为 key 的字段名称
* @param string $valuekey [description]
* @return array 新生成的数组
*/
function array_set_key($array, $key = '', $valuekey = '')
{
$res = [];
while (list($k, $v) = each($array)) {
if ($key == '') {
$res[] = (($valuekey != '') ? $v[$valuekey] : $v);
} else {
$res[$v[$key]] = (($valuekey != '') ? $v[$valuekey] : $v);
}
}
reset($array); return $res;
}/**
* 将多维数组中的某两个元素作为键名组成二维数组
* $array = [
* 0 => ['id'=>10,'title'=>'t10'],
* 1 => ['id'=>11,'title'=>'t11'],
* 2 => ['id'=>11,'title'=>'t12']
* ];
*
* $res = array_set_keys($array, 'id', 'title');
*
* 结果如下:
* $res = [
* 10 => [
* 't10' => ['id'=>10,'title'=>'t10']
* ],
*
* 11 => [
* 't11' => ['id'=>11,'title'=>'t11'],
* 't12' => ['id'=>11,'title'=>'t12']
* ]
* ];
* @param [type] $array [description]
* @param [type] $key1 [description]
* @param string $key2 [description]
* @param string $valuekey [description]
* @return [type] [description]
*/
function array_set_keys($array, $key1, $key2 = '', $valuekey = '')
{
$res = [];
while (list($k, $v) = each($array)) {
$k1 = $v[$key1];
$vl = ($valuekey != '') ? $v[$valuekey] : $v;
if ($key2 == '') {
$res[$k1][] = $vl;
} else {
$res[$k1][$v[$key2]] = $vl;
}
}
reset($array); return $res;
}

PHP 数组辅助函数的更多相关文章

  1. 019.CI4框架CodeIgniter辅助函数类之:Array数组查询

    01. 数组辅助函数,可以方便的查看数组内部的成员,用法如下图所示: <?php namespace App\Controllers; class Hello extends BaseContr ...

  2. Hibernate4读取Clob数据

    package cn.framework.dao.impl; import java.io.BufferedReader; import java.io.IOException; import jav ...

  3. CI框架浅析(全篇)

        业余花了点时间看看CodeIgniter框架(简称CI),CI目前的稳定版本是 3.X,4.0版本已经出来了,但还在测试中,所以我分析的还是 3.x 版本. CI是一个很轻便的框架,整个下载包 ...

  4. CodeIgniter框架解析

    转载于:https://www.cnblogs.com/xiaoxiaoqingyi/p/6901654.html 转载仅为以后自己学习. 业余花了点时间看看CodeIgniter框架(简称CI),C ...

  5. CI框架浅析

    CI框架浅析(全篇)     业余花了点时间看看CodeIgniter框架(简称CI),CI目前的稳定版本是 3.X,4.0版本已经出来了,但还在测试中,所以我分析的还是 3.x 版本. CI是一个很 ...

  6. 学习javaScript必知必会(3)~数组(数组创建,for...in遍历,辅助函数,高级函数filter、map、reduce)

    一.数组: 1.js是弱语言,js中的数组定义时:不用指定数据类型.不用功指定数组长度:数组可以存储任何数据类型的数据 2.数组定义的[ ] 的实质: [] = new Array(); {} = n ...

  7. TSPL学习笔记(4):数组相关练习

    最近研究函数式编程,都是haskell和scheme交互着看的,所以笔记中两种语言的内容都有,练习一般也都用两种语言分别实现. 本篇练习一些数组有关的问题,之所以与数组相关是因为在命令式编程中以下问题 ...

  8. ***CI查询辅助函数:insert_id()、affected_rows()

    查询辅助函数 $this->db->insert_id() 这个ID号是执行数据插入时的ID. $this->db->affected_rows() Displays the ...

  9. 数组链表下标指针map list

    1.时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间 ...

随机推荐

  1. jQuery_完成表单注册检验

    在校验表单的时候会很麻烦,但是jq可以用很简单的方法来检验,即使用validate. 原表单: 代码 <!DOCTYPE html> <html> <head> & ...

  2. kotlin 简单处理 回调参数 加?

    Kotlin Parameter specified as non-null is null 2017年10月18日 17:21:49 amiko_ 阅读数:9017    版权声明:本文为博主原创文 ...

  3. What exactly is the parameter e (event) and why pass it to JavaScript functions?

    What exactly is the parameter e (event) and why pass it to JavaScript functions? 问题 Well, when I lea ...

  4. 在RHEL6_Oracle_Linux_6上生成正确的udev_rule_规则文件

    1. #首先确认是 Linux 6.0以上版本 [root@vrh6 dev]# cat /etc/issue          Oracle Linux Server release 6.2Kern ...

  5. shell编程常用命令

    Linux中常用的命令 #nl  filename   使用nl命令打印文件内容并显示行号 #sed   '/nw/,$d'   filename     使用sed命令删除匹配nw至最后一行的内容 ...

  6. 描述一下 Intent 和 IntentFilter?

    Android 中通过 Intent 对象来表示一条消息,一个 Intent 对象不仅包含有这个消息的目的地,还可以包含消息的内容,这好比一封 Email,其中不仅应该包含收件地址,还可以包含具体的内 ...

  7. C#三种常用的读取XML文件的方法

    下面我将介绍三种常用的读取XML文件的方法.分别是 1: 使用 XmlDocument 2: 使用 XmlTextReader 3: 使用 Linq to Xml 这里我先创建一个XML文件,名为Bo ...

  8. Custom Configuration 的两种方法:1.Configuration Sections

    第一种Configuration Sections 1.App.config 2.CustomConfigurationManager.cs 3.TestProgram.cs. App.config ...

  9. 十三:jinja2过滤器之default过滤器和or过滤器

    在模板里面有时候需要对传过来的数据进行一些处理,jinja2有一些内置的过滤器可以进行处理.类似于python内置函数,通过 “|” 进行使用,详见jinja2官方文档 使用方法:{{ 变量名|过滤器 ...

  10. Linux(Debian)发行版中文输入法

    Linux发行版下有两大输入法框架:ibus 和fcitx,其中fcitx 的体验要比ibus 好,因此选择 fcitx 框架,并安装中文输入法. 中文输入法中你可以选择fcitx-pinyin or ...