a a a a " name="description" /> a a a a " />
56725

Undefined offset PHP array

Question:

Hello I have a code that checks for duplicates inside an xml file:

XML:

<?xml version="1.0"?> <profielen> <profiel> <voornaam>a</voornaam> <achternaam>a</achternaam> <adres>a</adres> <postcode>a</postcode> <plaats>a</plaats> <email>a</email> </profiel> <profiel> <voornaam>b</voornaam> <achternaam>b</achternaam> <adres>b</adres> <postcode>b</postcode> <plaats>b</plaats> <email>b</email> </profiel> <profiel> <voornaam>c</voornaam> <achternaam>c</achternaam> <adres>c</adres> <postcode>c</postcode> <plaats>c</plaats> <email>c</email> </profiel> <profiel> <voornaam>c</voornaam> <achternaam>c</achternaam> <adres>c</adres> <postcode>cL</postcode> <plaats>c</plaats> <email>c</email> </profiel> </profielen>

I can select 6 checkboxes, the more are selected the more it filters. If I select firstname, only a,b and the first person with the name c can stay and the second one will be ignored.

Now I have this code:

$xml = simplexml_load_file('/xampp/htdocs/UploadifyZWP/uploads/profiel.xml'); //Load the xml file into an array $myArray = $_REQUEST['checkboxarray']; //Contains the selected value (firstname = 0,lastname = 1 etc..) if(count($myArray) <1){ //If $myArray is empty it means no checkboxes are selected and there will be no filtering count($xml); }else{ $fields = $myArray; //If at least one field is selected, this code runs: switch(count($myArray)){ case 1: //One checkbox selected //Array where the profiles go withouth duplicates $profile = array(); $passed = 0; $teller = 0; //Loops through all the profiles in the xml array while($passed < count($xml)){ $add = false; //Checks for all the selected values, 1 checkbox is selected so only 0 (firstname) is selected and goes into $field foreach($fields as $field){ if(count($profile) < 1){ //If profile is empty, add the first profile from the xml array $add = true; }else { if($profile[$teller][$field] != $xml->profiel[$teller][$field]) { $add = true; break; }else{ $teller++; $passed++; } } } if($add = true){ //toevoegen $profile[$teller] = $xml->profiel[$teller]; $teller++; $passed++; } } echo count($profile); print_r($profile); break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; default: echo "error"; } }

So I put all the correct profiles in $profile array and the duplicate will be ignored. Now for testing I print the array profiles to see if it did what it should do, but the outcome is not what I was looking for. The first one (a) will be added to $profiles, the second one (b) will not, the third one (c) is added again and the fourth (duplicate(d)) is not. Now if I add 2 more profiles (e,f), e will be added and f will not. Now I have troubles installing a debugger so I don't see where it is going wrong. I also get an error that says:

<blockquote>

<b>Notice</b>: Undefined offset: 1 in <b>C:\xampp\htdocs\UploadifyZWP\ontdubbelen.php</b> on line <b>37</b><br />

</blockquote>

And

<blockquote>

<b>Notice</b>: Undefined offset: 3 in <b>C:\xampp\htdocs\UploadifyZWP\ontdubbelen.php</b> on line <b>37</b><br />

</blockquote>

Does anyone see where it is going wrong and why?

Answer1:

On this line if($profile[$teller][$field] != $xml->profiel[$teller][$field]) you have specified a third level of a multidimensional array but your data has only 2 levels as shown below.

SimpleXMLElement Object ( [voornaam] => a [achternaam] => a [adres] => a [postcode] => a [plaats] => a [email] => a )

Try this instead

if($profile[$teller] != $xml->profiel[$teller])

Answer2:

There are a few issues with this code, but the main one is that some of the logic in it is flawed.

I'll start by saying that the if statement if($add = true) will always return true, since you are <em>assigning</em> rather than <em>comparing</em>. You need to use if ($add == true) to perform a comparison.

Other than that, it's quite hard to say exactly what you're doing wrong, because it is a general failure in logic. The main issue comes down to the fact that, on the second iteration of your while loop (starting while($passed < count($xml))), the $profile array contains one element at index 0, (meaning that count($profile) < 1 returns false, but the value of $teller is now 1, since you incremented it in the first iteration. You then try to compare the value of $profile[$teller][$field], which fails, because there is no offset of 1 in your $profile array.

<strong>Edit</strong>

To check for the existence of an index in an array, you can use isset, for example

if (isset($profile[$teller]))

Recommend

  • What needs finalization in a multidimensional dynamic array?
  • Laravel - Dynamic relationship using hasManyThough() and unique merge
  • App Script HtmlService use with font like Google Web Fonts?
  • Is there a need to prepend setTimeout and setInterval with window object?
  • Is there a greater chance to collide when comparing GUIDs based on a hash vs \"Guid.NewGuid()?
  • running median of constant size array
  • Html select multiple get all values at onchange event
  • Is it possible to collect a stream to two different collections using one line?
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • Hide HTML elements without javascript, only CSS
  • Copy to all folders batch file?
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Jackson Parser: ignore deserializing for type mismatch
  • Moving mysql files across servers
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Retrieving value from sql ExecuteScalar()
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Perl system calls when running as another user using sudo
  • When should I choose bucket sort over other sorting algorithms?
  • How to make Safari send if-modified-since header?
  • How do you troubleshoot character encoding problems?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How to pass list parameters for each object using Spring MVC?
  • KeystoneJS: Relationships in Admin UI not updating
  • Benchmarking RAM performance - UWP and C#
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • Understanding cpu registers
  • how does django model after text[] in postgresql [duplicate]
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#