KevMull Posted July 1, 2008 Share Posted July 1, 2008 Hi, i'm fairly new to PHP after recently converting from asp and asp.net. My question sounds like a simple one... I want to lop throug hand extract the variable names from an array and use thes as literal strings but without the '$' prefix (which I assume I use the 'TRIM' function) My array is $inputArray = array($title, $gender, $firstname, $middlename, $lastname, $nationality, $telephone, $dob, $email, $address1, $address2, $address3, $address4, $city, $postcode, $country, $furthercontact, $logDate); The putput I want is... title, gender, firstname etc Many Thanks Link to comment https://forums.phpfreaks.com/topic/112741-extracting-variable-names-to-string-from-an-array/ Share on other sites More sharing options...
papaface Posted July 1, 2008 Share Posted July 1, 2008 Hi, i'm fairly new to PHP after recently converting from asp and asp.net. My question sounds like a simple one... I want to lop throug hand extract the variable names from an array and use thes as literal strings but without the '$' prefix (which I assume I use the 'TRIM' function) My array is $inputArray = array($title, $gender, $firstname, $middlename, $lastname, $nationality, $telephone, $dob, $email, $address1, $address2, $address3, $address4, $city, $postcode, $country, $furthercontact, $logDate); The putput I want is... title, gender, firstname etc Many Thanks Why do you want this? I can't think of any reason why you would want to do this. In any chase the array will simple evaluate to $inputArray = array(,,,,,,,,,,,,,,,,,); when it is run. TRIM is not used in this way. If you had an array like: array('$title', '$gender', '$firstname', '$middlename', '$lastname','$nationality', '$telephone', '$dob', '$email', '$address1', '$address2', '$address3', '$address4', '$city', '$postcode', '$country', '$furthercontact', '$logDate'); You could use trim() to get the name. Link to comment https://forums.phpfreaks.com/topic/112741-extracting-variable-names-to-string-from-an-array/#findComment-579017 Share on other sites More sharing options...
KevMull Posted July 2, 2008 Author Share Posted July 2, 2008 Thanks for the reply The reason I want teh varaible names is because I need to dynamlcally create an xml. The xml file will use the variable names as its tags and the array values inserted into the tags.. The variables in the array are posted values from an input form page. e.g. <title>Mr</title> <gender>male</gender> <firstname>Hohn</firstname> etc At the moment I have a process using DOM to add each tag seperatley with the tag hard coded, which is not very desirable. Link to comment https://forums.phpfreaks.com/topic/112741-extracting-variable-names-to-string-from-an-array/#findComment-579946 Share on other sites More sharing options...
ag3nt42 Posted July 2, 2008 Share Posted July 2, 2008 you could just write to a file and echo out each line as you want it in XML and just plug in your values.. ex: echo("<title>".$TITLE."</title>".PHP_EOL); echo("<gender>".$GENDER."</gender>"); Link to comment https://forums.phpfreaks.com/topic/112741-extracting-variable-names-to-string-from-an-array/#findComment-579955 Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2008 Share Posted July 2, 2008 Your array would actually be something like - $inputArray = array('title' => 'Mr', 'gender' => 'male'); This would give you key/value pairs. If the order of the items in the array matches the order you want to output them in, then a simple foreach() loop will work (untested but you can get the idea) - $content = ""; // string to build the content in foreach($inputArray as $key => $value) { $content .= "<$key>$value</$key>\n"; } // $content will contain the xml Edit: Added the missing / in the closing xml tag code Link to comment https://forums.phpfreaks.com/topic/112741-extracting-variable-names-to-string-from-an-array/#findComment-579961 Share on other sites More sharing options...
KevMull Posted July 2, 2008 Author Share Posted July 2, 2008 Thanks again, almost works.. $content is missing all the closing tags and is outputting... <$gender>Male <$firstname>Kevin <$lastname>Smith etc etc Thanks Link to comment https://forums.phpfreaks.com/topic/112741-extracting-variable-names-to-string-from-an-array/#findComment-580005 Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2008 Share Posted July 2, 2008 I edited my post above with a missing / in the closing xml tag. This might be why your closing tags are not correct. You would need to post your code and the data to get specific help with what it is doing. If there are $, then either they are present in the data or you are using single-quotes around variable names. Link to comment https://forums.phpfreaks.com/topic/112741-extracting-variable-names-to-string-from-an-array/#findComment-580011 Share on other sites More sharing options...
KevMull Posted July 2, 2008 Author Share Posted July 2, 2008 It was post edit, but all is working now. Many thanks for your help For those who are interested what i'm doing is from a simple name and address input form, getting the posted variable names and values and outputting them to an xml file. The code will either generate a new xl file if it doesn't exist OR append the tags and values to an existing xml file. Here's part of the main code.... // START XML Generate/Append if (file_exists($filename)) { // echo "The file $filename exists"; } else { // If doesn't exits, create xml file and xml headers // echo "The file $filename does not exist"; $ourFileHandle = fopen($filename, 'w') or die("can't open file"); $xmlHeader = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n"; $xmlHeader .= "<" . $xmlRouteNode . ">\r\n</" . $xmlRouteNode . ">"; fwrite($ourFileHandle, $xmlHeader); fclose($ourFileHandle); } $filename= "myXMLfile.xml"; $pDOMdoc = new DomDocument(); $pDOMdoc->load($filename);//load your exsisting xml doc //get the root xmlrouteNode in the xml file $peopleNode = $pDOMdoc->getElementsByTagName($xmlRouteNode)->item(0); //create new '<person>' node and add it to xmlrouteNode $personNode = $pDOMdoc->createElement('person'); $personNode = $peopleNode->appendChild($personNode); //These are the posted values from a seperate form page. $inputArray = array('$title'=> $title, '$gender'=>$gender, '$firstname' => $firstname, '$middlename' => $middlename, '$lastname' => $lastname, '$nationality' => $nationality, '$telephone' =>$telephone, '$dob' => $dob, '$email' =>$email, '$address1' => $address2, '$address2' => $address2, '$address3' => $address3, '$address4' => $address4, '$city' => $city, '$postcode' => $postcode, '$country' => $country, '$furthercontact' => $furthercontact, '$logDate' => $logDate); foreach($inputArray as $key =>$value) { //echo $content .= "<$key>$value</$key>"; $elementName = trim($key, chr(36)); // Remove '$' prefix from $key $node = $pDOMdoc->createElement($elementName); $node = $personNode->appendChild($node); ////add text node to '<name>' node $textNode = $pDOMdoc->createTextNode($value); $textNode = $node->appendChild($textNode); } $pDOMdoc->save($filename); // END XML Generate/Append Link to comment https://forums.phpfreaks.com/topic/112741-extracting-variable-names-to-string-from-an-array/#findComment-580041 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.