久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何通過僅從與第一個數組具有相同鍵的第二個

How to merge two arrays by taking over only values from the second array that has the same keys as the first one?(如何通過僅從與第一個數組具有相同鍵的第二個數組中接管值來合并兩個數組?) - IT屋-程序員軟件開
本文介紹了如何通過僅從與第一個數組具有相同鍵的第二個數組中接管值來合并兩個數組?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想合并兩個數組:

$filtered = array(1 => 'a', 3 => 'c');
$changed = array(2 => 'b*', 3 => 'c*');

而合并應包括 $filtered 的所有元素和 $changed 的所有元素,這些元素在 $filtered 中有相應的鍵:

Whereas the merge should include all elements of $filtered and all those elements of $changed that have a corresponding key in $filtered:

$merged = array(1 => 'a', 3 => 'c*');

array_merge($filtered, $changed) 也會將 $changed 的附加鍵添加到 $filtered 中.所以不太合適.

array_merge($filtered, $changed) would add the additional keys of $changed into $filtered as well. So it does not really fit.

我知道我可以使用 $keys = array_intersect_key($filtered, $changed) 來獲取兩個數組中存在的鍵,這已經完成了一半的工作.

I know that I can use $keys = array_intersect_key($filtered, $changed) to get the keys that exist in both arrays which is already half of the work.

但是我想知道是否有任何(本機)函數可以將 $changed 數組減少為具有 array_intersect_key 指定的 $keys 的數組?我知道我可以使用帶有回調函數的 array_filter 并檢查其中的 $keys,但可能還有其他一些純本機函數來僅從數組中提取那些元素可以指定鍵嗎?

However I'm wondering if there is any (native) function that can reduce the $changed array into an array with the $keys specified by array_intersect_key? I know I can use array_filter with a callback function and check against $keys therein, but there is probably some other purely native function to extract only those elements from an array of which the keys can be specified?

我這么問是因為原生函數通常比帶有回調的 array_filter 快得多.

I'm asking because the native functions are often much faster than array_filter with a callback.

推薦答案

如果我正確理解你的邏輯,應該這樣做:

This should do it, if I'm understanding your logic correctly:

array_intersect_key($changed, $filtered) + $filtered

<小時>

實施:

$filtered = array(1 => 'a', 3 => 'c');
$changed = array(2 => 'b*', 3 => 'c*');
$expected = array(1 => 'a', 3 => 'c*');    
$actual = array_key_merge_deceze($filtered, $changed);

var_dump($expected, $actual);

function array_key_merge_deceze($filtered, $changed) {
    $merged = array_intersect_key($changed, $filtered) + $filtered;
    ksort($merged);
    return $merged;
}

輸出:

Expected:
array(2) {
  [1]=>
  string(1) "a"
  [3]=>
  string(2) "c*"
}

Actual:
array(2) {
  [1]=>
  string(1) "a"
  [3]=>
  string(2) "c*"
}

這篇關于如何通過僅從與第一個數組具有相同鍵的第二個數組中接管值來合并兩個數組?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 国产精品欧美一区喷水 | 国产成人精品一区 | 久久99精品久久久久久青青日本 | 国产精品久久一区二区三区 | 91久久精品一区二区二区 | 成人精品一区二区三区中文字幕 | 另类 综合 日韩 欧美 亚洲 | 欧美成人自拍视频 | 欧美精品三区 | 成人精品一区亚洲午夜久久久 | 午夜国产一级 | 中文字幕精品一区 | 天天插天天舔 | 欧美精品1区 | 国产一级视屏 | 91精品久久久久久久久 | 亚洲精品免费观看 | 国产精品久久久久免费 | 国产成人精品一区二区三区在线观看 | 羞羞网站免费观看 | 久热精品在线观看视频 | 国产精品久久在线观看 | av免费网站在线观看 | 91成人在线 | 亚洲国产成人在线 | 欧美自拍另类 | 91伊人网 | 成人激情视频在线观看 | 四虎影视在线 | 91av在线视频观看 | 中文字幕在线观看一区二区 | 欧美一级视频 | 91文字幕巨乱亚洲香蕉 | 一区二区三区四区免费观看 | 国产精品免费一区二区三区四区 | 国产精品国产自产拍高清 | 精品动漫一区 | 精品成人av | 欧美激情国产日韩精品一区18 | 成人av一区| 欧美在线一二三 |