[KLUG Programming] PHP arrays vs. structures
Adam Tauno Williams
awilliam at whitemice.org
Mon Jul 19 12:50:54 EDT 2004
> > 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') {
//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