|
Posted by Aggelos on 02/07/07 16:34
On Feb 7, 4:25 pm, Rik <luiheidsgoe...@hotmail.com> wrote:
> Aggelos <djje...@gmail.com> wrote:
> > function objects_from_array($var){
> > $return = array();
> > if(is_object($var)){
> > $return = $var;
> > }
> > if(is_array($var)){
> > foreach($var as $item){
> > $return = array_merge($return, objects_from_array($item));
>
> > }
> > }
> > return $return;
> > }
>
> > Ok that will work now but it is not what I want because at the end my
> > object gets converted into an assiciative array.
>
> Not in my code, I assume in something you want to do afterwards? I told
> you I did not know in which format you want the return.
>
> > So my product->title now would become product[;title']
>
> Euh? That's something entirely different. Please explain your exact
> intentions from start to finish, would make it a lot easier to give you an
> answer you can actually use :-)
>
> > Do you know why my version isn't working ? you said something about
> > void... but i didn't understood that...
>
> This part:
> function recurseArray($array) {
> if(is_array($array)) {
> foreach($array as $arrayValue) {
> recurseArray($arrayValue);
> -----------------------^^
>
> If the $array you feed it is an object, the same object is immediately
> returned. So far so good. If it is an array, you perform the function on
> each item of the array. This may or may not return an object. However,
> there's nothing 'to the left' of this recurseArray($arrayValue), which
> means that whatever it returns, it is not assigned to anything, so
> whatever it returns is lost. If you feed it an array, the function is
> performed on every item, but isn't 'saved'. The only 'return' there is for
> when it's an object, so if it's not an object your function will return
> nothing/null/nada.
> --
> Rik Wasmus
This is my suggested sollution:
It succesfully returns the object ... ofcourse you have to initialise
the tempArray but I wasn't very sure about it ...
function recurseArray($array) {
if(is_array($array)) {
foreach($array as $arrayValue) {
$tempArray = recurseArray($arrayValue);
}
}
elseif(is_object($array)) {
return $array;
}
return $tempArray;
}
Thanks a lot for your replies.
Navigation:
[Reply to this message]
|