x2i Posted June 19, 2009 Share Posted June 19, 2009 Hi, I am struggling a bit with 2 arrays being sorted into a table. Basically I have an array of images which has been filled using the preg_match_all() function and again, an array of text using the same method. My aim is to have two columns in a table, the left column being the images and the right being the text associated with the image. The problem is I can't loop through both the arrays at once. Can anyone help me out at all... if you need more specifics then ask and I will provide them. Thanks in advance for any help given. Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/ Share on other sites More sharing options...
Dathremar Posted June 19, 2009 Share Posted June 19, 2009 Well does this mean that both arrays have same amount of elements? Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859663 Share on other sites More sharing options...
x2i Posted June 19, 2009 Author Share Posted June 19, 2009 Well does this mean that both arrays have same amount of elements? Yes both will have the same amount of items. I tried using the count() feature too. Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859665 Share on other sites More sharing options...
Dathremar Posted June 19, 2009 Share Posted June 19, 2009 Well then something like this: for ($i=0; $i < count($array1); $i++) { echo "<tr><td>".$array1[$i]."</td><td>".$array2[$i]."</td></tr>"; } That should give You separate row with the info image / text. You may echo it in any style ofc Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859666 Share on other sites More sharing options...
x2i Posted June 19, 2009 Author Share Posted June 19, 2009 I tried that but unfortunately I just get the word Array for every entry... When I use this it works however I get all of the elements outputted at once so it's hard to add the tr and td tags in between: foreach ($icons as $icon) { echo $icon[1] . "<br />"; } I then cannot do another similar for loop for the text at the same time as the above code... Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859670 Share on other sites More sharing options...
Dathremar Posted June 19, 2009 Share Posted June 19, 2009 Why is it hard to format them as you want to? Can You give me more of the code, to try to format the output. Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859677 Share on other sites More sharing options...
x2i Posted June 19, 2009 Author Share Posted June 19, 2009 Basically without revealing too much I am using the following to get the html from a page on my website: $html = file_get_contents("mypage.html"); On this page is a list of images each with a description of the image next to it. I am therefore using the following to separate them both into an array: //Get Icons preg_match_all( '/<img id="image.*?" src="(.*?)">/s', $html, $icons, PREG_SET_ORDER); //Get Details preg_match_all( '/<span id="description.*?">(.*?)<\/span>/s', $html, $descs, PREG_SET_ORDER); Now the only way that works to output these is the following: //Output all icons foreach ($icons as $icon) { echo "<img src=\"" . $icon[1] . "\" /><br />"; } //Output all descriptions foreach ($descs as $desc) { echo "<b>" . $desc[1] . "</b><br />"; } However this just outputs all of the images in a vertical line and then all the descriptions, therefore I wanted to put it into a table so the images were next to the right descriptions. Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859681 Share on other sites More sharing options...
Dathremar Posted June 19, 2009 Share Posted June 19, 2009 Ok so even though I still don't get why You can't loop echo them out in a table, I would recommend joining the 2 arrays into one 2 dimensional array ie. $array3 = array_combine($array1, $array2); foreach ($array3 as $img => $desc) { echo "<tr><td>".$img ."</td><td>".$desc."</td></tr>"; // or any other format ir div, span or w/e } Hope it helps Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859687 Share on other sites More sharing options...
nadeemshafi9 Posted June 19, 2009 Share Posted June 19, 2009 best idea is to print_r(array) in order to see what is inside teh array and then you know how you need to access it. Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859689 Share on other sites More sharing options...
taquitosensei Posted June 19, 2009 Share Posted June 19, 2009 if the same index number is used in each array couldn't you do for($a=0;$a<count($imagearray);$a++) { $image=$imagearray[$a]; $text=$textarray[$a]; } Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859690 Share on other sites More sharing options...
xangelo Posted June 19, 2009 Share Posted June 19, 2009 The fact that $icon[$i] in your for each makes me think you have a two dimensional array being set.. therefore, $icons[$i] will give you the Array value but $icon[$i] (which is like saying $icons[$x][$i]) is a value. Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859730 Share on other sites More sharing options...
akitchin Posted June 19, 2009 Share Posted June 19, 2009 each set of matches will be its own array within the parent array. therefore, as xangelo points out, you need to specify the key from one array as the first key for the second array. to use your specific variables: foreach ($icons AS $top_key => $icon_info) { echo '<tr><td>'.$icon_info[1].'</td><td>'.$descs[$top_key][1].'</td></tr>'; } again, everyone has contributed piecemeal to this issue to help solve it - just thought i'd give an example using the variables you've already shown us. often times the best way to know how to access your arrays is to print_r() them as nadeemshafi has pointed out so that you can visualize them. Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859740 Share on other sites More sharing options...
x2i Posted June 19, 2009 Author Share Posted June 19, 2009 None of the above worked i'm afraid although I have solved it myself using some of the methods you guys suggested. For those interested I did it this way: $all_icons = array(); $all_descs = array(); foreach ($descs as $desc) { array_push($all_descs, $desc[1]); } foreach ($icons as $icon) { array_push($all_icons, $icon[1]); } echo "<table>"; for($i=0; $i<count($icons); $i++) { echo "<tr><td><img src=\"" . $all_icons[$i] . "\" /></td> <td>".$all_descs[$i]."</td></tr>"; } echo "</table>"; Thanks for all your help guys... much appreciated. EDIT: I just read akitchin's reply and that worked a treat... thanks, a much shorter solution to the one I came up with. Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859753 Share on other sites More sharing options...
xangelo Posted June 19, 2009 Share Posted June 19, 2009 Just out of curiosity, what was the output of print_r()? Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859754 Share on other sites More sharing options...
x2i Posted June 19, 2009 Author Share Posted June 19, 2009 Not that it will make much sense to you but for the descriptions it was: Array ( [0] => Array ( [0] => (1,000) [1] => (1,000) [2] => (1,000) ) [1] => Array ( [0] => (60) [1] => (60) [2] => (60) ) [2] => Array ( [0] => (60) [1] => (60) [2] => (60) ) [3] => Array ( [0] => (50) [1] => (50) [2] => (50) ) [4] => Array ( [0] => (60) [1] => (60) [2] => (60) ) [5] => Array ( [0] => (60) [1] => (60) [2] => (60) ) [6] => Array ( [0] => (40) [1] => (40) [2] => (40) ) [7] => Array ( [0] => (30) [1] => (30) [2] => (30) ) [8] => Array ( [0] => (40) [1] => (40) [2] => (40) ) [9] => Array ( [0] => (40) [1] => (40) [2] => (40) ) [10] => Array ( [0] => (40) [1] => (40) [2] => (40) ) [11] => Array ( [0] => (40) [1] => (40) [2] => (40) ) [12] => Array ( [0] => (40) [1] => (40) [2] => (40) ) [13] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [14] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [15] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [16] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [17] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [18] => Array ( [0] => (30) [1] => (30) [2] => (30) ) [19] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [20] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [21] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [22] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [23] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [24] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [25] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [26] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [27] => Array ( [0] => (20) [1] => (20) [2] => (20) ) [28] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [29] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [30] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [31] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [32] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [33] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [34] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [35] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [36] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [37] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [38] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [39] => Array ( [0] => (10) [1] => (10) [2] => (10) ) [40] => Array ( [0] => (10) [1] => (10) [2] => (10) ) ) Link to comment https://forums.phpfreaks.com/topic/162928-solved-help-with-arrays-and-tables/#findComment-859765 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.