[KLUG Programming] PHP arrays vs. structures
Adam Tauno WIlliams
adam at morrison-ind.com
Mon Jul 19 13:46:47 EDT 2004
> Yes, I was wondering if you were working with XML-RPC calls.
> I do have a single question below
> > > > 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
> ===?
It is a good question. Documentation did not make it clear if key(...)
returns a type:mixed or a type:string. Internally I think how PHP4
handles types is a bit loosey-goosey; nice for somethings but
jaw-grinding for others. The examples (the ones I glanced at anyway)
seemed inconsistent among themselves.
http://us2.php.net/key says key(....) returns mixed, so you're probably
correct, that the ticks/quotes are inappropriate.
> 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.
Correct, I'm relying on foreach(...) to exhibit sane behaviour so that I
don't walk of the end of the array (which PHP permits), at which point
I'd assume key(...) would return 0, or false ???? I've never had any
problem with foreach(...)
> > //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.
More information about the Programming
mailing list