PHP array help

mash99

New Member
Messages
10
Reaction score
0
Points
0
Warning: array_unshift() [function.array-unshift]: The first argument should be an array in /home/mash99/public_html/thebestoptions.co.cc/book-store/finstall.php on line 383

Warning: array_unshift() [function.array-unshift]: The first argument should be an array in /home/mash99/public_html/thebestoptions.co.cc/book-store/finstall.php on line 384



// get old's spec:
$stopat=$_SERVER['DOCUMENT_ROOT']; // e.g. /usr/www
$running=$mypath; // to start with, e.g. /usr/www/users/ewalker/seo-toys/bookadder
while ($running!==$stopat)
{
$lastslash=strrpos($running,$slash);
if ($lastslash==NULL) break; // emergency fail-safe...
$running=substr($running,0,$lastslash); // e.g. /usr/www/users/ewalker/seo-toys
$target=$running.'/robots.txt';
if (is_file($target)!==FALSE) break;
}
$oldrobots=bringme($running.'/robots.txt'); // may fail if no file found--that's ok
// process:
$robots=NULL;
if ($oldrobots!=NULL)
{
$target=$localslash.$localurl; // such as: /red-cats-books/
foreach($oldrobots as $line)
{if (strpos($line,$target)===FALSE && strpos($line,$localslash.'freebie'.$localslash)===FALSE) $robots[]=$line;} // don't take old package lines
$top=count($robots);
$top=$top-1; // convert to 0-based offset
if (trim($oldrobots[$top])!=NULL) $robots[]=$crlf; // tag on blank separator line (if extant file)
}

// Add local stuff:
array_unshift($robots,$crlf); // prepend a blank separator line...
array_unshift($robots,'Sitemap: '.$freeburl.'Bookshop_index.xml'.$crlf); // ...then prepend the sitemap directive
$robots[]='User-agent: *'.$crlf;
$robots[]='Disallow: '.$localslash.$localurl.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'allbooks/allbooks.'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.$myshop.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'abechange.php'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'abes.php'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'book-search.php'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'free.php'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'holder.php'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'search.php'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'used-books.php'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.'Bookshop_index.xml'.$crlf;
$robots[]='Allow: '.$localslash.$localurl.$freebdirname.'-sitemap'.$crlf;
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
For our sake, use
PHP:
, [html] or [code] tags (as appropriate) to delineate code and make it readable.

The error and source code comments tells you exactly what's wrong: [FONT="Courier New"]$robots[/FONT] isn't an array, which can happen if no "robots.txt" file is found. If a value isn't what you expect, debug. Either use an interactive debugger (install e.g. XAMPP or WAMP, XDEBUG and an [URL="http://xdebug.org/docs/remote"]XDEBUG compatible client[/URL]) or simply print out the current value with [URL="http://php.net/var_dump"][FONT="Courier New"]var_dump[/FONT][/URL] or [URL="http://php.net/var_export"][FONT="Courier New"]var_export[/FONT][/URL]. 

[quote="mash99, post: 673641"][php]  while ($running!==$stopat)
  {
    $lastslash=strrpos($running,$slash);
    if ($lastslash==NULL) break;  // emergency fail-safe...
    $running=substr($running,0,$lastslash);  // e.g. /usr/www/users/ewalker/seo-toys
[/QUOTE]
Try dirname instead.

PHP:
    if (is_file($target)!==FALSE) break;
is_file only returns booleans, so there's no need to compare with FALSE, and there's definitely no need for strict comparison.

PHP:
    {if (strpos($line,$target)===FALSE && strpos($line,$localslash.'freebie'.$localslash)===FALSE) $robots[]=$line;}  // don't take old package lines
Separate lines for readability.

PHP:
  }

  //     Add local stuff:
  array_unshift($robots,$crlf);                                             // prepend a blank separator line...
  array_unshift($robots,'Sitemap: '.$freeburl.'Bookshop_index.xml'.$crlf);  // ...then prepend the sitemap directive
  $robots[]='User-agent: *'.$crlf;
  ...
All these lines should go in the "if ($oldrobots!=NULL) {" block.

PHP:
// get old's spec:
$stopat=$_SERVER['DOCUMENT_ROOT']; // e.g. /usr/www
$running=$mypath; // to start with, e.g. /usr/www/users/ewalker/seo-toys/bookadder
while ($running!==$stopat) {
    $running=dirname($running); // e.g. /usr/www/users/ewalker/seo-toys
    $target=$running.'/robots.txt';
    if (is_file($target)) break;
}

$oldrobots=bringme($running.'/robots.txt'); // may fail if no file found--that's
$robots=NULL;
if ($oldrobots!=NULL) {
    $target=$localslash.$localurl; // such as: /red-cats-books/
    foreach($oldrobots as $line) {
        if (strpos($line,$target)===FALSE
            && strpos($line,$localslash.'freebie'.$localslash)=== FALSE)
        { 
            $robots[]=$line;
        }
    } // don't take old package lines
    $top=count($robots);
    $top=$top-1; // convert to 0-based offset
    if (trim($oldrobots[$top])!=NULL) {
        $robots[]=$crlf; // tag on blank separator line (if extant file)
    }
    
    // Add local stuff:
    array_unshift($robots,$crlf); // prepend a blank separator line...
    array_unshift($robots,'Sitemap: '.$freeburl.'Bookshop_index.xml'.$crlf); // 
...
You should consider refactoring some of that into functions or classes.
 
Top