0

I'm having a problem with an array in PHP. The thing is that want to display some users info in a table on my page, these users have several properties. I just want to display the username followed by 4 properties, this properties in my database are boolean type (true/false). In order to represent these values on the list I'm using checkboxes, so each checkbox displays checked or not checked according to the value on database. I've already got those values through a query. I'm just having issues on the displaying on my view (page).

In order to check if the property is true or false I'm using two foreach loop. I use the first one to pass for each user and the second one to evaluate the 4 properties of each user. I also preset an array that is responsible for storing if that property is true or false. So if the property is true the index "selected" on my array is going to be true.

The array structure is:

$choiceProperty['text'] // This already contains the four possible     properties.
$choiceProperty['selected'] // This is going to store true if the property is true.
$choiceProperty['user_id'] // This is going to store the id of the user.

This is my code:

foreach ($user as $key => $userIndiv) {

        foreach ($properties as &$choiceProperty) {

            unset($choiceProperty['selected']);

            if ($choiceProperty['text'] == 'A' && $userIndiv->CategoryA) {
                $choiceProperty['selected'] = true;
            }
            if ($choiceProperty['text'] == 'B' && $userIndiv->CategoryB) {
                $choiceProperty['selected'] = true;
            }
            if ($choiceProperty['text'] == 'C' && $userIndiv->CategoryC) {
                $choiceProperty['selected'] = true;
            }
            if ($choiceProperty['text'] == 'D' && $userIndiv->CategoryD) {
                $choiceProperty['selected'] = true;
            }
            $choiceProperty['user_id'] = $userIndiv->id;
        }

        array_push($finalChoices, $properties); 

    }

As you can see each $userIndiv->CategoryA, CategoryB, CategoryC... already contains the value obtained from the database.

At the end of each user I add the current $properties array to another array $finalChoices in order to check again for the next user and so on.

The problems comes when I printed the $finalChoices array to see its content and I see that it contains an array for each user with its properties but the last index of each array of each user contains the exact same value for all the arrays. See what I mean:

First position of array:

First position of array

Second position of array:

Second position of array

Third and last position of array:

Their and last position of array

As you can see all elements of arrays in their last position have the same values of the last position array. So they're not showing their real value. For example, based on my database the first and second arrays should have "selected" on its last position since they have 'true' in CategoryD.

I'm quite new in PHP and I'm not sure what I'm doing wrong. Of course it could be something missing on my logic that could be making me have this error. If you could help me I'd be grateful!

I also want to apologize for my English, I'm not native speaker and it's still improving. Thank you.

To finish I'd like to add a picture of how that looks on my view: View

Phil
  • 157,677
  • 23
  • 242
  • 245
  • this may help you, https://stackoverflow.com/questions/8220399/php-foreach-pass-by-reference-last-element-duplicating-bug – Zane Sep 03 '18 at 01:34
  • Thank you so much Zane, from that link I could access to another link from the official PHP author and with his explanation I could resolve my problem. The explanation is here: https://bugs.php.net/bug.php?id=71454 So all I had to do was unset not $choiceProperty array but $properties before the second foreach loop. Thank you. – Héctor Sifuentes Sep 03 '18 at 03:50

0 Answers0