Re: associative arrays
| From: | Tedd Sperling | Date: | Tue, 02 Nov 2021 23:17:35 +0000 |
| Subject: | Re: associative arrays | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-327814@lists.php.net to get a copy of this message | ||
Jeffery:
Nah, that’s not the way memory works.
The file system is a way to store data on a hard drive or whatever (CD’s, so on). There is a way
to store everything via a binary-tree type solution. You need to see/retrieve all the data files and
that is served by binary searches.
Memory is allocated as it is needed and NOT in a binary tree type solution.
For example, if you create a variable, then the memory manager checks to see if there is sufficient
memory and if so, then it assigns a memory allocation large enough to hold the datatype of your
variable and returns a memory address for the variable as well as creating a look-up table for
variable name v memory address.
Later in your program when you use that variable for something, the language God looks up the memory
address of your variable and returns the value found at that memory address.
When you create an array, normally the memory manager would assign a large enough memory allocation
to handle all the items you asked for in the array via datatype requirements. PHP is a bit different
because it is an interpreted language and not something complied immediately before it is run like
Java. The compilation for PHP occurs, but after it is analyzed by the PHP God. That’s the reason
why you can use double brackets in a PHP array and dynamically assign variables to it but you are
not required to predetermine how many elements and what they are in your array. This is unlike other
languages like Java which requires you to define the datatype (i.e., String, FP, INT, or even Objs)
and HOW MANY elements are going to be in the array before you can use it.
So, if you create an array and also assign another array inside of it, the memory manager will
allocate the memory needed and will provides the memory address back (a pointer) for the initial
array and makes space for the second array. Normally, you can’t do that in other languages (like
Java) because arrays must be held specifically to the datatypes (how large) it holds. For example, a
String array cannot hold anything but Strings. PHP, because it is an interpreted language will
allows you to do all sorts of things that many other languages will not allow.
In any event, I do not think there is any construct that will keep track of what an array holds
other than through it’s index. The index can be an integer or a string like in an associated array
(in other words use “string” to match values). But, both array types work the same and have no
way to figure out what’s is inside the initial array other than through pointers to what it holds.
In other words, an array insides an array cannot see the pointers that surround it in the initial
array.
Hope this helps — and for the gurus who are reading this, please don’t hammer me because of some
minor issue I misspoke — I am just trying to help and not write a PHP manual..
Cheers,
Tedd
> On Nov 2, 2021, at 5:21 PM, JEFFRY KILLEN <jekillen@prodigy.net> wrote:
>
>
>
>> On Nov 2, 2021, at 1:03 PM, Tedd Sperling <tedd.sperling@gmail.com> wrote:
>>
>>
>>
>>> On Nov 2, 2021, at 2:25 PM, Christoph M. Becker <cmbecker69@gmx.de> wrote:
>>>
>>> On 02.11.2021 at 07:24, JEFFRY KILLEN wrote:
>>>> Hello:
>>>>
>>>> I have been programming php and javascript for some time.
>>>> I have not come on a way to navigate upward in javascript
>>>> objects (it does not have associative arrays but objects are
>>>> often used in the same way associative arrays are used)
>>>>
>>>> To create a metaphor: it is like a squirrel jumps from another tree and lands on a
>>>> branch. It
>>>> can find its way to the trunk and to the ground.
>>>
>>> The squirrel doesn't ask the branch how to get to the trunk or to the
>>> ground, though. Instead, it remembers its way so far. :)
>>>
>>> --
>>> Christoph M. Becker
>>
>> Not quite, a better analogy would be the squirrel doesn’t know on what branch his father
>> found his mother.
>>
>> More specifically, an array inside another array doesn’t know it’s inside another
>> array. In fact, it’s simply a memory address that can only be located via an index inside another
>> array, which is yet another memory address. Ask any memory address where it is and it can answer
>> you, but it can’t offer much more information other than what it is — after all, it’s just a
>> memory address.
>>
>> If you want data to tell you what data proceeded it, then look into a double linked-list.
>> That might solve your problem.
>>
>> Cheers,
>>
>> Tedd
>>
>> Tedd Sperling
>> tedd.sperling@gmail.com
>
> Yes, the squirrel analogy is a weak analogy. But like a file system, the array is a tree
> structure, as I
> understand it. Code or user can navigate around the file system paths using '..',
> which refers to the
> parent directory of the current directory.
>
> But where memory is concerned, in order to get to a particular branch, or object/array property
> the path has to already be known. Or recursive processing can hunt for a property and save
> what it finds for future reference. But that is the rub. It will have to save the property
> chain as
> a string. Then future reference has to use eval on the string to get the value or properties
> stored
> there. At least that is true for javascript and working with both, I am concerned with being
> able
> to translate from one to another. I presume that php works in a similar way: IE nested
> properties
> can be found with recusive search. But the chain of properties would have to saved as a string
> for future reference. And eval would have to be used to get to the value(s) and/or sibling
> properties.
> represented by the string.
>
> Or is there another way?
>
> I appreciate this exchange. Thank you
> JK
>
Tedd Sperling
tedd.sperling@gmail.com