[KLUG Programming] PHP arrays vs. structures
bill
bill at billtron.com
Mon Jul 19 13:28:23 EDT 2004
Hi Adam,
Yes, I was wondering if you were working with XML-RPC calls.
I do have a single question below.
kind regards,
bill
On Mon, 2004-07-19 at 12:50, Adam Tauno Williams wrote:
> > > This solves my problems
> > A common PHP phrase
> > > since the structures I have to deal with never
> > > use numeric keys, so if the first key is 0 then it is an array,
> > > otherwise it must be a structure (keys are strings). So I'm just lucky
> > > (for once!) in this case.
> > It's implicit, which means that unless the keys are explicitly set as
> > strings, you can refer to them numerically. That tidbit can be
> > extremely helpful (because so much code doesn't explicitly set the
> > key). In your case the check code you were looking for is not even
> > necessary.
>
> But other languages (C, etc...) as well as documents (XML, etc...) do
> clearly distinguish between an array and a structure. Using the numeric
> key = array hack I've constructed...
>
>
> function wrap($value) {
> $this->traceMessage('@OGoDocument:wrap');
> switch(gettype($value)) {
> case 'boolean':
> $this->traceMessage('@OGoDocument:wrap.boolean');
> $value = new XML_RPC_value($value, 'boolean');
> break;
> case 'integer':
> $this->traceMessage('@OGoDocument:wrap.integer');
> $value = new XML_RPC_value($value, 'int');
> break;
> case 'double':
> $this->traceMessage('@OGoDocument:wrap.double');
> $value = new XML_RPC_value($value, 'double');
> break;
> case 'string':
> $this->traceMessage('@OGoDocument:wrap.string');
> $value = new XML_RPC_value($value, 'string');
> break;
> case 'array':
> $this->traceMessage('@OGoDocument:wrap.array');
> $a = array();
> reset($value);
> if (key($value) == '0') {
Isn't this a string comparison as you have 0 in single quotes? If
you're looking for integers, why not drop the quotes or consider using
===?
I don't know for sure that it will ever cause problems the way you're
doing it, just a note on the type of comparison it will do. I'm always
leery of comparing anything to zero because it reminds me of how I've
seem people look for results of functions (because return false can
evaluate to zero). If a function returns zero it is usually not the
same as returning false.
> //array
> $this->traceMessage('@OGoDocument:wrap.array.array');
> $a_type = 'array';
> foreach ($value as $v)
> array_push($a, $this->wrap($v));
> } else {
> //struct
> $this->traceMessage('@OGoDocument:wrap.struct');
> $a_type = 'struct';
> foreach ($value as $k => $v)
> $a[$k] = $this->wrap($v);
> }
> $value = new XML_RPC_value($a, $a_type);
> break;
> case 'object':
> $this->traceMessage('@OGoDocument:wrap.object');
> /// NOT SUPPORTED
> break;
> case 'resource':
> $this->traceMessage('@OGoDocument:wrap.resource');
> /// NOT SUPPORTED
> break;
> case 'NULL':
> $this->traceMessage('@OGoDocument:wrap.NULL');
> /// NOT SUPPORTED
> break;
> }
> return $value;
> }
>
> .... which seems to be able to turn PHP structures into documents that
> can be transported to a web service.
Excellent.
b.
More information about the Programming
mailing list