zoukankan      html  css  js  c++  java
  • PHP 将二维数组中某列值作为数组的键名 -- 超实用

    有时候,想通过数组的中某字段值, 然后再在二维数组中获取存在该字段值的数组;

    一般能想到的就是foreach 遍历比较一下跟该字段值一样,就获取到想要的数组,如下:

     // 测试二维数组                                         
    $arr = array (                                    
      0 =>  array (                                  
        'value' => 1,                          
        'name' => 'test_0',                    
      ),                                       
      1 => array (                                  
        'value' => 2,                          
        'name' => 'test_1',                    
      ),                                       
      2 => array (                                  
        'value' => 3,                          
        'name' => 'test_2',                    
      ),                                       
      3 => array (                                  
        'value' => 4,                          
        'name' => 'test_3',                    
      ),                                       
      4 => array (                                  
        'value' => 5,                          
        'name' => 'test_4',                    
      ),                                       
    );    

    1. foreach 遍历大法

    比如:根据value = 4, 获取测试二维数组中value = 4的数组

    $value = 4;
    foreach ($arr as $key => $item) {
       if ($item['value'] == $value) {
           $res = $item;
       }
    }
    
    // 结果
    array (
     'value' => 4,
      'name' => 'test_3',
    )

    思考了下,要是不foreach ,直接通过二维数组的下标获取到想要的数组元素那就最好了!那要怎么样将二维数组中的元素的某个字段值变为二维数组的下标键名呢?

    so 了一下 PHP官方文档:https://www.php.net/manual/zh/function.array-column.php ,可以看到 有一个 array_column()

    array_column — 返回数组中指定的一列
    
    说明
    array_column ( array $input , mixed $column_key [, mixed $index_key = null ] ) : array
    array_column() 返回input数组中键值为column_key的列, 如果指定了可选参数index_key,那么input数组中的这一列的值将作为返回数组中对应值的键。
    
    参数
    input
    需要取出数组列的多维数组。 如果提供的是包含一组对象的数组,只有 public 属性会被直接取出。 为了也能取出 privateprotected 属性,类必须实现 __get() 和 __isset() 魔术方法。
    
    column_key
    需要返回值的列,它可以是索引数组的列索引,或者是关联数组的列的键,也可以是属性名。 也可以是NULL,此时将返回整个数组(配合index_key参数来重置数组键的时候,非常管用)
    
    index_key
    作为返回数组的索引/键的列,它可以是该列的整数索引,或者字符串键值。

    就是红色字体部分说到了,实践见证奇迹!!!

    2. array_column() 大大法

    $tempArr = array_column($arr, null, 'value');

    结果:

     // 以value字段为键名                                      
    array (                                  
      1 => array (                                
        'value' => 1,                        
        'name' => 'test_0',                  
      ),
     2 => array (                                
        'value' => 2,                        
        'name' => 'test_1',                  
      ),                                     
      3 => array (                                
        'value' => 3,                        
        'name' => 'test_2',                  
      ),                                     
      4 => array (                                
        'value' => 4,                        
        'name' => 'test_3',                  
      ),                                     
      5 => array (                                
        'value' => 5,                        
        'name' => 'test_4',                  
      ),                                     
    )  

     // 以name字段为键名   

    $tempArr = array_column($arr, null, 'name');

    array (
    'test_0' => array (
      'value' => 1,
      'name' => 'test_0',
    ),
    'test_1' => array (
      'value' => 2,
      'name' => 'test_1',
    ),
    'test_2' => array (
      'value' => 3,
      'name' => 'test_2',
    ),
    'test_3' => array (
      'value' => 4,
      'name' => 'test_3',
    ),
    'test_4' => array (
      'value' => 5,
      'name' => 'test_4',
    ),
    )

      

    
    

     然后,这样就可以直接通过 某个字段值 获取到二维数组中 某字段值 的数组元素了!超实用!!!

    // 实用数组下标方式获取想要数组元素
    $res = $tempArr[$value];
    // 结果 array (  'value' => 4, 'name' => 'test_3', )
  • 相关阅读:
    数据结构——快速排序
    设计模式——代理模式(静态代理和JDK、CGLib动态代理)
    Java多线程系列——信号量:Semaphore
    Java多线程系列——线程阻塞工具类LockSupport
    Java多线程系列——过期的suspend()挂起、resume()继续执行线程
    Java多线程系列——深入重入锁ReentrantLock
    JVM——深入分析对象的内存布局
    【Git】The authenticity of host '192.168.1.1 (192.168.1.1)' can't be established.
    【linux】CentOS: Sudo: unable to initialize policy plugin
    【Git】.git/FETCH_HEAD: Permission denied 的解决方法
  • 原文地址:https://www.cnblogs.com/smallyi/p/11089591.html
Copyright © 2011-2022 走看看