Toon posts:

[PHP] Multidimension-array array_search gezocht

Pagina: 1
Acties:
  • 124 views sinds 30-01-2008

Verwijderd

Topicstarter
Ik denk dat ik een multidimension-array array_search() zoek. Heb al wat rond gezocht en recursie icm array_search() geprobeerd, maar ik kwam er niet uit. Hieronder staat beschreven wat ik precies wil:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Array
(
    [0] => aaa
    [1] => bbbb
    [2] => Array
        (
            [0] => 5
            [1] => Array
                (
                    [0] => 0
                    [1] => 345534354
                    [2] => 324
                    [3] => 122
                )

            [2] => lala
            [3] => etc..
        )

)


$array[2][1][1] heeft in bovenstaand geval de waarde 345534354. Nu wil ik graag dat ik 345534354 opgeef en dat de functie mij $array[2][1][1] teruggeeft (oid).

Ik zoek dus de parent-keys die bij een bepaalde value horen. Ik heb het liefst een oplossing die werkt met niet-unieke 'values' (dan moet ik dus een array met verschillende oplossingen terug krijgen).

  • Brakkie
  • Registratie: Maart 2001
  • Niet online

Brakkie

blaat

In de user comments op php.net barst het echt van de voorbeelden. Dus onder het stukje tekst over array_search ;)

http://nl3.php.net/manual/en/function.array-search.php

Ik zou soieso even posten wat je tot nu toe al geprobeerd hebt en wat er precies niet lukt met je code.

[ Voor 39% gewijzigd door Brakkie op 10-08-2005 03:58 ]

Systeem | Strava


Verwijderd

Topicstarter
Ik heb de comments nagelopen op php.net en niks leek te werken.. Maargoed, nu nog eens nagelopen en heb nu wel een functie gevonden die doet wat ik wil. Blijkbaar was ik gister niet helemaal wakker.

De code voor de liefhebber:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
Author: mark meves <manicdepressive at mindless dot com>

recursively descend an arbitrarily deep multidimensional
array, stopping at the first occurence of scalar $needle.
return the path to $needle as an array (list) of keys 
if not found, return null.
(will infinitely recurse on self-referential structures)
*/
function array_search_recursive( $needle, $haystack )
{
    $path = NULL;
    $keys = array_keys($haystack);
    
    while (!$path && (list($toss,$k)=each($keys)))
    {
        $v = $haystack[$k];
        if (is_scalar($v))
        {
            if ($v===$needle)
                $path = array($k);
        }
        elseif (is_array($v))
        {
            if ($path=array_search_recursive( $needle, $v ))
                array_unshift($path,$k);
        }
    }
    
    return $path;
}


Hij geeft echter maar één result als de $needle niet uniek is. Maar dat moet ik zelf wel in kunnen bouwen.

  • m33p
  • Registratie: September 2002
  • Laatst online: 06-02 20:21
Ik gebruik er altijd zo'n soort constructie voor, mischien heb je er wat aan.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    function multi_array_search ($search_value, $array)
    {
        if (is_array($array))
        {
            foreach ($array as $key => $value)
            {
                $result = multi_array_search($search_value, $value);
                
                if (is_array($result))
                {
                    $return = $result;
                    array_unshift($return, $key);
                    return $return;
                }
                elseif ($result == TRUE)
                {
                    $return[] = $key;
                    return $return;
                }
            }
            
            return FALSE;
        }
        else
        {
            if ($search_value == $array)
                return TRUE;
            else
                return FALSE;
        }
    }

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 15-04 22:07

NMe

Quia Ego Sic Dico.

Mooi dat het opgelost is, maar OReason, topics als deze zijn hier niet de bedoeling. Ik kan uit de startpost geen enkele zelfwerkzaamheid opmaken; ik zie nergens wat je concreet al geprobeerd hebt, en waarom dat niet goed was. Codevoorbeelden zijn altijd welkom. Nu komt je vraag neer op "ik zoek een script dat dat en dat doet", en dat is hier ongewenst. Zie ook P&W FAQ - Scriptrequests. :)

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Dit topic is gesloten.