Jump to content

[SOLVED] Help with Arrays and Tables


x2i

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.