Re: $_GET strings seperation

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

 



On May 26, 5:39 pm, n...@xxxxxxxxxxxxxxxxxx ("Navid Yar") wrote:
Thanks so much Jarred. It helps me learn more when there's an
explaination on how the code works. I'll play around with it, change
it a bit and give that a try. Take care...
P.S. -- I'm in Arlington, TX

I work with a guy from Arlington. Live near the new stadium?
Incidentally, ponder this:

<code>
function shortGetNewQueryString($arr,$merge) {
   return array_merge($arr,$merge);
}
echo('<pre>');

// Let's do one new cID, new GET key/value
$query = Array('cID'=>42,'freudian'=>'slip');
$go = shortGetNewQueryString($_GET,$query);
print_r($go);

// Let's do one new cID, new GET key/value
$query = Array('cID'=>9-002,'footloose'=>'fancy free');
$go = shortGetNewQueryString($go,$query);
print_r($go);

// Let's do one new cID, new GET key/value
$query = Array('cID'=>493,'fugged'=>'dhaboutit');
$go = shortGetNewQueryString($go,$query);
print_r($go);

// Let's do one new cID, new GET key/value
$query = Array('cID'=>A4,'longlongtimeago'=>'in a galaxy far, far
away');
$go = shortGetNewQueryString($go,$query);
print_r($go);

echo('</pre>');
</code>

By the way, when you run that code, pay special attention to the
second test. Very very tricky entry anomaly... Wuffuh!

Pay attention to how short that new code is
( shortGetNewQueryString() ). It's certainly arguable you don't even
need to wrap it in a function. Consider:

<code>
// This is the best version, I believe: brief and simple.
function mediumGetNewQueryString ($arr,$add) {
   foreach ($add as $key=>$val) {
       $arr[$key] = $val;
   }
   return $arr;
}

echo('<pre>');
print_r( mediumGetNewQueryString($_GET,$query) );
echo('<pre>');
</code>

And then, of course, a number of shortcuts may be used to obscurify and
mystify your code for later puzzling, head-scratchedness.

This is, of course, exactly comparable to all the other example
methods:

<code>
// Hard to read, ie, needless brevity
function annoyingGetNewQueryString ($arr,$add) {
   foreach ($add as $key=>$val) $arr[$key] = $val;
   return $arr;
}

echo('<pre>');
print_r( annoyingGetNewQueryString($_GET, $query) );
echo('</pre>');
</code>

Caution: Using array_merge, though, will overwrite keynames, but NOT
numerical items. You can't auto-map over numerical keys with array_merge(),
apparently.

Consider:

<code>
$array = Array(
   [0] => 'moe'
   [1] => 'curly',
   [2] => 'larry'
);

// Is equivalent to ~
$array = Array();
$array[] 'moe';
$array[] 'curly';
$array[] 'larry';

// Is equivalent to ~
$array = Array();
array_push($array, 'moe');
array_push($array, 'curly');
array_push($array, 'larry');
</code>

When you add a numerical array in php, it is added to the stack as a new
item, or push. Essentially,

$array = Array('item1')
$array[] = 'item2' eq ~ "Array('item1','item2')"

And then when you call on the array, it

{ get Array as Numerically-Indexed Set } eq ~ split($array,$token=',') eq ~
({ [0] => 'item' , [1] = 'item2' })

So an array on a stack can be represeted in memory as a comma-delimited
numerically-indexed list, eg, 'item','item2'

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

[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