Page 1 of 1

Fill multidimensional array

Posted: Fri Feb 19, 2010 6:43 am
by klevis miho
I have this string:
title="Legality of prostitution" url="http://books.google.com"
title="Talk:Prostitution/Archive 7" url="http://books.google.ro/books"
title="Names of the usa people" url="http://books.google.ro"
title="Names of the usa people" url="http://books.google.ro"
title="Names of the usa people" url="http://books.google.ro"

Code: Select all

    foreach($matches as $match) {
        //$match is a line of the above string
        preg_match('#url="(.+?)"#s', $match, $link);
        $link = $link[1];
        
        preg_match('#title="(.+?)";#s', $match, $title);
        $title = $title[1];
        
    }
I want to echo this:
Title: Legality of prostitution
Link(s): http://books.google.com
Title: Talk:Prostitution/Archive 7"
Link(s): http://books.google.ro/books
Title: Names of the usa people
Link(s): http://books.google.ro http://books.google.ro http://books.google.ro

Would appreciate any help

Re: Fill multidimensional array

Posted: Fri Feb 19, 2010 7:11 am
by josh
Sorry my brain throws an E_NOTICE on this thread. $link is undefined line 2.

Re: Fill multidimensional array

Posted: Fri Feb 19, 2010 7:15 am
by klevis miho
No $link is ok, it gets the result of the pregmatch.


Anyway I thing I solved this by putting this:
$links[$title][] = $link;
after $title.

Re: Fill multidimensional array

Posted: Fri Feb 19, 2010 7:25 am
by klevis miho
Now I don't know how to echo this lol

Re: Fill multidimensional array

Posted: Fri Feb 19, 2010 3:30 pm
by josh
Sorry you are right, I hate functions that modify their inputs.

Try an array though:

# foreach($matches as $match) {
# //$match is a line of the above string
# preg_match('#url="(.+?)"#s', $match, $linkM);
# $link[] = $linkM[1];
#
# preg_match('#title="(.+?)";#s', $match, $titleM);
# $title[] = $titleM[1];
#
# }

print_r( $title) ;
print_r( $link)