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',
)

 

 

案例:

$pageGoods =  DB::table($orderGoodsTable." as a")
            ->whereIn('order_id', $pageOrderIds)
            ->join($goodsTable." as b", "b.goods_id", "=", "a.goods_id")
            ->select("a.order_id", "b.goods_id", "b.original_img", "b.goods_name")
            ->get()
            ->toArray();
        dd(array_column($pageGoods, null, 'order_id'));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM