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

  • SEARCH
  • Home
  • 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 9238317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:44:01+00:00 2026-06-18T07:44:01+00:00

I have the following array (example, real one is larger) Array ( [0] =>

  • 0

I have the following array (example, real one is larger)

Array
(
    [0] => Array
        (
            [984ab6aebd2777ff914e3e0170699c11] => Array
                (
                  [id] => 984ab6aebd2777ff914e3e0170699c11
                  [message] => Test1

        )

    [1] => Array
        (
            [ca403872d513404291e914f0cad140de] => Array
                (
                  [id] => ca403872d513404291e914f0cad140de
                  [message] => Test2
                )

        )

    [2] => Array
        (
            [ca403872d513404291e914f0cad140de] => Array
                (
                  [id] => ca403872d513404291e914f0cad140de
                  [message] => Test3

        )

    [3] => Array
        (
            [ca403872d513404291e914f0cad140de] => Array
                (
                  [id] => ca403872d513404291e914f0cad140de
                  [message] => Test4
                )

        )
)

Now I want to somehow “access” the subarray with a given id, e.g. access the subarray with ID 984ab6aebd2777ff914e3e0170699c11 and then proceed to use this array in a foreach like this..

foreach ($array_with_specific_id as $event) {
    echo $event['message'];
}

Is this possible?

Edit:

DB code to produce array in my model:

  public function get_event_timeline($id)
   {
     $data = array();
      foreach ($id as $result) {
          $query = $this->db->query("SELECT * FROM event_timeline WHERE id = ?", array($result['id']));
          foreach ($query->result_array() as $row)
          {
               array_push($data, array($row['id'] => $row));
          }
      }

      return $data;
   }
  • 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-18T07:44:03+00:00Added an answer on June 18, 2026 at 7:44 am

    When populating your array from the database you can cerate an additional $index array like the following scheme:

    $index = array (
        '984ab6aebd2777ff914e3e0170699c11' => ReferenceToElementInData,
        'ca403872d513404291e914f0cad140de' => ReferenceToElementInData,
        // ...
    )
    

    This can give you quick access to the elements via their id without an additional foreach loop. Of course it will need additional memory, but this should be ok as you will only save refrences to the original data. However, test it.

    Here comes an example:

    public function get_event_timeline($id)
    {
      $data = array();
    
      // create an additional index array
      $index = array();
    
      // counter 
      $c=0;
    
      foreach ($id as $result) {
          $query = $this->db->query("SELECT * FROM event_timeline WHERE id = ?", array($result['id']));
    
          // every entry in the index is an array with references to entries in $data
          $index[$result['id']] = array();
    
          foreach ($query->result_array() as $row)
          {
               array_push($data, array($row['id'] => $row));
               // create an entry in the current index
               $index[$row['id']][] = &$data[$c];
               $c++;
          }
      }
    
      return array (
          'data' => $data,
          'index' => $index
      );
    }
    

    Now you can access the elements via the index array without an additional foreach loop:

    $entry = $index['984ab6aebd2777ff914e3e0170699c11'][0];
    

    If there are multiple results per $id (as you mentioned in the comments you can access them using index greater then zero:

    $entry = $index['984ab6aebd2777ff914e3e0170699c11'][1];
    $entry = $index['984ab6aebd2777ff914e3e0170699c11'][2];
    

    You can get the count of items per $id by calling

    $number = count($index['984ab6aebd2777ff914e3e0170699c11']);
    

    That’s much the same like indexes that where used in databases to speed up queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following array as3 example: var arrayDefinitionsargsAmfPhp:Array = new Array(); arrayDefinitionsargsAmfPhp['tabela'] =
I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
I have the following array(in python): arr= ['\n', ' a#B\n', ' c#D\n'] Now I
I have taken following array; to fill-up gallery: private Integer[] mImageIds = { R.drawable.one,
I have the following JavaScript array of real estate home objects: var json =
I have the following array ($arrayres) (example data) Array ( [0] => Array (
I have the following example array: $comparisons = array( 0 => array( 'method' =>
I have the following array: var example = [ function() { /* hello */
I have following array, that I need to operate by hand on bitmaps. const
I have the following array: $franchise_a_status[] = array( 'id' => $franchise['franchise_id'], 'text' => $franchise['franchise_surname'].

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.