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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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..

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux