James C
James C

Reputation: 141

Problems iterating through an array

I'm having problems looking through the array data from a json file.

This is the code I have

$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}
}

The problem I have is its only 'looking' at the last set of data and not all of it. if I put the barcode in twice I get the error This user is already in your contacts but if I put a new one in and then the existing barcode again it doesn't work.

I'm sure its something to do with the foreach loop but can not figure it out.

my json data is structured like this :

[
{
    "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"
}
]

what I want to do is see if the barcode number already exists in the json file if it does show the error if it doesn't carry on with the rest of my code and add in the new data

Upvotes: 0

Views: 59

Answers (4)

Kasia Gogolek
Kasia Gogolek

Reputation: 3414

For the second iteration, the $alreadyscanned will be set on a user that doesn't match the condition if one that has been scanned already came before it. Either reset the value of $alreadyscanned or use array to keep a list of errors.

$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);
}

Upvotes: 1

Oussama
Oussama

Reputation: 634

You need to use a function for that, so that you can use return when you find the needed barcode

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}

Upvotes: 0

Alfabravo
Alfabravo

Reputation: 7569

Lacking semicolons and stuff makes it harder to get the desired result.

Something like this might help you out on getting the data you want. Basically you can check the result of parsing by print_r-ing the json decoding process.

Then, you get a process result for each entry and, again, as a test, you can print the resulting array.

<?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);

Upvotes: 0

trincot
trincot

Reputation: 350147

Consider using break in your loop when you get into the if part: you don't want to continue once you find a duplicate:

if($barcodejson == $barcode){
    $alreadyscanned = 'This user is already in your contacts';  
    break;
} else { do something}

Now the dosomething could be unwanted here (depending on what it does). You may need to do that in a separate loop. Something like this:

$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
    }
}

Upvotes: 0

Related Questions