Progressively simplifying the method of the previous post.
The first part of the simplification came when it was
realised that everything being passed to it was being
checked twice to see if it was an array or an object - once
before the function is called, and once after the function
had begin.
The latter pair of tests are retained so that they do not
need to be made explicitly in other part of the program
before calling the function in the first place.
<?php
function conv_obj($Data){
if(is_object($Data)){
foreach(get_object_vars($Data) as $key=>$val){
$ret[$key]=conv_obj($val);
}
return $ret;
}elseif(is_array($Data)){
foreach($Data as $key=>$val){
$ret[$key]=conv_obj($val);
}
return $ret;
}else{
return $Data;
}
}
?>
Reversing the tests to get the simplest case out of the way
first.
<?php
function conv_obj($Data){
if(!is_object($Data) && !is_array($Data)) return
$Data;
if(is_object($Data)){
foreach(get_object_vars($Data) as $key=>$val){
$ret[$key]=conv_obj($val);
}
return $ret;
}else{
foreach($Data as $key=>$val){
$ret[$key]=conv_obj($val);
}
return $ret;
}
}
?>
The only difference between the two loops is in what they
are iterating over.
<?php
function conv_obj($Data){
if(!is_object($Data) && !is_array($Data)) return
$Data;
if(is_object($Data)){
$vars = get_object_vars($Data);
foreach($vars as $key=>$val){
$ret[$key]=conv_obj($val);
}
return $ret;
}else{
$vars = $Data;
foreach($vars as $key=>$val){
$ret[$key]=conv_obj($val);
}
return $ret;
}
}
?>
Moving the common tail of the if statement's two branches:
<?php
function conv_obj($Data){
if(!is_object($Data) && !is_array($Data)) return
$Data;
if(is_object($Data)){
$vars = get_object_vars($Data);
}else{
$vars = $Data;
}
foreach($vars as $key=>$val){
$ret[$key]=conv_obj($val);
}
return $ret;
}
?>
A lot of the time $vars===$Data, and when it doesn't we
don't need $Data any more anyway, so we re-use the $Data
variable instead of bringing in a new $vars variable, and
then we can drop the do-nothing else branch:
<?php
function conv_obj($Data){
if(!is_object($Data) && !is_array($Data)) return
$Data;
if(is_object($Data)) $Data = get_object_vars($Data);
foreach($Data as $key=>$val){
$ret[$key]=conv_obj($val);
}
return $ret;
}
?>
A case for using array_map():
<?php
function conv_obj($Data){
if(!is_object($Data) && !is_array($Data)) return
$Data;
if(is_object($Data)) $Data = get_object_vars($Data);
$ret = array_map('conv_obj', $Data);
return $ret;
}
?>
And finally the return variable is otherwise unused:
<?php
function conv_obj($Data){
if(!is_object($Data) && !is_array($Data)) return
$Data;
if(is_object($Data)) $Data = get_object_vars($Data);
return array_map('conv_obj', $Data);
}
?>
----
Server IP: 203.89.181.186
Probable Submitter: 60.234.168.92
----
Manual Page -- http://www.php.net/manual/en/function.get-object-vars.p
hp
Edit -- https://master
.php.net/note/edit/78346
Del: integrated -- h
ttps://master.php.net/note/delete/78346/integrated
Del: useless -- http
s://master.php.net/note/delete/78346/useless
Del: bad code -- htt
ps://master.php.net/note/delete/78346/bad+code
Del: spam -- https:/
/master.php.net/note/delete/78346/spam
Del: non-english --
https://master.php.net/note/delete/78346/non-english
Del: in docs -- http
s://master.php.net/note/delete/78346/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78346
Reject -- https://mast
er.php.net/note/reject/78346
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|