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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:45:37+00:00 2026-05-20T03:45:37+00:00

I use multidimensional arrays to store product attributes (well Virtuemart does, to be precise).

  • 0

I use multidimensional arrays to store product attributes (well Virtuemart does, to be precise).
When I tried to echo the sub-arrays value, if the sub-array did not exist PHP threw:

Fatal error: Cannot use string offset
as an array

To get around this, I attempted to create a function to check on every array level if it is an actual array, and if it is empty (when trying on the whole thing at once such as: is_array($array['level1']['level2']['level3']), I got the same error if level1 or level2 are not actual arrays).

This is the function ($array contains the array to check, $array_levels is an array containing the names of the sub-arrays, in the order they should apper):

function check_md_array($array,$array_levels){
    if(is_array($array)){
        $dimension = null;  //This will store the dimensions string

        foreach($array_levels as $level){
            $dimension .= "['" . $level . "']"; //Add the current dimension to the dimensions string

            if(!is_array($array/* THE CONTENT OF $dimension SHOULD BE INSERTED HERE*/)){
                return false;
            }
        }           
        return true;
    }
}

How can I take the string contained in $dimensions, and insert it into the code, to be part of the statement?

  • 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-20T03:45:38+00:00Added an answer on May 20, 2026 at 3:45 am

    Short of doing an eval, I don’t think you can do it.

    if(eval("!is_array($array".$dimension.")"))
        return false
    

    However, this is another way to do it

    function check_md_array($array,$array_levels){
        if(!is_array($array))
             return false;
    
        foreach($array_levels as $level){
            if(!isset($array[$level]) || !is_array($array[$level]))
                return false;
            $array = $array[$level];
        }
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use a std::valarray to store/manipulate a 2D array? I'd like to
We can use array_unique() for remove duplicate entry from a single multidimensional array in
I'm starting with iPhone development and I need to use a multidimensional array. I
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
How do I form a multidimensional array using MySQL data? I'm using this code
Are there any reasons not to want to use a multi dimensional SESSION array
I want to save a multidimensional array to a file. A struct for example:
I need to create a multi-dimensional (nested) hashtable/dictionary so that I can use syntax
Use case: 3rd party application wants to programatically monitor a text file being generated

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.