|
Posted by Jochem Maas on 09/28/05 22:37
Scott Fletcher wrote:
> 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]
fantastic function - couldn't have written it better myself. ;-)
> 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>";
>
get to grips with var_dump() ...
var_dump($array);
> for ($y=0;$y<3;$y++)
> {
> //Initialize or reset...
> //$key = key($array);
> $key = key($array.$suffix);
here your doing exactly what you first asked if you could do
and were told you couldn't (not exactly like that anyhow)
($array.$suffix) will never be an array
> $suffix .= "['".$key."']";
this $suffix 'path' is being created in a way that
is not compatible with getVal(). the second arg
to getVal() can either be an array where the values
are the keys to 'dig' in with or a comman delimited
string containg the key names ... e.g:
$path1 = array('key1','key2','key3');
$path2 = 'key1,key2,key3';
$path1 and $path2 will give the same result.
>
> echo getVal($array,$suffix)." ###<br>";
getVal() will only ever return NULL
> }
> [/code]
>
> where I want it to look like this when done...
>
> --snip--
> <NEWSFEED>
> <MESSAGE>
> <COMMENT>
> <![CDATA[ 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
could you rephrase that in english?
> numeric number is to count how many time does the non-numeric key name had
> appear.
array_count_values() might interest you.
>
> Thanks..
>
[Back to original message]
|