Jump to content

Extracting variable names to String from an Array


KevMull

Recommended Posts

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

 

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.

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.

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

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.

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.