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 7951941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:42:47+00:00 2026-06-04T02:42:47+00:00

I have the following code in my app that will show a list of

  • 0

I have the following code in my app that will show a list of topics in alphabetical order and split them by the first tag name in each section block.

e.g:

A
animal
amazing

B
bat
baseball

The code is as follows:

<?php

    foreach($topics as $currentTopic):
        $thisLetter = strtoupper($currentTopic['Topic']['title'][0]);
        $sorted[$thisLetter][] = $currentTopic['Topic']['title'];
        unset($thisLetter);
    endforeach;

    foreach($sorted as $key=>$value):

        echo '<h3 class="alpha"><span>'.$key.'</span></h3>';
        echo '<ol class="tags main">';

        foreach($value as $thisTopic):

            echo '<li class="tag"><em>0</em>';
            echo $this->Html->link('<strong>'.$thisTopic['Topic']['title'].'</strong>',
                        array('controller'=>'topics','action'=>'view','slug'=>$thisTopic['Topic']['slug']),
                        array('escape'=>false,'rel'=>'tag'));
            echo '</li>';
        endforeach;

        echo '</ol>';

    endforeach;

    ?>

However as I have now split the array, I’m finding it difficult to access the other data in the array such as the topic slug used for the link as the $thisTopic variable just stores the title and the other required data. I also want to show the TopicPost count in the <em> as well so if a Topic has 4 related Posts show <em>4</em>

Currently doing what I’m doing gives the error: Fatal error: Cannot use string offset as an array because I’ve split the array…

Can anyone help?

If I debug the $topics array I get the following:

array(
    (int) 0 => array(
        'Topic' => array(
            'id' => '5',
            'title' => 'amazing',
            'slug' => 'amazing'
        ),
        'TopicPost' => array(
            (int) 0 => array(
                'id' => '9',
                'topic_id' => '5',
                'post_id' => '101'
            )
        )
    ),
    (int) 1 => array(
        'Topic' => array(
            'id' => '4',
            'title' => 'amazingness',
            'slug' => 'amazingness'
        ),
        'TopicPost' => array(
            (int) 0 => array(
                'id' => '8',
                'topic_id' => '4',
                'post_id' => '100'
            ),
            (int) 1 => array(
                'id' => '12',
                'topic_id' => '4',
                'post_id' => '101'
            ),
            (int) 2 => array(
                'id' => '4',
                'topic_id' => '4',
                'post_id' => '119'
            )
        )
    ),...
  • 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-04T02:42:49+00:00Added an answer on June 4, 2026 at 2:42 am

    You get the error because you only store the topic’s title.

    I suggest storing the whole topic info, instead of the title only:

    $orderedTopics= array();
    foreach ($topics as $topic) {
      $orderedTopics[strtoupper($topic['Topic']['title'][0])][] = $topic;
    }
    

    And then, to display it:

    foreach ($orderedTopics as $section=>$topics) {
      echo $section;
      foreach ($topics as $topic) {
        echo 'Title: ' . $topic['Topic']['title'];
        echo 'Body: ' . $topic['Topic']['body'];
        //etc...
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following section of code in an app that I am writing:
I have the following code that allows my console app to go to the
I have the following app.config section that I need to translate into code. I
I have a simple html/javascript based Android app that loads with the following code:
Let's say I have the following code in Google App Engine: from urllib import
In my Zend Framework app, I have following code: index.php $app_started_time = time(); layout.phtml
I have the following code: <fx:Script> <![CDATA[ import shared.GlobalsManager; import app.Globals; protected var globals:Globals=GlobalsManager.getGlobals();
Im trying to get debugging working without an app.config. I have the following code:
So, I'm finishing up an iPhone App. I have the following code in place
I have the following node.js server-side code: var app = require('http').createServer(handler) , io =

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.