crims0nluv Posted April 4, 2008 Share Posted April 4, 2008 Hi all, I know this sounds easy but I'm a newbie and I can't seem to be able to get the value in an array. The result is the array that I got $result= Array ( [0] => [1] => [2] => [3] => Array ( [contact] => {{1}} [contact-ATTR] => Array ( [0] => Array ( [name] => rose => [email protected] ) [1] => Array ( [name] => Sylph => [email protected] ) ) ) ) $result = array(); foreach ($result as $contact) { $result[] = array('name' => (string) $contact['name'], 'email' => (string) $contact['email']); } return $result; However, I get the error "Invalid argument supplied for foreach() " . Anyone can help me here? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/99483-foreach-to-get-array-value/ Share on other sites More sharing options...
doni49 Posted April 4, 2008 Share Posted April 4, 2008 $result= Array ( * => [1] => [2] => [3] => Array ( [contact] => {{1}} [contact-ATTR] => Array ( * => Array ( [name] => rose => [email][email protected] ) [1] => Array ( [name] => Sylph => [email][email protected] ) ) ) ) $result = array(); // <-------------------You clear out the result variable and // then try to operate on each element in the array? foreach ($result as $contact) { $result[] = array('name' => (string) $contact['name'], 'email' => (string) $contact['email']); } return $result; Also is the code you posted the exact code you are using? The array doesn't look like it should even get set in the first place--strings without quotes etc. Link to comment https://forums.phpfreaks.com/topic/99483-foreach-to-get-array-value/#findComment-508987 Share on other sites More sharing options...
crims0nluv Posted April 4, 2008 Author Share Posted April 4, 2008 Hi, Thanks for your reply Actually what I'm trying to do it to get the function simplexml_load_string works in PHP4. So what I did is that , ( I got the code from somewhere else) include('xmlobject.php'); $xml = new Xml; $source = $str; $out = $xml->parse($source, NULL); print_r($out); foreach ($out->contact as $contact) { $result[] = array('name' => (string) $contact['name'], 'email' => (string) $contact['email']); } return $result; } the $out gives me the array : Array ( * => [1] => [2] => [3] => Array ( [contact] => {{1}} [contact-ATTR] => Array ( * => Array ( [name] => rose => [email protected] ) [1] => Array ( [name] => Sylph => [email protected] ) ) ) ) I put the xmlobject.php at http://www.alldogsworld.com/xmlobject.txt The code is quite long. Or do you have any other method to replace the function simplexml_load_string in php4 ? Thanks again Link to comment https://forums.phpfreaks.com/topic/99483-foreach-to-get-array-value/#findComment-508991 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.