迭代数组的问题

时间:2018-03-01 15:50:32

标签: php json foreach

我在查看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文件中,如果它确实显示错误,如果它没有继续我的其余代码并添加新数据

4 个答案:

答案 0 :(得分:1)

对于第二次迭代,$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);
}

答案 1 :(得分:0)

当您进入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
    }
}

答案 2 :(得分:0)

缺少分号和东西会使得更难获得理想的结果。

这样的事情可能会帮助您获得所需的数据。基本上,您可以通过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);

答案 3 :(得分:0)

您需要使用一个函数,以便在找到所需的条形码时使用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}