我无法比较两个数组并将匹配的值返回给新函数。
因此,我从对 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']);
}
aluckdog
千巷猫影