Hi all,

I have a multi-dimensional array. Using a for function, I would like to display the information inside the array. This means using a variable $i for the first index of the array, which I think is causing the problem.

I am getting the error message:
Undefined offset: 2 Note, that the number changes depending on the value of $i.

The code I am using is:

$created = $results[$i]['data'];

But I get the above error when I echo that information inside the for loop.

Is this because you can't use variables as indexes for arrays?

If this is referring to the code in your other thread, do note that indices start at zero and not one. So check your for loop. If that's not it, provide some more code (including the loop) so we can see what happens with your variable $i.

Thanks. It is referring to the other thread I just posted. But actually I've just realised what I want to do hasn't worked.

My mistake in pulling the mySQL data (in my previous post) was I was using $statement->fetch(); instead of $statement->fetchAll();

Now I have an array: $results. Below is the first row of data so you can see what the array looks like:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [blog_id] => 1
                    [0] => 1
                    [title] => hello world
                    [1] => hello world
                    [post] => test
                    [2] => test
                    [date] => 2015-09-22 17:21:33
                    [3] => 2015-09-22 17:21:33
                    [modified] => 2015-09-22 17:31:33
                    [4] => 2015-09-22 17:31:33
                    [author_id] => 4
                    [5] => 4
                )

Now, I simply want to echo some of that data. My code for that is:

for ($i = 0; $i < 5; $i++)
{
    echo $results[$i]['title'];
    echo '<br>';
}

I am then getting 2 error messages. One is Undefined index: title (which is incorrect since you can see above that there IS an index called title.

Secondly, I get: Notice: Undefined offset: 1, which I get for each item in the array.

Any ideas?

Member Avatar for diafol

Looks like you have an additional level here. Show more of the array. Also choose NUM or ASSOC. I don.t think you need both.

Here is the full array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [blog_id] => 1
                    [0] => 1
                    [title] => hello world
                    [1] => hello world
                    [post] => Test
                    [2] => Test
                    [date] => 2015-09-22 17:21:33
                    [3] => 2015-09-22 17:21:33
                    [modified] => 2015-09-22 17:31:33
                    [4] => 2015-09-22 17:31:33
                    [author_id] => 4
                    [5] => 4
                )

            [1] => Array
                (
                    [blog_id] => 2
                    [0] => 2
                    [title] => hello world
                    [1] => hello world
                    [post] => Test
                    [2] => Test
                    [date] => 2015-09-22 17:21:33
                    [3] => 2015-09-22 17:21:33
                    [modified] => 2015-09-22 17:31:33
                    [4] => 2015-09-22 17:31:33
                    [author_id] => 4
                    [5] => 4
                )

            [2] => Array
                (
                    [blog_id] => 3
                    [0] => 3
                    [title] => hello world
                    [1] => hello world
                    [post] => Test
                    [2] => Test
                    [date] => 2015-09-22 17:21:33
                    [3] => 2015-09-22 17:21:33
                    [modified] => 2015-09-22 17:31:33
                    [4] => 2015-09-22 17:31:33
                    [author_id] => 4
                    [5] => 4
                )

            [3] => Array
                (
                    [blog_id] => 4
                    [0] => 4
                    [title] => hello world
                    [1] => hello world
                    [post] => Test
                    [2] => Test
                    [date] => 2015-09-22 17:21:33
                    [3] => 2015-09-22 17:21:33
                    [modified] => 2015-09-22 17:31:33
                    [4] => 2015-09-22 17:31:33
                    [author_id] => 4
                    [5] => 4
                )

        )

)

I had wondered about the numeric vs associative array. I'm guessing the mySQL query pulls both into the array, how do I specifiy one or the other?

for ($i = 0; $i < 5; $i++)
{
    echo $results[0][$i]['title']; // all items are somehow stored in item 0
    echo '<br>';
}

how do I specifiy one or the other

$result = $sth->fetch(PDO::FETCH_ASSOC);

See jkon's reply in your previous thread.

Oh awesome, that works great! Thanks so much for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.