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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:53:42+00:00 2026-05-14T01:53:42+00:00

I am trying to retrieve a value from an Array. Here is the code:

  • 0

I am trying to retrieve a value from an Array. Here is the code:

$opt=get_records_sql($sql1);

print_object($opt);

$n = count($opt);
if (empty($opt)){
    echo 'No options selected';
}
else{
    $optno = $opt["subjectid"];
    echo '<br>$optno = '.$optno;
}

I tried to use: $opt["subjectid"] but I get the following error:

Notice: Undefined index: subjectid

Contents of array:

Array
(
    [1] => stdClass Object
        (
            [uname] => JHollands06
            [tutor] => M LSt
            [subjectid] => 1
            [year] => 2010
            [optid] => 1
        )

)

How to I fetch the data subjectid which has value 1?

  • 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-05-14T01:53:42+00:00Added an answer on May 14, 2026 at 1:53 am

    Method 1: Convert the object to an array by casting it.

    $opt[1] = (array) $opt[1];
    echo $opt[1]['subjectid'];
    

    To convert all objects in an array (if there are more than one):

    foreach ($opt as $k => $val) {
        $opt[$k] = (array) $val;
    }
    

    Method 2: Simply call it as an object like it is already assigned.

    echo $opt[1]->subjectid
    

    There is a difference between an array and an object. An object contains variables that have to be called using the ‘->’ and an array contains values which are associated with a specific key. As your output states, you have an array containing an stdClass object, not another array.

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

Sidebar

Related Questions

I have next problem, when I'm trying to retrieve value from xforms:select elements I
I'm trying to retrieve the attribute value from an xsl:param and use it in
I am trying to retrieve the correct value from an ArrayList of objects (.NET
I am trying to use the getIntent value(score) which is retrieve from the previous
I'm trying to make a spinner with an array that I retrieve from a
I'm trying to retrieve all the data id from a database where their tags(array)
Hi I am trying to retrieve values from database. I have a row which
Im trying to make a function that retrieves a value from the db, but
I am trying to store/retrieve a value that is stored in the Application Settings.
I'm trying to retrieve mulitple checked value, which are being showed in JSP dynamically,

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.