0

i am playing with an array in PHP (still learning arrays!)

i have the following PHP code to pull data from my eventsTest table, and echo the array;

    <?php
if ($result = $con->query("SELECT * FROM eventsTest")) {
    $array = array();
    while($row=mysqli_fetch_assoc($result)) {
        $array[] = $row;

    }
print "<pre>";
print_r($array);
print "</pre>"; // DEBUG--- show all array data
} 

which prints my array as:

Array
(
    [0] => Array
        (
            [id] => 1
            [dateStart] => 2018-12-26
            [dateEnd] => 2018-12-26
            [name] => Test Event 1
        )

    [1] => Array
        (
            [id] => 2
            [dateStart] => 2018-12-27
            [dateEnd] => 2018-12-27
            [name] => Test Event 2
        )
)

instead of the [0] [1] [2] keys as the index, can i set the dateStart as the key for the array index?

for example:

    Array
(
    [2018-12-26] => Array
        (
            [0] => Array
                (
                    [dateEnd] => 2018-12-27
                    [name] => Test Event 1
                )

        )

    [2018-12-27] => Array
        (
            [0] => Array
                (
                    [dateEnd] => 2018-12-27
                    [name] => Test Event 2
                )

        )

    [2018-12-28] => Array
        (
            [0] => Array
                (
                    [dateEnd] => 2108-12-28
                    [name] => Test Event 3
                )

            [1] => Array
                (
                    [dateEnd] => 2018-12-28
                    [name] => Test Event 4
                )

        )

)

grouping the dateStart also in the same index if there is multiple rows with the same dateStart for example?

1
  • 2
    Yes. You can easily do that in your loop. Commented Dec 23, 2018 at 23:40

2 Answers 2

2

Like this:

while($row=mysqli_fetch_assoc($result)) {
    $array[$row['dateStart']][] = $row;
}

Note that this will give you a structure similar to what you asked for.

 Array
(
    [2018-12-26] => Array
        (
            [0] => Array
                (
                    [dateEnd] => 2018-12-27
                    [name] => Test Event 1
                )

        )

Because this $array[$row['dateStart']][] = is an array $array[$row['dateStart']] with a key $row['dateStart'] and it's value is an array that contains the rows.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like that:

<?php
if ($result = $con->query("SELECT * FROM eventsTest")) {
     $array = [];
     while($row=mysqli_fetch_assoc($result)) {
      if (isset($row['dateStart'])) {
       $array[$row['dateStart']] = $row;
      }
     }

print "<pre>";
print_r($array);
print "</pre>"; // DEBUG--- show all array data
} 

4 Comments

it will overwrite the last one. They would be better off doing $array[$row['dateStart']][]= $row; that way each level would be consistent, it will be hard to tell an array of data from an array of arrays of data.
i didnt need the isset so i left it out, and my index now shows dateStart, but i know in my table i have an index with the same value (multiple dateStart are the same) and its not showing these? is this because they have to be a unique index?
Yes and no. It's because they didn't do what I said above. But yes you only see one because the indexes are unique, so in that case make them arrays of rows of data.
@ArtisticPhoenix you were right, i changed it to add your code and it now shows the groups. thank you very much and good explanation of an already difficult part of arrays to get my head around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.