开发者

PHP — How to determine number of parents of a child array?

I'm not so strong with arrays but I need to determine how to count the number of parents a child array has in order to determine the indenting to display it as an option in a SELECT.

So, if I have this array:

array(
      'World'=>array(
            'North America'=>array(
                  'Canada'=>array(
                       'City'=>'Toronto'
                   )
            )
      )
);

How would I go about determining how many parents 'City' has in order to translate that into the number of spaces I want to use as an indent?

Thanks for any help.

EDIT: Let's see if I can explain myself better:

I have this code I'm using to build the OPTIONS list for a SELECT:

function toOptions($array) {

    foreach ($array as $key=>$value) {
        $html .= "<option value=\"" . $key . "\" >";

        $html .=  $value['title'];

        $html .= "</option>";

        if (array_key_exists('children', $value)) {
            $html .= toOptions($value['children']);
        }

    }

    return $html;

}

print toOptions($list);

So, I'm trying to determine how to get the number of parents in order to add spaces before the title in this line:

$开发者_C百科html .=  $value['title'];

Like:

$html .= "&nbsp;&nbsp;&nbsp;" . $value['title'];

But, I'm not sure how to figure out how many spaces to add.

Hopefully this is more clear.

Thanks for any help so far.


$x = array(
      'World'=>array(
            'North America'=>array(
                  'Canada'=>array(
                       'City'=>'Toronto'
                   )
            )
      )
);

// This function do something with the key you've found in the array
function visit($name, $depth)
{
    echo $name . ' has ' . $depth . ' parents.'; 
}

// This function visits all the contents aff $array
function find_recursive($array, $depth = 0)
{
    if (is_array($array)) {
        foreach ($array as $k => $value) {
            visit($k, $depth + 1);
            find_recursive($array, $depth + 1);
        }
    }
}

For visiting:

find_recursive($x);


Well. Off the top what you are dealing with is a multi dimensional array.

You could run a count w/ foreach on each level of the array, and use the count number returned +1 for each level the foreach loops through.

I'm not sure if this answers your question, but I am trying to see exactly what it is you are trying to achieve.


As you are already using a recursive function to display that data, you can just extend your function. There is no need to traverse the array more often than one time:

function getWhitespaces($count) {
    $result = '';
    while($count--) {
        $result .= '$nbsp;';
    }
    return $result;
}


function toOptions($array, $level=0) {

    foreach ($array as $key=>$value) {
        $html .= "<option value=\"" . $key . "\" >";

        $html .=  getWhitespaces($level) + $value['title'];

        $html .= "</option>";

        if (array_key_exists('children', $value)) {
            $html .= toOptions($value['children'], $level + 1);
        }

    }

    return $html;

}

print toOptions($list);


Try the following.. Your solution screams for recursion in my mind. Its a bit ugly but it seems to work

$totraverse = array(
    'Moon' => array(
        'Dark Side' => "Death Valley"
    ),
    'Halley Commet' => "Solar System",
    'World' => array(
        'North America' => array(
            'Canada' => array(
                'City' => 'Toronto'
            )
        ), 'South America' => array(
            'Argentina' => array(
                'City' => 'Toronto'
            )
        )
    )
);


function traverse($totraverse_, $path="", $count=0) {
    global $array;
    // echo count($totraverse_) . " count\n";
    if (!is_array($totraverse_)) {
        echo "returning $path and  $key\n";
        return array($path, $count);
    } else {
        foreach ($totraverse_ as $key => $val) {

            echo "assting $path and  $key\n";
            $result = traverse($val, $path . "/" . $key, $count + 1);
            if($result){
                $array[]=$result;
            }
        }
    }
    echo false;
}

$array = array();
traverse($totraverse);

foreach($array as $item){
    echo "{$item[0]}--->{$item[1]}\n";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜