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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:06:26+00:00 2026-06-15T12:06:26+00:00

I have some data represented as multi-dimensional arrays. I would like to embed these

  • 0

I have some data represented as multi-dimensional arrays. I would like to embed these data into my OO application and provide extra functionality to these data but also a way to access it both with the traditional random access and using an iterator (so I could use foreach). In other words I would like to implement a wrapping class for a multi-dimensional array.

What I already tried:

I. ArrayAccess
The problem with this one is that the server uses PHP 5.2.17 and &offsetGet() gives an error, thus I can’t return by reference, which means I can’t implement multidimensional element access:

$obj[$key1][$key2] = $value;

Note, that upgrading to a newer PHP version is currently not an option

II. Using magic methods
This is a bit trickier, but my problems rose when I tried using a variable as key. E.g.

$obj->$key1[$key2] = $value;

The interpreter first evaluated $key1[$key2] throwing a notice and returning the first char of $key1, and uses that as key on my array. I don’t know how to solve this one either. I thought of using brackets to force operation priority, but that had the same problem as in my next attempt:

III. Using simple get and set functions
Again, old PHP. It cries when I try to write:

$obj->get($key1)[$key2] = $value;

Because it doesn’t know how to index an expression ending in round brackets.

There’s still the lost resort option: make the array public and forget OO all together. But before I go there I want to be certain that there’s just really no other way of doing this.

  • 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-15T12:06:27+00:00Added an answer on June 15, 2026 at 12:06 pm

    Sometimes, it’s best shown with an example. For instance, you could have a multidimensional array with ordered quantities of a product, where the first index defines a customer, the second an order and the third the products. The value then is the quantity.

    IV. Using simple get and set functions, but with multiple parameters:

    $value = $obj->get($key1, $key2);
    $obj->set($key1, $key2, $value);
    

    or

    $quantity = $orderedQuantities($customerName, $orderNo, $productCode);
    

    The essence of writing an object wrapper, is that it looks like an object and behaves like such. If you implement it so that it looks and behaves like an array, you may just as well use an array. The only reason to wrap an array in an object that behaves like an array is that you can pass it around by reference, without using & parameters.

    If that is your goal, just assign the array to $obj, and use $obj->arrayprop[$key1][$key2]. If on the other hand you want it to behave like an object, skip the array syntax altogether.

    V. Using nested objects
    So using the same example, you can write a customers object (kind of a collection), which embeds order objects, etc..

    $quantity = $customers->getCustomerByName($customerName)->getOrder($orderNo)->getProduct($productCode)->quantity;

    or, to make it more readable:

    $customer = $customers->getCustomerByName($customerName);
    $order = $customer->getOrder($orderNo);
    $orderLine= $order->getOrderLine($productCode);
    $quantity = $product->quantity;

    Now, each of these entities are represented by an object. The advantage is that you can easily extend this structure with extra properties. You can give the customer collection ‘findByCustomerNo’ and ‘findByName’ methods to find a specific customer, but also a ‘filterByPostalCode’, which could return a new collection containing only the customers from that postal code.

    $order could now contain functions to calculate totals.

    $orderLine may contain only a productCode (key) and a quantity, but by making it an object, you can easily add discount, a customized name or description or other properties.

    All in all, this structure allows a lot of flexibility, although it needs a little more typing at first and a little getting used to.

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

Sidebar

Related Questions

I have some data represented in a 1300x1341 matrix. I would like to split
I have a terrain map which i would like to represent some data in.
I have some data from log files and would like to group entries by
Hi I have some data thats represented like this: 0,tcp,http,SF,239,486,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,8,8,0.00,0.00,0.00,0.00,1.00,0.00,0.00,19,19,1.00,0.00,0.05,0.00,0.00,0.00,0.00,0.00,normal. Its from the kdd
I have some data in a database that I need represented in an XML
I have some one-many normalized data that looks like this. a | x a
I have some data which can be represented by an unsigned Integral type and
If I have some json data represented in a dictionary type, what's the pythonic
There's some legacy code that I would like to refactor. There is some data
I have some data loaded as a np.ndarray and need to convert it to

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.