Undefined offset PHP array

让人想犯罪 __ 提交于 2019-12-13 04:21:58

问题


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:

Notice: Undefined offset: 1 in C:\xampp\htdocs\UploadifyZWP\ontdubbelen.php on line 37

And

Notice: Undefined offset: 3 in C:\xampp\htdocs\UploadifyZWP\ontdubbelen.php on line 37

Does anyone see where it is going wrong and why?


回答1:


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



回答2:


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 assigning rather than comparing. 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.

Edit

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

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


来源:https://stackoverflow.com/questions/16936389/undefined-offset-php-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!