Fill multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Fill multidimensional array

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Fill multidimensional array

Post by josh »

Sorry my brain throws an E_NOTICE on this thread. $link is undefined line 2.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Fill multidimensional array

Post 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.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Fill multidimensional array

Post by klevis miho »

Now I don't know how to echo this lol
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Fill multidimensional array

Post 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)
Post Reply