webdeveloper123 Posted May 3, 2022 Share Posted May 3, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/ Share on other sites More sharing options...
Barand Posted May 3, 2022 Share Posted May 3, 2022 Use foreach() to iterate through arrays. 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595899 Share on other sites More sharing options...
webdeveloper123 Posted May 3, 2022 Author Share Posted May 3, 2022 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) Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595900 Share on other sites More sharing options...
webdeveloper123 Posted May 3, 2022 Author Share Posted May 3, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595901 Share on other sites More sharing options...
Solution Barand Posted May 3, 2022 Solution Share Posted May 3, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595902 Share on other sites More sharing options...
webdeveloper123 Posted May 3, 2022 Author Share Posted May 3, 2022 oh no!! I was thinking maybe it should be array_merge a few hours ago but thought Nah it can't be! anyway, thanks for the code barand! Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595903 Share on other sites More sharing options...
webdeveloper123 Posted May 3, 2022 Author Share Posted May 3, 2022 So it must have done it first time when I inserted one new element into the array, but because I Had more than one element I had to use array_merge? Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595904 Share on other sites More sharing options...
webdeveloper123 Posted May 3, 2022 Author Share Posted May 3, 2022 btw, I just used array_merge on the for loop script and it works! Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595905 Share on other sites More sharing options...
andrejuniordesouza Posted May 3, 2022 Share Posted May 3, 2022 (edited) try like this: Edited May 3, 2022 by andrejuniordesouza Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595906 Share on other sites More sharing options...
Barand Posted May 3, 2022 Share Posted May 3, 2022 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; } Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595907 Share on other sites More sharing options...
webdeveloper123 Posted May 4, 2022 Author Share Posted May 4, 2022 thanks Barand and andrejuniordesouza! Quote Link to comment https://forums.phpfreaks.com/topic/314756-using-for-loop-to-display-elements-from-array/#findComment-1595929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.