简体   繁体   English

PHP关联数组,仅包含1个元素

[英]PHP associative array with only 1 element

I have a function that return an array ( result are from the bd ) 我有一个返回数组的函数(结果来自bd)

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

foreach resultat in the array, if they don't meet a requirement, I removed them from my array. forarray结果在数组中,如果它们不符合要求,我将其从数组中删除。

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 当我的数组中没有元素或不超过1个元素时,一切工作正常,但是当我只有1个元素时,无法使用索引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 例如,我的BD返回20个元素,并且取消设置除#17之外的所有元素,因此我将必须以这种方式访问​​它

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. 我不明白为什么当数组中只有1个元素时无法使用索引0访问数组。

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. 因此,剩下的数组的索引值为17。

Use array_values to fix this. 使用array_values修复此问题。

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. 因此,如果删除键17上的项目以外的所有项目,则您的一项是键17,而键0不存在。

A quick fix to this would be to run array_values on the result when you assign it to the session 一种快速解决方案是将结果分配给会话时对结果运行array_values

$_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. 如果数组中只有一项,那么它将位于键0。

unset() doesn't reset the array keys. unset()不会重置阵列键。 You can reset array keys by doing the below, which will reset the keys to incrementing numbers from 0 您可以通过执行以下操作来重置阵列键,这会将键重置为从0开始递增的数字

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

PHP doesn't reset your array as you remove elements from it. 当您从数组中删除元素时,PHP不会重置您的数组。 Just because there is only one element in the array doesn't mean that it is element 0. Examples: 仅仅因为数组中只有一个元素并不意味着它就是元素0。示例:

$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". 如果将$ x [5]设置为“ foo”,则不会期望$ x [0]为“ foo”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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