<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:series="http://unfoldingneurons.com/"
> <channel><title>Comments on: Convert Key =&gt; Value Arrays Into Standard Variables In&#160;PHP</title> <atom:link href="http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Tue, 22 May 2012 05:31:04 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>By: Zync</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-26172</link> <dc:creator>Zync</dc:creator> <pubDate>Fri, 22 Jul 2011 04:09:14 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-26172</guid> <description>Thank you all very much for this, was exactly what I was looking for today. using Peter&#039;s approach just whipped up a quick match string, just add your allowed variables to the array.&lt;code&gt;
$acceptedIncomingVars = array(&#039;id&#039;, &#039;name&#039;, &#039;code&#039;, &#039;cost&#039;, &#039;desc&#039;, &#039;mixtypeid&#039;, &#039;mixdetailid&#039;);$acceptedVars = &quot;/&quot;;
for($i = 0; $i  $value) {
$key = strtolower($key);if (preg_match($acceptedVars,$key))
{
$$key = $value;
}
}echo $code;
&lt;/code&gt;</description> <content:encoded><![CDATA[<p>Thank you all very much for this, was exactly what I was looking for today. using Peter&#8217;s approach just whipped up a quick match string, just add your allowed variables to the array.</p><p><code><br
/> $acceptedIncomingVars = array('id', 'name', 'code', 'cost', 'desc', 'mixtypeid', 'mixdetailid');</p><p>$acceptedVars = "/";<br
/> for($i = 0; $i  $value) {<br
/> $key = strtolower($key);</p><p> if (preg_match($acceptedVars,$key))<br
/> {<br
/> $$key = $value;<br
/> }<br
/> }</p><p>echo $code;<br
/> </code></p> ]]></content:encoded> </item> <item><title>By: Pascal</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-22713</link> <dc:creator>Pascal</dc:creator> <pubDate>Sun, 13 Feb 2011 00:57:07 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-22713</guid> <description>As Hugo Magalhães said, I think this woud be a &quot;better&quot; solution...
This way, nobody can modify or change your variables!&lt;code&gt;extract($REQUEST, EXTR_PREFIX_ALL, &quot;my_prefix&quot;);&lt;/code&gt;</description> <content:encoded><![CDATA[<p>As Hugo Magalhães said, I think this woud be a &#8220;better&#8221; solution&#8230;<br
/> This way, nobody can modify or change your variables!</p><p><code>extract($REQUEST, EXTR_PREFIX_ALL, "my_prefix");</code></p> ]]></content:encoded> </item> <item><title>By: droope</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-16776</link> <dc:creator>droope</dc:creator> <pubDate>Mon, 05 Apr 2010 20:02:42 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-16776</guid> <description>(great blog, btw)</description> <content:encoded><![CDATA[<p>(great blog, btw)</p> ]]></content:encoded> </item> <item><title>By: Robert</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-16703</link> <dc:creator>Robert</dc:creator> <pubDate>Tue, 30 Mar 2010 17:18:34 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-16703</guid> <description>No, Droope is correct. Mostly.The ${ } does designate a string.But the purpose of this article is showing how to easily assign array variables to their key name. I know that sounds confusing, but its it.Example:$people[&#039;me&#039;] = &#039;robert&#039;;
$people[&#039;friend&#039;] = &#039;sam&#039;;
$people[&#039;girl&#039;] = &#039;bailey&#039;;foreach($people as $key =&gt; $val) {
// ${&quot;me&quot;} = &#039;robert&#039;${$key} = $val; //or $$key = $val;
}So thatecho $me; //echo &quot;robert&quot;;</description> <content:encoded><![CDATA[<p>No, Droope is correct. Mostly.</p><p>The ${ } does designate a string.</p><p>But the purpose of this article is showing how to easily assign array variables to their key name. I know that sounds confusing, but its it.</p><p>Example:</p><p>$people['me'] = &#8216;robert&#8217;;<br
/> $people['friend'] = &#8216;sam&#8217;;<br
/> $people['girl'] = &#8216;bailey&#8217;;</p><p>foreach($people as $key =&gt; $val) {<br
/> // ${&#8220;me&#8221;} = &#8216;robert&#8217;</p><p> ${$key} = $val; //or $$key = $val;<br
/> }</p><p>So that</p><p>echo $me; //echo &#8220;robert&#8221;;</p> ]]></content:encoded> </item> <item><title>By: Droope</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-16342</link> <dc:creator>Droope</dc:creator> <pubDate>Fri, 05 Mar 2010 02:37:59 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-16342</guid> <description>Hi David,I think the brackets are just a way of delimiting the variable&#039;s name from the rest of the string.Say you had variable $table_prefix,echo &quot;table name: $table_prefix_users&quot;; // would return &quot;table name: &quot;;
echo &quot;table name: ${table_prefix}_users&quot;; // would return &quot;table name: wp_users&quot;;I am not sure about this :) I am most likely wrong.Cheers!
Droope</description> <content:encoded><![CDATA[<p>Hi David,</p><p>I think the brackets are just a way of delimiting the variable&#8217;s name from the rest of the string.</p><p>Say you had variable $table_prefix,</p><p>echo &#8220;table name: $table_prefix_users&#8221;; // would return &#8220;table name: &#8220;;<br
/> echo &#8220;table name: ${table_prefix}_users&#8221;; // would return &#8220;table name: wp_users&#8221;;</p><p>I am not sure about this :) I am most likely wrong.</p><p>Cheers!<br
/> Droope</p> ]]></content:encoded> </item> <item><title>By: David Moreen</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-16281</link> <dc:creator>David Moreen</dc:creator> <pubDate>Sun, 28 Feb 2010 03:55:29 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-16281</guid> <description>Hey is not on google so I was wondering what the  ${ } does? I have never seen it before.</description> <content:encoded><![CDATA[<p>Hey is not on google so I was wondering what the  ${ } does? I have never seen it before.</p> ]]></content:encoded> </item> <item><title>By: Peter Scarborough</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-244</link> <dc:creator>Peter Scarborough</dc:creator> <pubDate>Fri, 14 Dec 2007 21:56:20 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-244</guid> <description>Sorry, there was an error in my regex syntax there, I learned regex with javascript, and just learning PHP regEx... I got the two confused when I wrote thatThe correct syntax for the above would be
$acceptedGETS = &quot;/\bvarname1\b&#124;\bvarname2\b&#124;\bvarname3\b&#124;\betc\b/&quot;;
foreach($_GET as $key =&gt; $value) {
$key = strtolower($key);
if (preg_match($acceptedGETS,$key)) $$key = $value;
}the \b is required on both sides of each variable name to force exact words, without them qvarname1x34r would match.</description> <content:encoded><![CDATA[<p>Sorry, there was an error in my regex syntax there, I learned regex with javascript, and just learning PHP regEx&#8230; I got the two confused when I wrote that</p><p>The correct syntax for the above would be<br
/> $acceptedGETS = &#8220;/\bvarname1\b|\bvarname2\b|\bvarname3\b|\betc\b/&#8221;;<br
/> foreach($_GET as $key =&gt; $value) {<br
/> $key = strtolower($key);<br
/> if (preg_match($acceptedGETS,$key)) $$key = $value;<br
/> }</p><p>the \b is required on both sides of each variable name to force exact words, without them qvarname1x34r would match.</p> ]]></content:encoded> </item> <item><title>By: david</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-243</link> <dc:creator>david</dc:creator> <pubDate>Fri, 14 Dec 2007 19:45:59 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-243</guid> <description>Thank you for the RegEx, Peter -- that could prove very useful!</description> <content:encoded><![CDATA[<p>Thank you for the RegEx, Peter &#8212; that could prove very useful!</p> ]]></content:encoded> </item> <item><title>By: Peter Scarborough</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-242</link> <dc:creator>Peter Scarborough</dc:creator> <pubDate>Fri, 14 Dec 2007 19:37:39 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-242</guid> <description>Also, per using the method for GET POST or REQUEST you could use a regex match on the $k to only convert expected variable names.
IE:
$acceptedGETS = /(varname1)&#124;(varname2)&#124;(varname3)&#124;(etc)/
foreach($_GET as $key =&gt; $value) {
$key = strtolower($key);
if $key.match($acceptedGETS) $$key = $value;
}This will prevent someone from creating their own variable names</description> <content:encoded><![CDATA[<p>Also, per using the method for GET POST or REQUEST you could use a regex match on the $k to only convert expected variable names.<br
/> IE:<br
/> $acceptedGETS = /(varname1)|(varname2)|(varname3)|(etc)/<br
/> foreach($_GET as $key =&gt; $value) {<br
/> $key = strtolower($key);<br
/> if $key.match($acceptedGETS) $$key = $value;<br
/> }</p><p>This will prevent someone from creating their own variable names</p> ]]></content:encoded> </item> <item><title>By: Peter Scarborough</title><link>http://davidwalsh.name/convert-key-value-arrays-standard-variables-php#comment-241</link> <dc:creator>Peter Scarborough</dc:creator> <pubDate>Fri, 14 Dec 2007 18:31:40 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/convert-key-value-arrays-standard-variables-php/#comment-241</guid> <description>I have used this alot for both GET&#039;s and POST&#039;s, sure, they can create their own variables, who cares? If I use a variable, I always declare it after pulling from get or post, the variables that I use from post and get are also validated for correct values.Also, something I have found handy for dealing with case sensitivity for get variables, (since sometimes if someone posts it to say, ICQ for someone else to visit, ICQ will lowercase the whole string) is
foreach($_GET as $k = $v) {
$k = strtolower($k);
\$$k = $v
}
then you can safely test to see if the get was set
if(isset($variablenametotestfor)) {//begin execution}</description> <content:encoded><![CDATA[<p>I have used this alot for both GET&#8217;s and POST&#8217;s, sure, they can create their own variables, who cares? If I use a variable, I always declare it after pulling from get or post, the variables that I use from post and get are also validated for correct values.</p><p>Also, something I have found handy for dealing with case sensitivity for get variables, (since sometimes if someone posts it to say, ICQ for someone else to visit, ICQ will lowercase the whole string) is<br
/> foreach($_GET as $k = $v) {<br
/> $k = strtolower($k);<br
/> \$$k = $v<br
/> }<br
/> then you can safely test to see if the get was set<br
/> if(isset($variablenametotestfor)) {//begin execution}</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 3/7 queries in 0.007 seconds using disk: basic
Object Caching 805/805 objects using disk: basic

Served from: davidwalsh.name @ 2012-05-22 03:05:25 -->
