0

I want to do something like this

$cars = array('1', '2', '3', '4', '5', '6');
$cars = array('7', '8', '9', '10', '11', '12');
$cars = array('13', '14', '15', '16', '17', '18');

foreach ($cars as $car){
    echo $car[0].'<br />';
    echo $car[1].'<br />';
    echo $car[2].'<br />';
    echo $car[3].'<br />';
    echo $car[4].'<br />';
    echo $car[5].'<br />';
    echo '----------------<br />';
}

to get a result like this

1
2
3
4
5
6
----------------
7
8
9
10
11
12
----------------
13
14
15
16
17
18

I was hoping this code would work but I get errors like :Notice: Uninitialized string offset: 2 in C:\xampp\htdocs\....php on line 9

I have tried a lot of thing like $cars = array(array('1', '2', '3', '4', '5', '6')); but none of them worked for me, so please if anyone could help me with this one, I am new to php and I hope to learn more from you.

3
  • try $cars[] = array(... in your first 3 lines. (add [] after $cars). right now you are overwriting $cars each time. Commented Aug 8, 2015 at 15:17
  • Check out a tutorial on multidimensional arrays first. Then if you have a specific problem of why you code doesn't work ask that. Neither of your attempts suggest that you have tried this first. Your first attempt just redefines the variable in pretty much every programming language out there. The second attempt is closer. Try developphp.com/page.php?id=229 Commented Aug 8, 2015 at 15:18
  • @scrappedcola, I've watched like 5 tutorials, and none of them served what I needed to do! Commented Aug 8, 2015 at 15:25

5 Answers 5

4

You are overwriting your array each time, rather than adding to it.

Try this:

$cars[] = array('1', '2', '3', '4', '5', '6');
$cars[] = array('7', '8', '9', '10', '11', '12');
$cars[] = array('13', '14', '15', '16', '17', '18');

This is the same as

$cars = array(
    array('1', '2', '3', '4', '5', '6'),
    array('7', '8', '9', '10', '11', '12'),
    array('13', '14', '15', '16', '17', '18')
);
Sign up to request clarification or add additional context in comments.

3 Comments

Wow such a simple solution, and I was busting my mind over this, thank you awesome dude, you need to wait 9 minutes for the perfect answer!
@Maria Bernal, please see my answer. The specific error you got is not about overwriting.
@trogne his looping is fine if the array is in the correct format, but it's not because (as above) he's overwriting rather than adding
0

You need to understand that a 2 dimensional array is an array of arrays.

$cars1 = array('1', '2', '3', '4', '5', '6');
$cars2 = array('7', '8', '9', '10', '11', '12');
$cars3 = array('13', '14', '15', '16', '17', '18');
$cars2DArray = array($cars1, $cars2, $cars3)

foreach ($cars2DArray as $cars){
foreach($cars as $car){
echo $car.'<br>';
}
echo '----------------<br>'; }

Comments

0
$cars = array(
    array('1', '2', '3', '4', '5', '6'),
    array('7', '8', '9', '10', '11', '12'),
    array('13', '14', '15', '16', '17', '18')
);

foreach ($cars as $sub) {
    foreach($sub as $item) {
        echo $item;
    }
}

You iterate through each array, then iterate through each item in those arrays.

Comments

0

If you want to have a line break after every 6 numbers, here's an easy solution for you.

<?php

$cars = array();
$cars= array('1', '2', '3', '4', '5', '6','7', '8', '9', '10', '11', '12','13', '14', '15', '16', '17', '18');
$c=0;
foreach($cars as $car)
{
    if($c==6)
    {
        echo '----------------<br />';
        $c=0;
    }
    echo $car . "<br>";
    $c++;
}

?>

Comments

0

The Uninitialized string offset error is not because of overriding.

You get the same error with this :

$cars = array('1', '2', '3', '4', '5', '6');

foreach ($cars as $car){
    echo $car[0].'<br />';
    echo $car[1];
}

Here $car is not an array, it is the array value.

It will work for $car[0], cause php knows how to interpret the only value, but $car[1] doesn't exist (Uninitialized).

1 Comment

I think perhaps you missed the "multidimensional" part of the question.

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.