从一个对象数组的列中删除在一个扁平数组中找到的元素。

huangapple go评论72阅读模式
英文:

Remove elements from a flat array if found in a column of an array of objects

问题

我需要过滤一个一维值数组,根据对象数组中的特定属性/列。以下是您提供的代码示例:

  1. $array_one = array_keys(wc_get_account_menu_items());
  2. $array_two = $wpdb->get_results("SELECT pa_menu_endpoint FROM $phantom_menu WHERE pa_menu_from='pa_custom_tab'");

这两个数组的示例数据如下:

  1. $array_one = [
  2. 'dashboard',
  3. 'orders',
  4. 'downloads',
  5. 'edit-address',
  6. 'woo-wallet',
  7. 'edit-account',
  8. 'customer-logout',
  9. 'test',
  10. 'testtest'
  11. ];
  12. $array_two = [
  13. (object) ['pa_menu_endpoint' => 'test'],
  14. (object) ['pa_menu_endpoint' => 'testtest']
  15. ];

您想要从$array_one数组中排除"pa_menu_endpoint"值,期望的过滤结果如下:

  1. [
  2. 'dashboard',
  3. 'orders',
  4. 'downloads',
  5. 'edit-address',
  6. 'woo-wallet',
  7. 'edit-account',
  8. 'customer-logout'
  9. ]

请注意,这是如何使用PHP过滤$array_one数组以排除与$array_two中的"pa_menu_endpoint"值匹配的元素。

英文:

I have a one-dimensional array of values in one array and I need to filter it by a particular property/column in an array of objects.

My input arrays are populated by the following code:

  1. $array_one = array_keys(wc_get_account_menu_items());
  2. $array_tow = $wpdb->get_results("SELECT pa_menu_endpoint FROM $phantom_menu WHERE pa_menu_from='pa_custom_tab'");

Example data for these arrays:

  1. $array_one = [
  2. 'dashboard',
  3. 'orders',
  4. 'downloads',
  5. 'edit-address',
  6. 'woo-wallet',
  7. 'edit-account',
  8. 'customer-logout',
  9. 'test',
  10. 'testtest'
  11. ];
  12. $array_tow = [
  13. (object) ['pa_menu_endpoint' => 'test'],
  14. (object) ['pa_menu_endpoint' => 'testtest']
  15. ];

How can I exclude "pa_menu_endpoint" values from the $array_one array.

Desired filtered result:

  1. [
  2. 'dashboard',
  3. 'orders',
  4. 'downloads',
  5. 'edit-address',
  6. 'woo-wallet',
  7. 'edit-account',
  8. 'customer-logout'
  9. ]

答案1

得分: 0

你可以使用array_udiff()和空合并运算符直接通过比较回调来过滤二维数组$array_one,当$a$b不是对象时,可以回退到$array_tow的值。

  1. var_export(
  2. array_udiff(
  3. $array_tow,
  4. $array_one,
  5. fn($a, $b) => ($a->pa_menu_endpoint ?? $a) <=> ($b->pa_menu_endpoint ?? $b)
  6. )
  7. );

也许更容易理解的方法是预先展平过滤数组$array_one,这需要两个函数调用,但无需自定义回调。

  1. var_export(
  2. array_diff(
  3. $array_tow,
  4. array_column($array_one, 'pa_menu_endpoint')
  5. )
  6. );
英文:

You can filter the flat $array_tow array by the two dimensional array_one array directly using array_udiff() and the null coalescing operator in the comparing callback to fallback to the $array_tow value when $a or $b is not an object.

Code: (Demo)

  1. var_export(
  2. array_udiff(
  3. $array_tow,
  4. $array_one,
  5. fn($a, $b) =&gt; ($a-&gt;pa_menu_endpoint ?? $a) &lt;=&gt; ($b-&gt;pa_menu_endpoint ?? $b)
  6. )
  7. );

Perhaps simpler to understand will be to pre-flatten the filtering array ($array_one) -- this is two function calls, but no custom callbacks.

Code: (Demo)

  1. var_export(
  2. array_diff(
  3. $array_tow,
  4. array_column($array_one, &#39;pa_menu_endpoint&#39;)
  5. )
  6. );

huangapple
  • 本文由 发表于 2023年3月31日 04:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定