2 回答
TA贡献1847条经验 获得超7个赞
这可以很容易地完成,无需任何功能:
$sass = [
[
"article_number" => 010100002,
"stock" => 100
],
[
"article_number" => null,
"stock" => 100
],
[
"article_number" => null,
"stock" => 100
]
];
$sql = [
[
"article_number" => 010100002,
"stock" => 0
],
[
"article_number" => 010100003,
"stock" => 0
],
[
"article_number" => 010100004,
"stock" => 0
]
];
$update_arr = [];
foreach($sass as $sass_prod){
if($sass_prod["article_number"]){
foreach($sql as $sql_prod){
if($sql_prod["article_number"] === $sass_prod["article_number"]){
$update_arr[] = [
"article_number" => $sass_prod["article_number"],
"stock" => $sass_prod["stock"]
];
break;
}
}
}
}
echo "<pre>";
print_r($update_arr);
echo "</pre>";
TA贡献1829条经验 获得超7个赞
假设 article_numbers 是唯一的
选项 1(仅从数据库中选择匹配的项目)
在此选项中,我将仅选择带有 article_numbers 的产品来自您从 SAAS API 返回的数组。您可以循环抛出选择结果并构建最终数组(参见$finalArray选项 2 中的)
$saasArray = [
["article_number" => 010100001, "stock" => 100],
["article_number" => 010100002, "stock" => 200],
["article_number" => 010100003, "stock" => 300],
["article_number" => 010100004, "stock" => 400]
];
$article_numbers = [];
$qMarks = "";
$types = "";
foreach($saasArray as $article){
$qMarks .= "?, ";
$types .= "i";
$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 = [
["article_number" => 010100001, "stock" => 100],
["article_number" => 010100002, "stock" => 200],
["article_number" => 010100003, "stock" => 300],
["article_number" => 010100004, "stock" => 400]
];
$dbArray = [
["article_number" => 010100001, "stock" => 0],
["article_number" => 010100002, "stock" => 0],
["article_number" => 010100003, "stock" => 0],
["article_number" => 010100005, "stock" => 0],
];
$saasArrayIndexed = [];
$dbArrayIndexed = [];
//bulding the indexed arrays (for better performance)
foreach($saasArray as $article){
$saasArrayIndexed[$article["article_number"]] = $article["stock"];
}
foreach($dbArray as $article){
$dbArrayIndexed[$article["article_number"]] = $article["stock"];
}
//remove non commen items
foreach($dbArrayIndexed as $key => $stock){
if (!isset($saasArrayIndexed[$key])) unset($dbArrayIndexed[$key]);
}
//now $dbArrayIndexed holds nonly the common items with the stock data from the database
//build similar array to return to the saas api
var_dump($dbArrayIndexed);
$finalArray = [];
foreach($dbArrayIndexed as $article_number => $stock){
$finalArray[] = ["article_number" => $article_number, "stock"=> $stock];
}
echo "final array \n";
var_dump($finalArray);
exit;
这将输出
array(3) {
[2129921]=>
int(0)
[2129922]=>
int(0)
[2129923]=>
int(0)
}
final array
array(3) {
[0]=>
array(2) {
["article_number"]=>
int(2129921)
["stock"]=>
int(0)
}
[1]=>
array(2) {
["article_number"]=>
int(2129922)
["stock"]=>
int(0)
}
[2]=>
array(2) {
["article_number"]=>
int(2129923)
["stock"]=>
int(0)
}
}
现场演示https://3v4l.org/5kRF5
我更喜欢选项 1 来节省一些资源。
- 2 回答
- 0 关注
- 109 浏览
添加回答
举报