简体   繁体   中英

PHP associative array with only 1 element

I have a function that return an array ( result are from the bd )

$resultat = $bd->Select($sql);

foreach resultat in the array, if they don't meet a requirement, I removed them from my array.

foreach($indexToRemove as $elem)
    unset($resultat[$elem]);

Then, I put my array into a session array

$_SESSION['entrepriseTrouver'] = $resultat;

Then, I display my result into a page

        $nbResultatParPage = 9; // Correspond au nombre de résultats maximale désirés par page
        $index = (($nbResultatParPage*($_SESSION['page'] - 1)) + 1); // Trouve l'index actuel à afficher ( le nombre de résultats par page * le chiffre de la page précédente ) + 1
        $max = $index + $nbResultatParPage; // Correspond a l'index maximum à afficher


        for($index; $index < $max; $index++) // On affiche les entreprises
        {
            echo "<br/>";
            if(isset($_SESSION['entrepriseTrouver'][$index-1]))
            {
                echo "#".$index."<br/>";
                print_r($_SESSION['entrepriseTrouver'][$index-1]);
                echo "<br/>";
                //echo $_SESSION['entrepriseTrouver'][$index-1][3]."<br/>".$_SESSION['entrepriseTrouver'][$index-1][7]."<br/><br/>";
                echo "-------------------------------------------------------------------------------------------------------------------------------";
            }

        }

Everything working fine when i have no element or more than 1 element into my array but when I only have 1 element, I am not able to access it using the index 0

print_r($_SESSION['entrepriseTrouver'][0]);

I am only able to access it using the key. Example, my BD return 20 elements and I unset all element except the #17, I will have to access it this way

print_r($_SESSION['entrepriseTrouver'][17]);

I don't understand why i cant access my array using the index 0 when I only have 1 element into my array.

Unsetting values in an array does not re-index it. So, you're left with an array with a value at index 17, and that's it.

Use array_values to fix this.

foreach($indexToRemove as $elem){
    unset($resultat[$elem]);
}
$_SESSION['entrepriseTrouver'] = array_values($resultat);

print_r($_SESSION['entrepriseTrouver'][0]);

When you unset an array element you remove that element and it's key. So if you remove all items except the item at key 17, then your one item is key 17 and key 0 does not exist.

A quick fix to this would be to run array_values on the result when you assign it to the session

$_SESSION['entrepriseTrouver'] = array_values($resultat);

This returns all the values in the array and indexes numerically, which has the effect of re-indexing your array. If you have only one item in the array it will now be at key 0.

unset() doesn't reset the array keys. You can reset array keys by doing the below, which will reset the keys to incrementing numbers from 0

$_SESSION['entrepriseTrouver'] = array_values($_SESSION['entrepriseTrouver']);

PHP doesn't reset your array as you remove elements from it. Just because there is only one element in the array doesn't mean that it is element 0. Examples:

$x[] = "foo"

echo $x[0] // echos "foo"

$x[0] = "foo"
$x[1] = "bar"
unset($x[0])
echo $x[0] // echos nothing
echo $x[1] // echos "bar"

You would not expect, if you set $x[5] = "foo", for $x[0] to be "foo".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM