Re: associative arrays

From: Date: Tue, 02 Nov 2021 17:41:38 +0000
Subject: Re: associative arrays
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-327808@lists.php.net to get a copy of this message
> On Nov 2, 2021, at 5:48 AM, Ashley Sheridan <ash@ashleysheridan.co.uk> wrote: > > On 02/11/2021 06: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) >> >> I would like to find out if there is a way of navigating a >> multidimensional associate array upward: I.E.: >> From a child property/value to a parent property >> >> For instance: >> >> $sampleArray = []; >> $sampleArray['nuts'] = ['treeNuts'=>'walnut']; >> $sampleArray['fruit'] = >> ['notUsuallyFruit'=>['tomatoe'=>true, 'avacado'=>true, >> 'eggplant'=>true]]; >> >> How would I get from 'tomatoe' to 'notUsuallyFruit', to >> 'fruit' and find 'nuts'? Is there a parent property implicit >> in associative arrays in php? I see next and prev in the manual under array functions. But >> I don't >> see anything that suggests moving upward to find a parent and parent syblings. >> >> I written lots of recursive code to search from the root to the tips of the branches. >> >> 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. >> >> Thank you for time and attention >> JK > > An array has no concept of a parent, a nested array has no concept of it being nested within > another array. You're used to this behaviour in Javascript because you're working with > objects. > > Why not use objects in your PHP code? They're more robust and have allow you more control > over arrays. You can have your objects implement the various ArrayObject interfaces (like > IterateAggregate, ArrayAccess, Countable, etc) to allow you to work with the object more like an > array in your code where you need to. You can find more information on this in the manual: > https://www.php.net/manual/en/class.arrayobject.php > > This will allow you to create whatever methods you need on your entity classes to implement > additional behaviours you require. > > -- > Ashley Sheridan > https://www.ashleysheridan.co.uk Thank you for your reply and suggestions: Javascript objects also don't seem to have a concept of parent properties or nesting within other object either. JK

« previous php.general (#327808) next »