Re: Array - partical path to the array's path (part 2)

From: Date: Wed, 28 Sep 2005 15:01:52 +0000
Subject: Re: Array - partical path to the array's path (part 2)
Groups: php.general 
Request: Send a blank email to php-general+get-223360@lists.php.net to get a copy of this message
To the top... Aw!!! This is driving me nut... I can't get it to do what I want it to do... Also, another problem is that we're all not thinking at the same level so I'll just dump the simple code so you all can see what I'm trying to do... [code] function getVal($arr, $path) { $retval = null; if (is_string($path)) $path = explode(",", $path); if (!is_array($arr) || empty($arr) || !is_array($path) || empty($path)) return null; //$path = array_map("trim", $path); while ($c = count($path)) { $key = array_shift($path); if ((!is_string($key) && !is_numeric($key)) || !isset($arr[ $key ]) || ($c > 1 && !is_array($arr[ $key ]))) { return null; } $arr = $arr[ $key ]; } return $arr; } $array = array(); $array['NEWSFEED']['0']['MESSAGE']['0']['COMMENT']['0']['VALUE'] = "&nsp;Comment #1"; $array['NEWSFEED']['0']['MESSAGE']['1']['COMMENT']['0']['VALUE'] = "Comment <span style='color:green;'>#2</span>"; for ($y=0;$y<3;$y++) { //Initialize or reset... //$key = key($array); $key = key($array.$suffix); $suffix .= "['".$key."']"; echo getVal($array,$suffix)." ###<br>"; } [/code] where I want it to look like this when done... --snip-- <NEWSFEED> <MESSAGE> <COMMENT> <![CDATA[&nbsp;Comment #1]]> </COMMENT> </MESSAGE> <MESSAGE> <COMMENT> <![CDATA[Comment <span style='color:green;'>#2</span>]]> </COMMENT> </MESSAGE> </NEWSFEED> --snip-- Problem is I can't use the foreach() loop because it would include the numeric value into the string, like <0>blah</0>... The reason for the numeric number is to count how many time does the non-numeric key name had appear. Thanks..

« previous php.general (#223360) next »