php比较两个数组并发送到函数

我无法比较两个数组并将匹配的值返回给新函数。


因此,我从对 SaaS 平台的 API 调用中获取了一些数据,该平台返回产品的当前库存水平:


Array

(

    [0] => Array

        (

            [article_number] => 010100002

            [stock] => 100

        )


    [1] => Array

        (

            [article_number] => 

            [stock] => 100

        )


    [2] => Array

        (

            [article_number] => 

            [stock] => 100

        )

etc....

接下来,我执行 SQL 查询以从我的数据库中获取一些需要更新当前库存水平的数据,这些数据返回:


Array

(

    [0] => Array

        (

            [article_number] => 010100002

            [stock] => 0

        )


    [1] => Array

        (

            [article_number] => 010100003

            [stock] => 0

        )


    [2] => Array

        (

            [article_number] => 010100004

            [stock] => 0

        )


etc....

我想比较这两个数组进行匹配article_number,如果有一些匹配,article_number我想相应地更新库存。所以我想将匹配的值返回到一个新数组,这样我就可以将新的库存水平发送回 API/SaaS 网上商店。


经过一番阅读,我认为您可以像这样测试数组:


$array1 = array($variants_sku);

$array2 = array($all_products);

$result = array_intersect($array1, $array2);

echo '<pre>';

print_r($result);

或者


$result = array_intersect($variants_sku, $all_products);

echo '<pre>';

print_r($result);

但无论我尝试什么,我都会收到类似Notice: Array to string conversion. 我已经阅读了这个错误的含义,但我无法让它工作!


我的完整代码:


// get all variant skus and stock from webshop

    $limit = 250;


    $count = $api->variants->count();


    $variants = array();

    for($i = 0; $i * $limit < $count; $i++){

      $block_of_variants = $api->variants->get(null, ['limit' => 250, 'page' => ($i + 1)]);

      $variants = array_merge($variants, $block_of_variants);

    }


    $variants_sku = [];

    foreach($variants as $item){

      $variants_sku[] = array('article_number' => $item['sku'],'stock' => $item['stockLevel']);

    }


// get all product art. nr's and stock from database

  $all_products = array();

  $sql = "SELECT article_code, stock FROM products";

  $result = $db->query($sql);


  foreach($result as $row ) {

    $all_products[]= array('article_number' => $row['article_code'],'stock' => $row['stock']);

  }


ABOUTYOU
浏览 110回答 2
2回答

aluckdog

这可以很容易地完成,无需任何功能:$sass = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_number" => 010100002,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"stock" => 100&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "article_number" => null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "stock" => 100&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "article_number" => null,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "stock" => 100&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]];$sql = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "article_number" => 010100002,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"stock" => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_number" => 010100003,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"stock" => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"article_number" => 010100004,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"stock" => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]];$update_arr = [];foreach($sass as $sass_prod){&nbsp; &nbsp; if($sass_prod["article_number"]){&nbsp; &nbsp; &nbsp; &nbsp; foreach($sql as $sql_prod){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($sql_prod["article_number"] === $sass_prod["article_number"]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$update_arr[] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "article_number" => $sass_prod["article_number"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "stock" => $sass_prod["stock"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }}echo "<pre>";print_r($update_arr);echo "</pre>";

千巷猫影

假设 article_numbers 是唯一的选项 1(仅从数据库中选择匹配的项目)在此选项中,我将仅选择带有 article_numbers 的产品来自您从 SAAS API 返回的数组。您可以循环抛出选择结果并构建最终数组(参见$finalArray选项 2 中的)$saasArray = [&nbsp; &nbsp; ["article_number" => 010100001, "stock" => 100],&nbsp; &nbsp; ["article_number" => 010100002, "stock" => 200],&nbsp; &nbsp; ["article_number" => 010100003, "stock" => 300],&nbsp; &nbsp; ["article_number" => 010100004, "stock" => 400]];$article_numbers = [];$qMarks = "";$types = "";foreach($saasArray as $article){&nbsp; &nbsp; $qMarks .= "?, ";&nbsp; &nbsp; $types .= "i";&nbsp; &nbsp; $article_numbers[] = $article["article_number"];}$qMarks = rtrim($qMarks, ", ");// $types = "iiii";// select ..... where article_code in (?,?,?,?)$q = "SELECT article_code, stock FROM products WHERE article_code in ($qMarks);";$stmt = $con->prepare($q);$stmt->bind_param($types, ...$article_numbers);$stmt->execute();选项 2 选择所有项目并将它们与 PHP 匹配如果您有一个包含数据库中所有项目的数组,并且您只想将其过滤为您从 SAAS API 获得的数组中存在的 article_numbers,请选中此选项。我将循环 2 次以构建 2 个数组的 2 个索引数组版本,然后在 DB 数组中再循环一次并删除不匹配的项目。<?php$saasArray = [&nbsp; &nbsp; ["article_number" => 010100001, "stock" => 100],&nbsp; &nbsp; ["article_number" => 010100002, "stock" => 200],&nbsp; &nbsp; ["article_number" => 010100003, "stock" => 300],&nbsp; &nbsp; ["article_number" => 010100004, "stock" => 400]];$dbArray = [&nbsp; &nbsp; ["article_number" => 010100001, "stock" => 0],&nbsp; &nbsp; ["article_number" => 010100002, "stock" => 0],&nbsp; &nbsp; ["article_number" => 010100003, "stock" => 0],&nbsp; &nbsp; ["article_number" => 010100005, "stock" => 0],];$saasArrayIndexed = [];$dbArrayIndexed = [];//bulding the indexed arrays (for better performance)foreach($saasArray as $article){&nbsp; &nbsp; $saasArrayIndexed[$article["article_number"]] = $article["stock"];}foreach($dbArray as $article){&nbsp; &nbsp; $dbArrayIndexed[$article["article_number"]] = $article["stock"];}//remove non commen itemsforeach($dbArrayIndexed as $key => $stock){&nbsp; &nbsp; if (!isset($saasArrayIndexed[$key])) unset($dbArrayIndexed[$key]);}&nbsp;//now $dbArrayIndexed holds nonly the common items with the stock data from the database&nbsp;//build similar array to return to the saas apivar_dump($dbArrayIndexed);$finalArray = [];foreach($dbArrayIndexed as $article_number => $stock){&nbsp; &nbsp; $finalArray[] = ["article_number" => $article_number, "stock"=> $stock];}echo "final array \n";var_dump($finalArray);exit;这将输出array(3) {&nbsp; [2129921]=>&nbsp; int(0)&nbsp; [2129922]=>&nbsp; int(0)&nbsp; [2129923]=>&nbsp; int(0)}final arrayarray(3) {&nbsp; [0]=>&nbsp; array(2) {&nbsp; &nbsp; ["article_number"]=>&nbsp; &nbsp; int(2129921)&nbsp; &nbsp; ["stock"]=>&nbsp; &nbsp; int(0)&nbsp; }&nbsp; [1]=>&nbsp; array(2) {&nbsp; &nbsp; ["article_number"]=>&nbsp; &nbsp; int(2129922)&nbsp; &nbsp; ["stock"]=>&nbsp; &nbsp; int(0)&nbsp; }&nbsp; [2]=>&nbsp; array(2) {&nbsp; &nbsp; ["article_number"]=>&nbsp; &nbsp; int(2129923)&nbsp; &nbsp; ["stock"]=>&nbsp; &nbsp; int(0)&nbsp; }}现场演示https://3v4l.org/5kRF5我更喜欢选项 1 来节省一些资源。
打开App,查看更多内容
随时随地看视频慕课网APP