1

I have a array in php that looks like this when doing a print_r($breadcrumb)

Array (
    [url] => /category/
    [text] => Category
)
Array (
    [url] => /category/subcategory/
    [text] => Subcategory
)
Array (
    [url] => /category/subcategory/subsubcategory/
    [text] => Subsubcategory
)

I want this breadcrumb to display as

Category > Subcategory > Subsubcategory

According to Google docs, the HTML should look like this:

<div xmlns:v="http://rdf.data-vocabulary.org/#"> 
  <span typeof="v:Breadcrumb">
    <a href="/category/" rel="v:url" property="v:title">
      Category
    </a> ›
    <span rel="v:child">
      <span typeof="v:Breadcrumb">
        <a href="/category/subcategory/" rel="v:url" property="v:title">
          Subcategory
        </a> ›
        <span rel="v:child">         
          <span typeof="v:Breadcrumb">
            <a href="/category/subcategory/subsubcategory/" rel="v:url" property="v:title">
              Subsubcategory
            </a> ›          
          </span>
        </span>
      </span>
    </span>
  </span>
</div>

How can I go from PHP array to HTML?

I've looked at other answers, but they don't work in my scenario

1
  • Sounds like you're after PHP's DomDocument. Also you're going to have a much easier time if your initial array is structured in the same way you intend for the output. Right now, you're going to have to count to number of occurrences of "sub" within text Commented Mar 8, 2013 at 10:47

1 Answer 1

2

Here's how I would go about this:

The PHP:

<?php
    $breadcrumb = array(
        array(
            'url' => '/category/',
            'text' => 'Category'
        ),
        array(
            'url' => '/category/subcategory/',
            'text' => 'Subcategory'
        ),
        array(
            'url' => '/category/subcategory/subcategory',
            'text' => 'Subsubcategory'
        )
    );
    $children = count($breadcrumb) - 1;
?>

The display markup:

<div xmlns:v="http://rdf.data-vocabulary.org/#">
    <span typeof="v:Breadcrumb">
        <?php for ($i = 0; $i < count($breadcrumb); $i++) : ?>
        <?php if ($i == 0) : ?>
        <a href="<?php echo $breadcrumb[0]['url']; ?>" rel="v:url" property="v:title"><?php echo $breadcrumb[0]['text']; ?></a> › 
        <?php else : ?>
        <span rel="v:child">
            <span typeof="v:Breadcrumb">
                <a href="<?php echo $breadcrumb[$i]['url']; ?>" rel="v:url" property="v:title"><?php echo $breadcrumb[$i]['text']; ?></a> ›
        <?php endif; ?> 
        <?php endfor; ?>
        <?php echo str_repeat("</span>\r\n</span>\r\n", $children); ?>
    </span>
</div>

This approach will also allow deeper sub categories if necessary.

Hope it helps

Updated display code:

<div xmlns:v="http://rdf.data-vocabulary.org/#">
    <span typeof="v:Breadcrumb">
        <?php for ($i = 0; $i < count($breadcrumb); $i++) : ?>
        <?php if ($i == 0) : ?>
        <a href="<?php echo $breadcrumb[0]['url']; ?>" rel="v:url" property="v:title"><?php echo $breadcrumb[0]['text']; ?></a> › 
        <?php else : ?>
        <span rel="v:child">
            <span typeof="v:Breadcrumb">
                <a href="<?php echo $breadcrumb[$i]['url']; ?>" rel="v:url" property="v:title"><?php echo $breadcrumb[$i]['text']; ?></a><?php if ($i < $children):?> › <?php endif; ?>
        <?php endif; ?> 
        <?php endfor; ?>
        <?php echo str_repeat("</span>\r\n</span>\r\n", $children); ?>
    </span>
</div>

The above code trims the last > symbol from the end :)

Sign up to request clarification or add additional context in comments.

1 Comment

I thought that there was need for a recursive function, but your answer is actually working. Not a surprise for you, but for me :) THANK YOU!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.