/    Sign up×
Community /Pin to ProfileBookmark

Find a key’s location in multi-dimensional Array

So, here is the relevant part of this array’s structure:

[CODE]+–Animation
+–Arrays
+–Buttons
+–Cookies
+–Desctags
+–Dice
+–Directions–SubCats–+–DirectionsAbsolute
| +–DirectionsRelative
|
+–Dream——-SubCats–+–DreamArrival
| +–DreamLeaving
| +–DreamUpload
+–Effects
+–Floors
+–Furres——SubCats–+–FurresActions—–SubCats–+–FurresActionsIdle
| | +–FurresActionsInteract
| | +–FurresActionsMessage
| | +–FurresActionsMove
| | +–FurresActionsOrient
| | +–FurresActionsPosture
| | +–FurresActionsTurn
| |
| +–FurresAppearance–SubCats–+–FurresAppearanceGender
| | +–FurresAppearanceSpecies
| |
| +–FurresDreams
| +–FurreSelect
|
+–Kitterdust
+–Locations—SubCats–+–LocationsAbsolute
| +–LocationsRegions
| +–LocationsRelative
|
+–Messages
+–Objects
+–PhoenixSpeak
+–Request
+–Sound
+–Time
+–Variables
+–Walls
+–Window
+–ZZOther[/CODE]

All the words here are array [i]keys[/i]. I want to be able to

[LIST=1]

  • [*]

    Select an array key


  • [*]

    See if the array that key is in has a parent array


  • [*]

    Get the [I]grand[/I]parent array key


  • [*]

    Repeat to top-level array


  • [*]

    Have all returned values in an array


  • [/LIST]

    For example, if I select FurresAppearanceGender, the array would have three values: “FurresAppearanceGender”, “FurresAppearance” and “Furres”. If I select “Furres” then the array would have only one value: “Furres” itself, since that has no parent array.

    How can I do this?

    to post a comment
    PHP

    11 Comments(s)

    Copy linkTweet thisAlerts:
    @NogDogJun 24.2017 — If the naming convention you used here is rigorously followed, you could just derive it from the text, and not bother with some sort of array traversal thing to loop through every part of the array until you find it.
    [code=php]
    $term = "CamelCaseTerm";
    preg_match_all('/[A-Z][a-z]+/', $term, $matches);
    $hierarchy = !empty($matches[0]) ? $matches[0] : false; // or null?
    var_export($hierarchy);
    [/code]

    Output:
    <i>
    </i>array (
    0 =&gt; 'Camel',
    1 =&gt; 'Case',
    2 =&gt; 'Term',
    )
    Copy linkTweet thisAlerts:
    @Mr_Initial_ManauthorJun 25.2017 — I'm not sure if I'll remember to rigourously follow it, though. I suppose I can edit the code so that the parts of the terms are automatically separated by an underscore or hyphen, and do an explode().
    Copy linkTweet thisAlerts:
    @NogDogJun 26.2017 — This seems to work, at least with my little test set:
    [code=php]
    <?php

    /**
    * @param $search
    * @param $array
    * @param array $matches
    * @return bool
    */
    function getKeyHierarchy($search, $array, Array &$matches)
    {
    foreach($array as $key => $value) {
    $matches[] = $key;
    if($key == $search) {
    return true;
    }
    elseif(is_array($value)) {
    if(getKeyHierarchy($search, $value, $matches)) {
    return true;
    }
    }
    else {
    array_pop($matches);
    }
    }
    if(!empty($matches)) {
    array_pop($matches);
    }
    return false;
    }

    // test
    $data = array(
    't1' => 'top no child',
    't2' => array(
    'c21' => 'child of t2',
    'c22' => 'another child of t2'
    ),
    't3' => array(
    'c31' => 'child of t3',
    'c32' => array(
    'gc321' => 'grandchild of t3',
    'gc322' => 'another grandchild of t3'
    )
    )
    );
    var_dump($data);
    $tests = array('t1', 'c21', 'c32', 'gc322', 'c456');
    foreach($tests as $test) {
    echo "Testing '$test':".PHP_EOL;
    $hierarchy = array();
    $result = getKeyHierarchy($test, $data, $hierarchy);
    var_dump($hierarchy);
    echo PHP_EOL;
    }
    [/code]

    Output:
    <i>
    </i>array(3) {
    ["t1"]=&gt;
    string(12) "top no child"
    ["t2"]=&gt;
    array(2) {
    ["c21"]=&gt;
    string(11) "child of t2"
    ["c22"]=&gt;
    string(19) "another child of t2"
    }
    ["t3"]=&gt;
    array(2) {
    ["c31"]=&gt;
    string(11) "child of t3"
    ["c32"]=&gt;
    array(2) {
    ["gc321"]=&gt;
    string(16) "grandchild of t3"
    ["gc322"]=&gt;
    string(24) "another grandchild of t3"
    }
    }
    }
    Testing 't1':
    array(1) {
    [0]=&gt;
    string(2) "t1"
    }

    Testing 'c21':
    array(2) {
    [0]=&gt;
    string(2) "t2"
    [1]=&gt;
    string(3) "c21"
    }

    Testing 'c32':
    array(2) {
    [0]=&gt;
    string(2) "t3"
    [1]=&gt;
    string(3) "c32"
    }

    Testing 'gc322':
    array(3) {
    [0]=&gt;
    string(2) "t3"
    [1]=&gt;
    string(3) "c32"
    [2]=&gt;
    string(5) "gc322"
    }

    Testing 'c456':
    array(0) {
    }

    ?
    Copy linkTweet thisAlerts:
    @kimiraniJun 26.2017 — If the naming convention you used here is rigorously followed, you could just derive it from the text, and not bother with some sort of array traversal thing to loop through every part of the array until you find it.
    [code=php]
    $term = "CamelCaseTerm";
    preg_match_all('/[A-Z][a-z]+/', $term, $matches);
    $hierarchy = !empty($matches[0]) ? $matches[0] : false; // or null?
    var_export($hierarchy);
    [/code]

    Output:
    <i>
    </i>array (
    0 =&gt; 'Camel',
    1 =&gt; 'Case',
    2 =&gt; 'Term',
    )
    [/QUOTE]


    That is very awesome threat, thanks, i used this code too

    <?php

    /**

    * @param $search

    *
    @param $array

    * @param array $matches

    *
    @return bool

    */

    function getKeyHierarchy($search, $array, Array &$matches)

    {

    foreach($array as $key => $value) {

    $matches[] = $key;

    if($key == $search) {

    return true;

    }

    elseif(is_array($value)) {

    if(getKeyHierarchy($search, $value, $matches)) {

    return true;

    }

    }

    else {

    array_pop($matches);

    }

    }

    if(!empty($matches)) {

    array_pop($matches);

    }

    return false;

    }

    maybe you will to see my [B]film semi[/B] thank yoy
    Copy linkTweet thisAlerts:
    @Mr_Initial_ManauthorJun 26.2017 — ... I have no idea how that works...
    Copy linkTweet thisAlerts:
    @NogDogJun 26.2017 — ... I have no idea how that works...[/QUOTE]

    Welcome to the world of recursive functions. ?
    Copy linkTweet thisAlerts:
    @Mr_Initial_ManauthorJun 29.2017 — What I think I'm going to do is something through SQL instead--use a query so that, when I assign a category, the parent and grandparent categories (if they apply) are also assigned--or that parent categories and grandparent categories are also used in the searches. One of the two. Or both.
    Copy linkTweet thisAlerts:
    @NogDogJun 29.2017 — What I think I'm going to do is something through SQL instead--use a query so that, when I assign a category, the parent and grandparent categories (if they apply) are also assigned--or that parent categories and grandparent categories are also used in the searches. One of the two. Or both.[/QUOTE]

    That can work pretty easily as long as there is a known limit to the number of "generations" (and it's not some large number). It gets a bit trickier if there is no limit, and you just want to traverse the "family tree" until you get to the root node. In the first, easier case, it could be something like:
    <i>
    </i>select
    t1.name as child,
    t2.name as parent,
    t3.name as grandparent
    from table_name t1
    left join table_name t2 on t1.parent_id = t2.id
    left join table_name t3 on t2.parent_id = t3.id
    where t1.name = 'NameOfInterest'

    It's the left joins on the same table to get the ancestors that does the magic. ?
    Copy linkTweet thisAlerts:
    @Mr_Initial_ManauthorJun 29.2017 — The limit is three generations (I have parent_categ and grandparent_categ columns in the table). I decided I only needed to dig as deep as grandparent, and would have to update the table (and all my queries) if I went as deep as greatgrandparent.
    Copy linkTweet thisAlerts:
    @NogDogJun 29.2017 — Sounds like a good option, then. ?
    Copy linkTweet thisAlerts:
    @acentriatechJul 05.2017 — That is very awesome threat, thanks, i used this code too

    <?php

    /**

    * @param $search

    *
    @param $array

    * @param array $matches

    *
    @return bool

    */

    function getKeyHierarchy($search, $array, Array &$matches)

    {

    foreach($array as $key => $value) {

    $matches[] = $key;

    if($key == $search) {

    return true;

    }

    elseif(is_array($value)) {

    if(getKeyHierarchy($search, $value, $matches)) {

    return true;

    }

    }

    else {

    array_pop($matches);

    }

    }

    if(!empty($matches)) {

    array_pop($matches);

    }

    return false;

    }

    maybe you will to see my film semi thank yoy

    [b]**Links removed by Site Staff so it doesn't look like you're spamming us. Please don't post them again.** [/b]
    ×

    Success!

    Help @Mr_Initial_Man spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...