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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:19:55+00:00 2026-06-16T04:19:55+00:00

I’m working on a project in CodeIgniter 2 and now I’m stuck on the

  • 0

I’m working on a project in CodeIgniter 2 and now I’m stuck on the most basic of concepts.

Model:

Get an object array from my table which contains all rows that match a specific value for a field named foo. There can be one or more rows that match. In this example, two rows.

public function get_sample($foo)
{
    $query = $this->db->get_where('sample_db', array('foo' => $foo));

    return $query->result();
}

Controller:

Assign the results and make output available to view.

public function view($foo)
{
    $data['array'] = $this->sample_model->get_sample($foo);

    $this->load->view('view', $data);
}

View:

echo var_dump($array); // for testing

echo $array[1]->text

var_dump() of $array:

array(2) {
    [0]=> object(stdClass)#23 (4) {
        ["id"]=> string(1) "1"
        ["foo"]=> string(3) "bar"
        ["number"]=> string(4) "1234"
        ["text"]=> string(23) "This is content in 1234"
    }
    [1]=> object(stdClass)#24 (4) {
        ["id"]=> string(1) "2"
        ["foo"]=> string(3) "bar"
        ["number"]=> string(4) "9999"
        ["text"]=> string(23) "This is content in 9999"
    } 
} 

The rendered ouput of echo $array[1]->text; is: This is content in 9999

And I understand how all that is working: $array[1]->text is the content of the text field in the array object with the index of 1, my second object.

However, I have a field called number and I want to access the object with a certain number and get its corresponding text value.

Example: How can I retrieve the value of text where the number is 9999? I cannot use $array[1]->text since I can never be sure of the object’s position in the array. Something like $array['number' => '9999']->text, but I know that’s not right. Maybe I need to loop through the array looking for a match?

This looks so simple yet everything I’ve tried has failed and resulted in various PHP errors. I’ve been studying the PHP manual here and here, but cannot seem to find anything about what I’m looking to do, or maybe I’m just misapplying what I’m reading. Any guidance is appreciated.

In addition to an answer using best practices following the MVC model, I’m hoping for a link to the proper page in the documentation, as well as pointing out any errors in my wording/terminology above.


EDIT:

This answer contains the actual code I used to solve my problem. Although, Yagi’s answer was accepted because it put me in the right direction.

  • 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-16T04:19:56+00:00Added an answer on June 16, 2026 at 4:19 am

    How about using this :

    foreach ($array as $row) {
            if ($row->number == '9999') {
                echo $row->text;
            }
        }
    

    Loop through the array, and find the number object value of 9999 and get the text

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.