SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Hybrid View

  1. #1
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will array_walk work with a 2 dimension array? IF so do I just reference the array element like this $element[1] or $element[2] while inside the walking function?
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well here is what I'm doing....

    Code:
    $x=1;
    
    function chopUP($element){
    $element = str_replace("<A HREF = \"", "", $element);
    $element = str_replace("\">", ",", $element);
    $element = str_replace("\n</A>", "", $element);
    $element = str_replace("CHAPTER", "Chapter", $element);
    
    $link_array = explode(",",$element); 
    $main_array[$x][1]=$link_array[1]; 
    $main_array[$x][2]=$link_array[2];
    $x=$x+1; 
    }
    
    function putBACK($element){
    print("<a href = \"$element[1]\">$element[2]</a><br>");
    }
    
    
    array_walk($link, "chopUP");
    array_walk($main_array, "putBACK");
    Code:
    Warning: Wrong datatype in array_walk() call in /home/literatu/public_html/spider/spider.php3 on line 47
    
    Warning: Wrong datatype in array_walk() call in /home/literatu/public_html/spider/spider.php3 on line 48
    any help?

    also link is an array I define up here:

    Code:
    while(!feof($web_content)) {
    
    	$line = fgets($web_content, 1500) ;
        
    	while(ereg("<A HREF=\"[^\"]*\">[^<]*</a>", $line, $match)){
    	print($match[0]);
    	print("<br>\n");
    	$link[$x] = $match[0];
    	$x = $x+1;
    	
        $replace = ereg_replace("\?", "\?", $match[0]);
    	$line = ereg_replace($replace, "", $line);
    	}
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $x=1;
    function chopUP($element){
    .....
    }
    I think you wanna define $x=1 in your function or insert it into function as parameter or define as GLOBAL in your function

  4. #4
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the function acts like a loops because its being array walked. x is a counter variable so if I defined it as 1 inside the loop it wouldn't work.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  5. #5
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was playing around with your code and the only thing I could come up with was: adding the at '@' symbodl before array_walk.

    PHP Code:

    @array_walk($link"chopUP");
    @
    array_walk($main_array"putBACK"); 
    I got that from the manual. Check it out at: http://php.net/manual/en/function.array-walk.php . It also, it specifies that you must use reset() to reset the array as array_walk doesn't do it.

    Do these and the suggestion sylow made and see whwether thry work.

    Peter
    Last edited by petesmc; Feb 20, 2001 at 10:10.

  6. #6
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope - doesn' work.

    Code:
    Warning: Wrong datatype in array_walk() call in /home/literatu/public_html/spider/spider.php3 on line 47
    
    Warning: Wrong datatype in array_walk() call in /home/literatu/public_html/spider/spider.php3 on line 48
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  7. #7
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay I apparently solved part of the problem. The link array wasn't being made because the function way up there had a little error in it - fixed it.

    So the first array walk works - the second one does not and its the second one where I'm passing the 2 dimensional array.

    Code:
    $x=1;
    
    function chopUP($element){
    $element = str_replace("<A HREF = \"", "", $element);
    $element = str_replace("\">", ",", $element);
    $element = str_replace("\n</A>", "", $element);
    $element = str_replace("CHAPTER", "Chapter", $element);
    
    $link_array = explode(",",$element); 
    $main_array[$GLOBALS["x"]][1]=$link_array[1]; 
    $main_array[$GLOBALS["x"]][2]=$link_array[2];
    $GLOBALS["x"]=$GLOBALS["x"]+1;
    return $main_array; 
    }
    
    function putBACK($element){
    print("<a href = \"$element[1]\">$element[2]</a><br>");
    }
    
    
    array_walk($link, 'chopUP');
    array_walk($main_array, 'putBACK');
    
    
    ?>
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  8. #8
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when I Change the first function to look like this:

    Code:
    function chopUP($element){
    $element = str_replace("<A HREF=\"", "", $element);
    $element = str_replace("\">", ",", $element);
    $element = str_replace("\n</A>", "", $element);
    $element = str_replace("CHAPTER", "Chapter", $element);
    
    $link_array = explode(",",$element); 
    $main_array[$GLOBALS["x"]][1]=$link_array[0]; 
    $main_array[$GLOBALS["x"]][2]=$link_array[1];
    $GLOBALS["x"]=$GLOBALS["x"]+1;
    
    echo $main_array[1][1];
    echo $main_array[1][2];
    echo $main_array[2][1];
    echo $main_array[2][2];
    return $main_array; 
    }
    The first 2 rows in the main_array are printed - however they are only printed once.

    This is interesting - if array_walk executes the function once for each element in the array then should these 2 rows be printed more than once? And if for some reason it stalls after executing it once then why is the second row defined at all?
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  9. #9
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind. I've went another way with the program.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  10. #10
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aspen, your code has given me a headache!

    How many elements do you expect to be in $link ; ie how many times do you expect chopUP() to be called in the array_walk?
    PHP Code:
    array_walk($link'chopUP'); 
    Insert a line in function chopUp() to print something when it gets called for debuging; ie
    PHP Code:
    function chopUP($element){
       echo 
    "function chopUP called";
       ... 

  11. #11
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the length of link would have been variable based on the number of chapters in a book - but like I said its done via another method.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •