T
T
Thomas Jefferson2021-10-01 15:16:13
PHP
Thomas Jefferson, 2021-10-01 15:16:13

How to avoid repeating data in php object?

I create an object based on the data received from the database, and when creating an array of objects, the data from the object is repeated, from the beginning with numeric keys and then with text keys, how can I make the object be written only with text keys?

here is the php code:

$total = [];
$query = mysqli_query($conn, $allSites);
while ($row = mysqli_fetch_array($query)) {
  $total[] = $row;
}


and here is the result of print_r($total):
Array
(
    [0] => Array
        (
            [0] => 1
            [red] => 1
            [1] => 175
            [ip] => 175
            [2] => c00aa
            [ded] => 00aa
            [3] => 81a97e83
            [gred] => 1a97e83
            [4] => 23
            [sed] => 23
        )

    [1] => Array
        (
            [0] => 2
            [red] => 2
            [1] => 879
            [ip] => 879
            [2] => 0000d
            [ded] => 0000d
            [3] => 0f280a
            [gred] => 0f280a
            [4] => 555
            [sed] => 555
        )

    [2] => Array
        (
            [0] => 3
            [red] => 3
            [1] => 379
            [ip] => 379
            [2] => 0020d
            [ded] => 0020d
            [3] => 0f28c2767
            [gred] => 0f28c2767
            [4] => 5423
            [sed] => 5423
        )
)


but would like:
Array
(
    [0] => Array
        (
            [red] => 1
            [ip] => 175
            [ded] => 00aa
            [gred] => 1a97e83
            [sed] => 23
        )

    [1] => Array
        (
            [red] => 2
            [ip] => 879
            [ded] => 0000d
            [gred] => 0f280a
            [sed] => 555
        )

    [2] => Array
        (
            [red] => 3
            [ip] => 379
            [ded] => 0020d
            [gred] => 0f28c2767
            [sed] => 5423
        )
)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
londhor, 2021-10-01
@fredded

Pass the MYSQLI_ASSOC parameter to mysqli_fetch_array
to get:

$total = [];
$query = mysqli_query($conn, $allSites);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
  $total[] = $row;
}

A
Anton Shamanov, 2021-10-01
@SilenceOfWinter

read the manual for mysqli_fetch_array() and everything will become clear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question