简体   繁体   中英

PHP MySQLi fetch “array push” overrides data

I have 2 arrays:

$arr = [];

$tempArray = [
    'val1' => "xxx",
    'val2' => 0,
    'val3' => 0
];

Then in my mysql query i fill the temp array with values from the current row and finally push him into the $arr:

$stmt->bind_result($tempArray["val1"], $tempArray["val2"], $tempArray["val3"]);

while ( $stmt->fetch () ) {
    array_push($arr, $tempArray);
}

The Problem is, on every loop the "array_push" overrides the data in the $arr .

For example I loop 3 times in the $stmt->fetch() .

1. Loop

$tempArray = [
    'val1' => "Hello",
    'val2' => 1,
    'val3' => 2
]

$arr = [
    0 = [
        'val1' => "Hello",
        'val2' => 1,
        'val3' => 2
    ];
]

2. Loop

$tempArray = [
    'val1' => "Stack",
    'val2' => 3,
    'val3' => 4
]

$arr = [
    0 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ],
    1 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ];
]

3. Loop

$tempArray = [
    'val1' => "Overflow",
    'val2' => 5,
    'val3' => 6
]

$arr = [
    0 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ],
    1 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ],
    2 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ]
]

I never saw this behavior before and i don't know why it does this.

What i want at the end is this:

$arr = [
    0 = [
        'val1' => "Hello",
        'val2' => 1,
        'val3' => 2
    ],
    1 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ],
    2 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ]
]

$stmt class (requested from @Stevish)

$query = "...";
if ( $stmt = $this->db->prepare($query)) {
        $stmt->bind_param('i',  $xxx);
        $stmt->execute();
        $stmt->store_result();
        $$stmt->bind_result($tempArray["val1"], $tempArray["val2"], $tempArray["val3"]);
        while ( $stmt->fetch () ) {
            $arr[] = $tempArr;
        }
    }

Try:

while ( $stmt->fetch () ) {
    $arr[] = $tempArray;
}

The [] after the variable simply lets PHP know that you're adding a new entry to the array. It will be given a numerical value (0, 1, 2) as you require.

The issue is that you are inserting a reference to $tempArray into $arr. Then you change the reference. By the third loop you have 3 references to the same array. That is why the values are showing that way... you can solve this in a rather non intuitive way.

try:

$stmt->bind_result($tempArray["val1"], $tempArray["val2"],$tempArray["val3"]);
while ( $stmt->fetch () ) {
    $x = $tempArray; //This copies the values of $tempArray to $x and each loop will create a new x.
    array_push($arr, $x);
}

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