Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7885851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:10:33+00:00 2026-06-03T05:10:33+00:00

i coded the following object in php. it was working, but after changing something

  • 0

i coded the following object in php. it was working, but after changing something in my structur the directory crawler only save the last found directory in the array.

<?php

include_once('root.php');

class crawler
{

private $pages = array();

/**
 * @param string $dir
 * @return array current_dir
 */
function found_dir($dir)
{
//         echo "Found (dir): {$dir}\n";
    // add $dir to $pages
    $parts = explode("/", $dir);
    $current = &$this->pages;
    // e.g. ./content/page/sub_page/
    //  [0] => '.'
    //  [1] => 'content'
    // [-1] => ''
    for ($i = 2; $i < count($parts) - 1; $i++) {
        if (!in_array('sub', $current))
            $current['sub'] = array();
        if (!in_array($parts[$i], $current['sub']))
            $current['sub'][$parts[$i]] = array('name' => $parts[$i]);
        $current = &$current['sub'][$parts[$i]];
    }
    return $current;
}

/**
 * @param string $file
 */
function found_file($file)
{
//        echo "Found (file): {$file}\n";
    // move to current $dir
    $current = &$this->found_dir($file);
    // add $file to $pages
    $parts = explode(".", substr($file, strrpos($file, "/") + 1));
    if (count($parts) == 1) {
        $current[$parts[0]] = file_get_contents($file);
    } else {
        if (strlen($parts[0]) == 0)
            $current[$parts[1]] = true;
        else if (!in_array($parts[0], $current))
            $current[$parts[0]] = array($parts[1] => file_get_contents($file));
        else
            $current[$parts[0]][$parts[1]] = file_get_contents($file);
    }
}

/**
 * @param string $root
 */
function read_all_files($root = '.')
{
    // vars for handling
    $directories = array();
    $last_letter = $root[strlen($root) - 1];
    $root = ($last_letter == '\\' || $last_letter == '/') ? $root : $root . DIRECTORY_SEPARATOR;
    $directories[] = $root;

    while (sizeof($directories)) {
        $dir = array_pop($directories);
        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if ($file == '.' || $file == '..') {
                    continue;
                }
                $file = $dir . $file;
                if (is_dir($file)) {
                    $directory_path = $file . DIRECTORY_SEPARATOR;
                    array_push($directories, $directory_path);

                } elseif (is_file($file))
                    $this->found_file($file);
            }
            closedir($handle);
        }
    }
}

/**
 * @return root
 */
public function generatePages()
{
    // get available files, create temp array
    $this->read_all_files('./content/');
    // print
    print_r($this->pages);
    // loop array
    return new root($this->pages);
}

}

i can’t find the error. perhaps somebody of you see the error. thank you very much for help!

beste regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:10:34+00:00Added an answer on June 3, 2026 at 5:10 am

    You’re using in_array where you should be using isset():

    $pages = array('sub' => array())
    
    in_array('sub', $pages) => false
    isset($pages['sub'])    => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code illustrates an object literal being assigned, but with no semicolon afterwards:
The following code works in Firefox but in Internet Explorer it returns error Object
I have the following code which was working but now for some reason does
So far i have the following code, but it doesn't seem to be working,
I'm working in PHP (but in this case I think the programming language doesn't
I have the following code (changed object names, so syntax/spelling errors ignore). public class
the following code (from a database object created for each new view): -(void)update:(NSString *)queryText{
The following code gets a JSON object and then spits its contents out into
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
In the following code $j is the jquery object. I was wondering why I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.