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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:31:30+00:00 2026-06-17T11:31:30+00:00

EDITED: I am generating a 2D array and storing it in db as json

  • 0

EDITED:
I am generating a 2D array and storing it in db as json string. When I need to modify anything in the array, I then fetch the json string and decode it like

$myarray = (array)json_decode($jsonString);

The dump of array is as

enter image description here

$index = 2;

When I wan to access object at index ‘2’ like $myarray[$index] I get null. Please guide what I am doing wrong?

  • 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-17T11:31:31+00:00Added an answer on June 17, 2026 at 11:31 am

    In your comment you said this “array” was decoded from JSON. When you use json_decode, send true as the 2nd parameter. That tells it to make arrays instead of objects when decoding.

    You’re having trouble because the array is being decoded as an object, which you access using -> instead of [].

    $newArray = json_decode($jsonString, true);
    

    UPDATE: You were trying to do (array)json_decode($jsonString) and that wasn’t working. That’s because PHP is silly when it comes to type-casting.

    Here’s a quote from the PHP docs:

    If an object is converted to an array, the result is an array whose
    elements are the object’s properties. The keys are the member variable
    names, with a few notable exceptions: integer properties are
    unaccessible
    ; private variables have the class name prepended to the
    variable name; protected variables have a ‘*’ prepended to the
    variable name. These prepended values have null bytes on either side.
    This can result in some unexpected behaviour.

    Source: http://php.net/manual/en/language.types.array.php#language.types.array.casting

    So, it wasn’t working because PHP said so.

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

Sidebar

Related Questions

EDITED I'm using Twitter Bootstrap and need to move navbar right( not float:right, but
Edited Now my code is like this: #!/usr/bin/perl # import packages use Net::POP3; use
I am generating Java code on the fly which will then be compiled and
Edited question... Hello, I would like to load a .xhtml file of my composite
Can a pdf attachment be opened from outlook, edited, and then saved back to
Edited after getting answers Some excellent answers here. I like Josh's because it is
Edited at the request of commenters. I hope this is compliant. First post! Trying
Edited: I want to get the actual with of the document with JS. I
(Edited for clarification) My (non-OSGi) application build is in Gradle, and I am trying
Edited with a SSCCE and a workaround I want to programmatically change the location

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.