作者:林海书6758 | 来源:互联网 | 2024-09-28 14:12
篇首语:本文由编程笔记#小编为大家整理,主要介绍了迭代数组的问题相关的知识,希望对你有一定的参考价值。
我在浏览json文件中的数组数据时遇到问题。
这是我的代码
$barcode = '0000000' //just an example this is set as a variable
$json_data = json_decode($json,true);
foreach ($json_data as $data){
$barcodejson = $data['Barcode'];
if($barcodejson == $barcode){
$alreadyscanned = 'This user is already in your contacts';
} else { do something}
}
我遇到的问题是它只是“查看”最后一组数据而不是全部数据。如果我把条形码放两次我得到错误This user is already in your contacts
但如果我把一个新的,然后现有的条形码再次它不起作用。
我确定它与foreach循环有关,但无法弄明白。
我的json数据结构如下:
[
{
"FirstName": "lee",
"LastName": "skelding",
"Email": "mail.mail.com",
"Barcode": "732580652913857773001",
"Phone": "00000000",
"Company": "SKELLATECH V3",
"Position": "CEO"
},
{
"FirstName": "Kenneth",
"LastName": "Brandon",
"Email": "mail.mail.com",
"Barcode": "732559813913833509001",
"Phone": null,
"Company": null,
"Position": null
},
{
"FirstName": "lee",
"LastName": "skelding",
"Email": "mail.mail.com",
"Barcode": "732580652913857773001",
"Phone": "0000000000",
"Company": "SKELLATECH V3",
"Position": "CEO"
}
]
我想要做的是查看json文件中是否已存在条形码编号,如果它确实显示错误,如果它没有继续我的其余代码并添加新数据
答案
对于第二次迭代,$alreadyscanned
将设置在与该条件不匹配的用户上(如果已扫描的用户已经在其之前)。重置$alreadyscanned
的值或使用数组来保留错误列表。
$alreadyscanned = [];
foreach ($json_data as $data){
$barcodejson = $data['Barcode'];
if($barcodejson == $barcode){
$alreadyscanned[$barcodejson] = 'This user is already in your contacts';
} else { do something}
}
foreach($alreadyscanned as $barcode => $error) {
var_dump($barcode. " :: " . $error);
}
另一答案
当你进入break
部分时,考虑在循环中使用if
:一旦找到重复内容,你不想继续:
if($barcodejson == $barcode){
$alreadyscanned = 'This user is already in your contacts';
break;
} else { do something}
现在dosomething
在这里可能是不受欢迎的(取决于它的作用)。您可能需要在单独的循环中执行此操作。像这样的东西:
$alreadyscanned= "";
foreach ($json_data as $data){
$barcodejson = $data['Barcode'];
if($barcodejson == $barcode){
$alreadyscanned = 'This user is already in your contacts';
break;
}
}
if ($alreadyscanned=="") {
foreach ($json_data as $data){
$barcodejson = $data['Barcode'];
// do something
}
}
另一答案
缺少分号和东西会使得更难获得理想的结果。
这样的事情可能会帮助您获得所需的数据。基本上你可以通过print_r
-json解码过程来检查解析的结果。
然后,您将获得每个条目的处理结果,并且再次作为测试,您可以打印生成的数组。
php
//Enter your code here, enjoy!
$json = '[
{
"FirstName": "lee",
"LastName": "skelding",
"Email": "mail.mail.com",
"Barcode": "732580652913857773001",
"Phone": "00000000",
"Company": "SKELLATECH V3",
"Position": "CEO"
},
{
"FirstName": "Kenneth",
"LastName": "Brandon",
"Email": "mail.mail.com",
"Barcode": "732559813913833509001",
"Phone": null,
"Company": null,
"Position": null
},
{
"FirstName": "lee",
"LastName": "skelding",
"Email": "mail.mail.com",
"Barcode": "732580652913857773001",
"Phone": "0000000000",
"Company": "SKELLATECH V3",
"Position": "CEO"
}]'
;
$barcode = '732580652913857773001'; //just an example this is set as a variable
$json_data = json_decode($json, true);
print_r($json_data);
foreach ($json_data as $data){
$barcodejson = $data['Barcode'];
if($barcodejson == $barcode){
$alreadyscanned[] = 'user ' . $data["Email"] .' is already in your contacts';
} else {
$alreadyscanned[] = 'This user '. $data["Email"] .' is not in your contacts';
}
}
print_r($alreadyscanned);
另一答案
您需要使用一个函数,以便在找到所需的条形码时使用return
function searchbarcode($json_data, $barcode)
{
foreach($json_data as $data)
{
if ( $data['Barcode'] == $barcode )
return true;
}
return false;
}
$barcode = '0000000' //just an example this is set as a variable
$json_data = json_decode($json,true);
if(searchbarcode($json_data, $barcode)){
$alreadyscanned = 'This user is already in your contacts';
} else { do something}