Autor Zpráva
Echo
Profil *
Zdravím,
napsal jsem si jednoduchou funkci pro zjištění rodičů v poli:
	function whereinarray($search, $array, $parent = array()) {
		if(is_array($array)){
			if(in_array($search, $array)){
			        // zde výpis print_r($parent);
				return $parent;
			}
			foreach($array as $key => $element){
				if(is_array($element)){
					$parent[] .= $key;
					$this->whereinarray($search, $element, $parent);
				}
			}
		}
	}


Pole je například:
Array ( [0] => Array ( [0] => 5 [1] => Array ( [0] => 6 ) ) [1] => 43 ) 


Rodiče pro "6" jsou:
Array ( [0] => 0 [1] => 1 ) 


Funkce jako taková funguje, avšak nevrátí mi pole parent. Když odkomentujete ten jeden řádek tak vám to vypíše rodiče pro 6. Nevíte prosím, v čem je problém? Děkuji za odpovědi.
Echo
Profil *
Tak jsem to vyřešil. Chyběl return. Viz:
    function whereinarray($search, $array, $parent = array()) {
        if(is_array($array)){
            if(in_array($search, $array)){
                return $parent;
            }
            foreach($array as $key => $element){
                if(is_array($element)){
                    $parent[] .= $key;
                    return $this->whereinarray($search, $element, $parent); // Chybějící return
                }
            }
        }
    }

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0