0

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

2
  • Your first and last record in your JSON are the same so it could be giving the appearance that it isn't functioning correctly. Commented Mar 1, 2018 at 15:59
  • That's the problem, it allows me to add the new set of data even though it already exists that what I want to do. check to see if the data does already exist and if it does show my error if not add it Commented Mar 1, 2018 at 16:01

4 Answers 4

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

See update to my answer. If dosomething consists of code that adds data, you should really show that in your code as it is an essential part of your question.
Correct, the issue is that you are looping on all rows, and if any DONT MATCH you are adding more to it in do something. The one that does match, skips doing something, but the other rows will keep on doing something since they did not match.
This is working and will not let me add an entry if the barcode already exists, although it is acting weired as it now adds multiple entries. add the first one 1 entry, add the second entry 1 entry add the third and I get 2 duplicate entires add the 4th and I get 4 duplicate entries. I'll have to check over the rest of my code and see if something is conflicting.
That sounds like you have nested loops in your code. Or a loop with a function call where the function also loops over the data. Something like that.
0

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

Comments

0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.