Jump to content

[SOLVED] Small Help with array


john_bboy7

Recommended Posts

I have a code which extracts the title name and link of that title. i get the result fine.. but i dont know how to print it respectively.

 

For example here is my code:

 

preg_match_all('#(?<=class="topictitle">)[^<]+#', $r , $matches1);

preg_match_all('#(?<=<span class="topictitle"></span><a href=")[^"]+#', $r , $matches2);

foreach($matches1[0] as $val1){
foreach($matches2[0] as $val2){
echo $val1 . "<br />\n";
echo $val2 . "<br />\n";
echo "<br>";
}
}

 

Now The above code gives me:

 

Match1(a)

Match2(a)

Match2(b)

Match2©

Match2(d)

 

Match1(b)

Match2(a)

Match2(b)

Match2©

Match2(d)

 

I want it to be like this:

 

Match1(a)

Match2(a)

 

Match1(b)

Match2(b)

 

Match1©

Match2©

 

I hope you got my question.

 

Link to comment
https://forums.phpfreaks.com/topic/154622-solved-small-help-with-array/
Share on other sites

let me know what this prints back to you:

 

preg_match_all('#(?<=class="topictitle">)[^<]+#', $r , $matches1);

preg_match_all('#(?<=<span class="topictitle"></span><a href=")[^"]+#', $r , $matches2);

echo '<pre>';print_r($matches1);echo '</pre>';
echo '<p> </p>';
echo '<pre>';print_r($matches2);echo '</pre>';

let me know what this prints back to you:

 

preg_match_all('#(?<=class="topictitle">)[^<]+#', $r , $matches1);

preg_match_all('#(?<=<span class="topictitle"></span><a href=")[^"]+#', $r , $matches2);

echo '<pre>';print_r($matches1);echo '</pre>';
echo '<p> </p>';
echo '<pre>';print_r($matches2);echo '</pre>';

 

It gives me all the list of titles first then all the list of links i.e. 56 title and 56 links.. But i want it to be like

 

title

link

 

title

link

 

and so on..

 

i hope u get it..

 

Is the no such thing in foreach which causes both value to be in for e.g:

 

foreach($matches1[0] as $val1 AND $matches2[0] as $val2){

 

And then print it...

A simple for loop would do it for you. For example:

 

$size = count($matches1[0]);

for ($i = 0; $i < $size; $i++)
{
echo $matches1[0][$i] . "<br />\n";
echo $matches2[0][$i] . "<br />\n";
echo "<br />\n";
}

 

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' @ line ==> $size = count($matches1[0]);

 

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';'  @ line ==> $size = count($matches1[0]);

 

You are most likely missing a semicolon from the previous line that has code in it ;)

 

Sorry my bad..

Man u r really genius.. you solved my two topics in 1 minute.. Thankyou very much..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.