Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP/Wordpress中实现嵌套数组的过滤器_Php_Arrays_Wordpress - Fatal编程技术网

在PHP/Wordpress中实现嵌套数组的过滤器

在PHP/Wordpress中实现嵌套数组的过滤器,php,arrays,wordpress,Php,Arrays,Wordpress,我正在将一些代码从前端(JavaScript)移动到服务器端(PHP),在服务器端它将被过滤并通过API调用发送出去,而我似乎无法让过滤器在后端正常工作。代码获取一个对象数组,并将其筛选为某个嵌套字段(也是一个对象数组)包含某些值的对象。API的基本形状: { "id": 1217, "name": "Best product ever", "tags": [ { "id": 125, "name": "Important Value",

我正在将一些代码从前端(JavaScript)移动到服务器端(PHP),在服务器端它将被过滤并通过API调用发送出去,而我似乎无法让过滤器在后端正常工作。代码获取一个对象数组,并将其筛选为某个嵌套字段(也是一个对象数组)包含某些值的对象。API的基本形状:

{
  "id": 1217,
  "name": "Best product ever",
  "tags": [
      {
        "id": 125,
        "name": "Important Value",
        "slug": "important-value"
      },
      {
        "id": 157,
        "name": "Value",
        "slug": "value"
      },
      {
    "id": 180,
    "name": "Value",
    "slug": "value"
  },
  {
    "id": 126,
    "name": "Value",
    "slug": "value"
  },
  {
    "id": 206,
    "name": "Other Important Value",
    "slug": "other-important-value"
  }
}
工作JS代码:

let productAttributes = ['important-value', 'value', 'value', 'value', 'other-important-value'];

filterResults(results) {
let filteredResults = results.filter(product => {
  return product.tags.find(tag => {
    return tag.slug === this.productAttributes[0];
  });
});

if (this.productAttributes[0] !== 'certain important value') {
  filteredResults = filteredResults.filter(product => {
    return product.tags.find(tag => {
      return tag.slug === this.productAttributes[4];
    });
  });
}

return filteredResults;
}
以及(尚未运行)PHP代码:

function get_awesome_products() {
 $baseRequest = 'https://myawesomeapi/wp-json/wc/v3/products/? 
 consumer_key=xxxx&consumer_secret=xxxx&per_page=100&page=';
 for ($count = 1; $count <= 9; $count++ ) {
$request = wp_remote_get( $baseRequest . (string)$count);
$body = wp_remote_retrieve_body( $request );
$data = array_values( json_decode( $body, true ));

  if ($count < 2) {
    $completeProductList = $data;
  } else {
    $completeProductList = array_merge($completeProductList, $data);
  }
}
// The code above this comment is doing what I expect, the code below is not.


$filteredProducts = null;
foreach ($completeProductList as &$product) {
  $tagArray = $product['tags'];
  if (in_array($reg_test_array[0], $tagArray, true) && 
  in_array($reg_test_array[4], $tagArray, true)) 
  {
    array_push($filteredProducts, $product);
  }

  unset($product);
  return new WP_REST_Response($filteredProducts, 200);
函数get\u awesome\u products(){
$baseRequest=https://myawesomeapi/wp-json/wc/v3/products/? 
消费者密钥=xxxx&消费者密钥=xxxx&每页=100&每页=';

对于($count=1;$count,这应该与您发布的JavaScript代码等效,但没有在过滤结果中循环两次

如果不知道
重要值
其他重要值
的上下文,以及它们在
$attributes
数组中是如何排序的,那么要改进所使用的条件检查就有点困难了。不过,到目前为止,我编写的内容感觉像是一种代码味道,因为它依赖于硬编码的值

function filterResults(array $results, array $attributes)
{
    return array_reduce($results, function ($filteredResults, $result) use ($attributes) {
        // Extract tag slugs from result
        $tagSlugs = array_column($result['tags'], 'slug');

        // Append result to filtered results where first attribute exists in tag slugs; 
        // Or first attribute is not *other-important-value* and fourth attribute exists in tag slugs
        if (in_array($attribute[0], $tagSlugs) && ($attribute[0] === 'other-important-value' || in_array($attribute[4], $tagSlugs))) {
            $filteredResults[] = $result;
        }

        return $filteredResults;
    }, []);
}

这应该相当于您发布的JavaScript代码,但没有在过滤结果中循环两次

如果不知道
重要值
其他重要值
的上下文,以及它们在
$attributes
数组中是如何排序的,那么要改进所使用的条件检查就有点困难了。不过,到目前为止,我编写的内容感觉像是一种代码味道,因为它依赖于硬编码的值

function filterResults(array $results, array $attributes)
{
    return array_reduce($results, function ($filteredResults, $result) use ($attributes) {
        // Extract tag slugs from result
        $tagSlugs = array_column($result['tags'], 'slug');

        // Append result to filtered results where first attribute exists in tag slugs; 
        // Or first attribute is not *other-important-value* and fourth attribute exists in tag slugs
        if (in_array($attribute[0], $tagSlugs) && ($attribute[0] === 'other-important-value' || in_array($attribute[4], $tagSlugs))) {
            $filteredResults[] = $result;
        }

        return $filteredResults;
    }, []);
}

您还可以使用PHP等效函数(以及其他一些特定于数组的函数)执行此任务

例如:

// Your 0 and 4 index values from $reg_test_array
$importantTags = [ "important-value", "other-important-value" ];

$filteredProducts = array_filter($completeProductList, function($product) use ($importantTags) {
  return (bool)array_intersect($importantTags, array_column($product['tags'], 'slug'));
});

return new WP_REST_Response($filteredProducts , 200);


您还可以使用PHP等效函数(以及其他一些特定于数组的函数)来完成此任务

例如:

// Your 0 and 4 index values from $reg_test_array
$importantTags = [ "important-value", "other-important-value" ];

$filteredProducts = array_filter($completeProductList, function($product) use ($importantTags) {
  return (bool)array_intersect($importantTags, array_column($product['tags'], 'slug'));
});

return new WP_REST_Response($filteredProducts , 200);


PHP代码的其余部分在哪里?您是否可以包含完整的函数,以便我们可以看到您的
$completeProductList
来自哪里?您是否确定结果是一个数组而不是一个对象?编辑以添加先前的代码。API调用和各种页面的组合正在工作。在Insom中测试此端点的结果nia显示了一个对象数组。一些实际的示例数据也会很有帮助。PHP代码的其余部分在哪里?你能包含完整的函数吗?这样我们就可以看到你的
$completeProductList
来自哪里?你确定结果是数组而不是对象吗?编辑以添加先前的代码。API调用和组合变量ious pages正在工作。在失眠中测试此终结点的结果显示了一个对象数组。一些实际的示例数据也会有所帮助。我听说这是关于硬编码的,但将出现在
$attributes
数组中的值将是相当一致和可预测的。我听说这是关于硬编码的,但值不是将出现在
$attributes
数组中的内容将是相当一致和可预测的。我刚刚用不同
$importantTags
的各种场景测试了这一点,每次都很有魅力!感谢您花时间!我刚刚用不同
$importantTag的各种场景测试了这一点s
,它每次都像一个魔咒一样工作!感谢您抽出时间!