Jump to content

using for loop to display elements from array


Go to solution Solved by Barand,

Recommended Posts

Hi,

I've got a for loop  which displays elements from an array. The array looks like this:

$table = array ( 
            0 => array ( 
                'fname' => 'Peter', 
                'lname' => 'Smith', 
                'age' => '37'
                ), 
            1 => array ( 
                'fname' => 'Paul', 
                'lname' => 'Hartley', 
                'age' => '48'
                ), 
            2 => array ( 
                'fname' => 'Mary', 
                'lname' => 'Baker', 
                'age' => '42'
                ), 
            3 => array ( 
                'fname' => 'Jane', 
                'lname' => 'Doe', 
                'age' => '51'
              
                )
            );

The for loop looks like this:

for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 1; $col++) {
    echo "<li>".$table[$row]['fname']."</li>";
     echo "<li>".$table[$row]['lname']."</li>";
      echo "<li>".$table[$row]['age']."</li>";
  }
  echo "</ul>";
}

Now this code is working fine. Then I set about adding an element to the array using array_push like this:

$newdata =  array (
      'fname' => 'Lionel',
      'lname' => 'Messi',
      'age' => '36'
    );



array_push($table,$newdata);

And I displayed it (sucessfully) using this:

for ($row = 0; $row < 5; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 1; $col++) {
    echo "<li>".$table[$row]['fname']."</li>";
     echo "<li>".$table[$row]['lname']."</li>";
      echo "<li>".$table[$row]['age']."</li>";
  }
  echo "</ul>";
}

Note: The only difference between the above 2 for loops is I changed $row < 4 to $row < 5

Then I wanted to add more than one element to the array so I used this:

$newdata = array ( 
            4 => array ( 
                'fname' => 'Jon', 
                'lname' => 'Atkins', 
                'age' => '27'
                ), 
            5 => array ( 
                'fname' => 'Phil', 
                'lname' => 'Jones', 
                'age' => '14'
                ), 
            6 => array ( 
                'fname' => 'Frank', 
                'lname' => 'Lampard', 
                'age' => '48'
                ), 
            7 => array ( 
                'fname' => 'Toney', 
                'lname' => 'Brentford', 
                'age' => '25'
              
                )
            );

array_push($table,$newdata);

But the weird baffling part is I am using the same forloop:

for ($row = 0; $row < 8; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 1; $col++) {
    echo "<li>".$table[$row]['fname']."</li>";
     echo "<li>".$table[$row]['lname']."</li>";
      echo "<li>".$table[$row]['age']."</li>";
  }
  echo "</ul>";
}

Note : This time I have changed $row < 8 but I get this error :

Row number 4


Notice: Undefined index: fname
 Undefined index: lname
Undefined index: age 

And errors like :

Row number 5


Notice: Undefined offset: 5 in on line 86

Notice: Trying to access array offset on value of type null in  on line 86

Notice: Undefined offset: 5 in  on line 87

Notice: Trying to access array offset on value of type null in  on line 87

Notice: Undefined offset: 5 in  on line 88

Notice: Trying to access array offset on value of type null in  on line 88

And I get the same errors as above for row 5 but for rows 6 and 7.

Btw, I have row numbers for each element in the array. So Row Number 0 displays the First Name, last name and age for the element at index 0 etc.

And I have done a print_r($table) and get this:

Array ( [0] => Array ( [fname] => Peter [lname] => Smith [age] => 37 ) [1] => Array ( [fname] => Paul [lname] => Hartley [age] => 48 ) [2] => Array ( [fname] => Mary [lname] => Baker [age] => 42 ) [3] => Array ( [fname] => Jane [lname] => Doe [age] => 51 ) [4] => Array ( [4] => Array ( [fname] => Jon [lname] => Atkins [age] => 27 ) [5] => Array ( [fname] => Phil [lname] => Jones [age] => 14 ) [6] => Array ( [fname] => Frank [lname] => Lampard [age] => 48 ) [7] => Array ( [fname] => Toney [lname] => Brentford [age] => 25 ) ) )

It seems there are two [4] in the array. I would ideally like to get the rows automatically adjusted so I don't have to keep changing the code when I add new elements in the array. I used 

$arrayLength = count($table);

And changed it to: (only showing relevant code here, the rest is same as above for loops):

$row < $arrayLength;

and I get these errors:

Notice: Undefined index: fname  on line 86

Notice: Undefined index: lname   on line 87

Notice: Undefined index: age  on line 88

But the offset errors disappear 

Sorry for long post

Many thanks

Hey Barand, yes I used foreach before on this array sucessfully but then came across this for loop and thought it look interesting and so I gave it a try.

I'm Using:

foreach ($table as $row => $data) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  foreach ($data as $col) {
    echo "<li>$col[fname]</li>";
    echo "<li>$col[lname]</li>";
    echo "<li>$col[age]</li>";
  }
  echo "</ul>";
}

and I am getting the data from $newdata but getting illegal offset errors all over the page for the other array. Plus the data from $newdata shows all records under Row Number 4 but apparently they should all be in 1 array now ($table)

sorry that should read:

and I am getting the data from $newdata but getting illegal offset errors all over the page for the other array. even though  apparently they should all be in 1 array now ($table). Plus the data from $newdata shows all records under Row Number 4

  • Solution

How many arrays do you now have?

$table = array ( 
            0 => array ( 
                'fname' => 'Peter', 
                'lname' => 'Smith', 
                'age' => '37'
                ), 
            1 => array ( 
                'fname' => 'Paul', 
                'lname' => 'Hartley', 
                'age' => '48'
                ), 
            2 => array ( 
                'fname' => 'Mary', 
                'lname' => 'Baker', 
                'age' => '42'
                ), 
            3 => array ( 
                'fname' => 'Jane', 
                'lname' => 'Doe', 
                'age' => '51'
              
                )
            );
            
$newdata = array ( 
            4 => array ( 
                'fname' => 'Jon', 
                'lname' => 'Atkins', 
                'age' => '27'
                ), 
            5 => array ( 
                'fname' => 'Phil', 
                'lname' => 'Jones', 
                'age' => '14'
                ), 
            6 => array ( 
                'fname' => 'Frank', 
                'lname' => 'Lampard', 
                'age' => '48'
                ), 
            7 => array ( 
                'fname' => 'Toney', 
                'lname' => 'Brentford', 
                'age' => '25'
              
                )
            );
            
$table = array_merge($table, $newdata);

foreach ($table as $row => $data) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  foreach ($data as $col) {
    echo "<li>$col</li>";
  }
  echo "</ul>";
}

giving

image.thumb.png.08c21f82a1fe7bf624eb7cc757126d06.png

There is always more than one way to solve a problem. Instead of array_merge() you could

add them individually, using array_push() as above or

$table[] =  array ( 
                'fname' => 'Jon', 
                'lname' => 'Atkins', 
                'age' => '27'
                );
                 
$table[] =  array ( 
                'fname' => 'Phil', 
                'lname' => 'Jones', 
                'age' => '14'
                ); 
                
$table[] = array ( 
                'fname' => 'Frank', 
                'lname' => 'Lampard', 
                'age' => '48'
                );
                 
$table[] = array ( 
                'fname' => 'Toney', 
                'lname' => 'Brentford', 
                'age' => '25'
                );

or you could loop through the newdata array to add them to $table

foreach ($newdata as $rec) {
    $table[] = $rec;
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.