By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,828 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,828 IT Pros & Developers. It's quick & easy.

Recurse an array with an object/s

P: n/a
Hello,
I am using php4
And I have a class product
I create an array of productObjects that has 2 dimensions.
Generraly I am sorting my objects inside arrays and I need to some how
recurse those arrays to get to the objects.

To do that I am using the following method recurseArray()

function recurseArray($array) {
if(is_array($array)) {
foreach($array as $arrayValue) {
recurseArray($arrayValue);
}
}
elseif(is_object($array)) {
$object = $array;
return $object;
}
}

the problem with that is that when i try to do
$_product = recurseArray($productArray)
print_r($_product);

it won't pring anything. But if i do the pring inside the method like
that
....
elseif(is_object($array)) {
$object = $array;
print_r($object)
return $object;
}
....
it will print my object.

Do you know why is this happening ?
It makes me crazy !!!
Thanks, Angelos.

Feb 7 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Hi,

Please try: return recurseArray($arrayValue);

Should do the trick.

On Feb 7, 9:49 am, "Aggelos" <djje...@gmail.comwrote:
Hello,
I am using php4
And I have a class product
I create an array of productObjects that has 2 dimensions.
Generraly I am sorting my objects inside arrays and I need to some how
recurse those arrays to get to the objects.

To do that I am using the following method recurseArray()

function recurseArray($array) {
if(is_array($array)) {
foreach($array as $arrayValue) {
recurseArray($arrayValue);
}
}
elseif(is_object($array)) {
$object = $array;
return $object;
}

}

the problem with that is that when i try to do
$_product = recurseArray($productArray)
print_r($_product);

it won't pring anything. But if i do the pring inside the method like
that
...
elseif(is_object($array)) {
$object = $array;
print_r($object)
return $object;
}
...
it will print my object.

Do you know why is this happening ?
It makes me crazy !!!
Thanks, Angelos.

Feb 7 '07 #2

P: n/a
Rik
Aggelos <dj*****@gmail.comwrote:
Hello,
I am using php4
And I have a class product
I create an array of productObjects that has 2 dimensions.
Generraly I am sorting my objects inside arrays and I need to some how
recurse those arrays to get to the objects.

To do that I am using the following method recurseArray()

function recurseArray($array) {
if(is_array($array)) {
foreach($array as $arrayValue) {
recurseArray($arrayValue);
This won't do anything, you returned objects are cast in the void....

How would you like your objects returned by the function? In case of a
flat array (which I suspect):
function objects_from_array($var){
$return = array();
if(is_object($var)){
$return[] = $var;
}
if(is_array($var)){
$return = array_merge($return,objects_from_array($var));
}
return $return;
}
--
Rik Wasmus
Feb 7 '07 #3

P: n/a
On Feb 7, 2:53 pm, "petersprc" <peters...@gmail.comwrote:
Hi,

Please try: return recurseArray($arrayValue);
That would just loop forever... won't it ?

Feb 7 '07 #4

P: n/a
function objects_from_array($var){
$return = array();
if(is_object($var)){
$return[] = $var;
}
if(is_array($var)){
$return = array_merge($return,objects_from_array($var));
}
return $return;}
That function will also enter an endless loop like petesprc
We need to loop inside the array to get the next dimension... don't
we ? so when you do arraymerge the $var should be the second dimension
of the array in the first call of the function.

Isn't that right ?
So I suppose I need to have a foreach loop

my array is like that
$productArray[$productTypeId][$productId][$published] = new
objectProduct();

so for that i would do 3 nested foreach loops ...
But I want to do that using a function to get to the objectProduct.

Feb 7 '07 #5

P: n/a
Rik
Aggelos <dj*****@gmail.comwrote:
>function objects_from_array($var){
$return = array();
if(is_object($var)){
$return[] = $var;
}
if(is_array($var)){
$return = array_merge($return,objects_from_array($var));
Oops,
if(is_array($var){
foreach($var as $item){
$return = array_merge($return, objects_from_array($item));
}
}
> }
return $return;}
That function will also enter an endless loop like petesprc
No, it won't like this :P. It will loop through an array and if it's done,
it will return all objects found. Unless you're doing something very
strange like putting in a reference to a parent array in a sub array.

We need to loop inside the array to get the next dimension... don't
we ? so when you do arraymerge the $var should be the second dimension
of the array in the first call of the function.
my array is like that
$productArray[$productTypeId][$productId][$published] = new
objectProduct();

so for that i would do 3 nested foreach loops ...
Or just a recursive function.

--
Rik Wasmus
Feb 7 '07 #6

P: n/a
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.
So my product->title now would become product[;title']

Do you know why my version isn't working ? you said something about
void... but i didn't understood that...

Feb 7 '07 #7

P: n/a
Rik
Aggelos <dj*****@gmail.comwrote:
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
Feb 7 '07 #8

P: n/a
On Feb 7, 4:25 pm, Rik <luiheidsgoe...@hotmail.comwrote:
Aggelos <djje...@gmail.comwrote:
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
Comprende, I understand now :)
Thanks a lot.

Feb 7 '07 #9

P: n/a
Aggelos wrote:
On Feb 7, 2:53 pm, "petersprc" <peters...@gmail.comwrote:
>Hi,

Please try: return recurseArray($arrayValue);

That would just loop forever... won't it ?
Not unless you have a loop in your array itself.

Your problem is you're only returning something if the last item in the
first level of your array is an object. Otherwise you're not returning
anything - so print_r has nothing to print.

But one question - you say you have a two dimensional array. But this
is only a single dimensional array. It just happens that an array
element might itself be an array. That's not two dimensions.

Why not try posting some sample data and what you're trying to get out
of it? Then maybe we could help you a little better.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 7 '07 #10

P: n/a
On Feb 7, 4:25 pm, Rik <luiheidsgoe...@hotmail.comwrote:
Aggelos <djje...@gmail.comwrote:
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.

Feb 7 '07 #11

P: n/a
Rik
Aggelos <dj*****@gmail.comwrote:
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;
}

This will only return the _last_ object found, as you continue to
overwrite $tempArray for every array-element.

--
Rik Wasmus
Feb 7 '07 #12

P: n/a
But one question - you say you have a two dimensional array. But this
is only a single dimensional array. It just happens that an array
element might itself be an array. That's not two dimensions.
ok I'll explain to you and tell me if I am wrong.

I have class package and class product

A package has many products when I call my constructor for the package
I create an array of it's products as well.

CODE:

class package {
var $packageId;
var $title;
var $description;

var $_productArray = array();

function package($packageId=NULL,$title=NULL,$description=N ULL) {
$this->packageId = $packageId;
$this->title = $title;
$this->description = $description;

$productPackageArray = product_package::select(array('package_id'=>
$packageId));
foreach($productPackageArray[$packageId] as $contentId =>
$publishedArray) {
foreach($publishedArray as $published =$productValue) {
$_ObjProduct = product::select(array('content_id'=>
$contentId,'published'=>$published));
$this->_productArray[$_product[$contentId][$published]-
>productTypeId] = $_ObjProduct;
}
}
}
.......
}

END OF CODE

I am not sure if you'll understand my code but anyway I end up
creating product packages and having an object package which instead
of having just the ids of the products has the whole object in it.

and that creates an array like that $package[$packageId]-
>_productArray[$productTypeId] = $_ObjProduct; (I don't know how to
symbolize the object inside the array)

So how many dimensions is that and how does it look to you Jerry. ?
Feb 7 '07 #13

P: n/a
Aggelos wrote:
>But one question - you say you have a two dimensional array. But this
is only a single dimensional array. It just happens that an array
element might itself be an array. That's not two dimensions.

ok I'll explain to you and tell me if I am wrong.

I have class package and class product

A package has many products when I call my constructor for the package
I create an array of it's products as well.

CODE:

class package {
var $packageId;
var $title;
var $description;

var $_productArray = array();

function package($packageId=NULL,$title=NULL,$description=N ULL) {
$this->packageId = $packageId;
$this->title = $title;
$this->description = $description;

$productPackageArray = product_package::select(array('package_id'=>
$packageId));
foreach($productPackageArray[$packageId] as $contentId =>
$publishedArray) {
foreach($publishedArray as $published =$productValue) {
$_ObjProduct = product::select(array('content_id'=>
$contentId,'published'=>$published));
$this->_productArray[$_product[$contentId][$published]-
>productTypeId] = $_ObjProduct;
}
}
}
......
}

END OF CODE

I am not sure if you'll understand my code but anyway I end up
creating product packages and having an object package which instead
of having just the ids of the products has the whole object in it.

and that creates an array like that $package[$packageId]-
>_productArray[$productTypeId] = $_ObjProduct; (I don't know how to
symbolize the object inside the array)

So how many dimensions is that and how does it look to you Jerry. ?

Aggelos,

This is still a single-dimensional array. Two dimensional arrays are
access by two subscripts, i.e. $myarray[1][2].

And yes, I understand your code. But again - what does the *data* look
like, and what do you expect for output?

Try populating your array and print it with print_r(). Post it along
with what you would like the output to look like.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 7 '07 #14

P: n/a
Jerry Stuckle wrote:
Aggelos wrote:
>>But one question - you say you have a two dimensional array. But this
is only a single dimensional array. It just happens that an array
element might itself be an array. That's not two dimensions.

ok I'll explain to you and tell me if I am wrong.

I have class package and class product

A package has many products when I call my constructor for the package
I create an array of it's products as well.

CODE:

class package {
var $packageId;
var $title;
var $description;

var $_productArray = array();

function package($packageId=NULL,$title=NULL,$description=N ULL) {
$this->packageId = $packageId;
$this->title = $title;
$this->description = $description;

$productPackageArray =
product_package::select(array('package_id'=>
$packageId));
foreach($productPackageArray[$packageId] as $contentId =>
$publishedArray) {
foreach($publishedArray as $published =$productValue) {
$_ObjProduct = product::select(array('content_id'=>
$contentId,'published'=>$published));
$this->_productArray[$_product[$contentId][$published]-
>>productTypeId] = $_ObjProduct;
}
}
}
......
}

END OF CODE

I am not sure if you'll understand my code but anyway I end up
creating product packages and having an object package which instead
of having just the ids of the products has the whole object in it.

and that creates an array like that $package[$packageId]-
>>_productArray[$productTypeId] = $_ObjProduct; (I don't know how to
symbolize the object inside the array)

So how many dimensions is that and how does it look to you Jerry. ?


Aggelos,

This is still a single-dimensional array. Two dimensional arrays are
access by two subscripts, i.e. $myarray[1][2].

And yes, I understand your code. But again - what does the *data* look
like, and what do you expect for output?

Try populating your array and print it with print_r(). Post it along
with what you would like the output to look like.

Actually, allow me to correct one thing. This is an N-dimensional array
(depending on the depth). But you are handling it as a single
dimensional array in your recursive function. This is perfectly fine,
and I do similar things quite regularly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 7 '07 #15

P: n/a
Actually, allow me to correct one thing. This is an N-dimensional array
(depending on the depth). But you are handling it as a single
dimensional array in your recursive function. This is perfectly fine,
and I do similar things quite regularly.
I happy to hear that. It is good to listen that I haven't used a
technique that is so awkward to be used from otheres.

At the moment what I did with my ricursive function works perfect.
The question is if that's the right way to do it...

And that is an object haveing as an attribute another object and that
object laso have another object as an attribute:
oject -object -object

or is it better to keep them sepperate and just use their unique id's
to link the two objects (thats what I am doing until now)

So again:

1st way *******************************
*****************************************
class package {
var $packageId;
var $productArray = array();

function package($packageId=NULL) {
$this->packageId = $packageId;
$this->productArray = product::select(....);
}
}
class product {
var $productId;
var $attributesArray = array();
function product($productId=NULL) {
$this->productId = $productId;
$this->attributesArray = attribute::select(....);
}
}
2nd way *******************************
*****************************************
class package {
var $packageId;
function package($packageId=NULL) {
$this->packageId = $packageId;
}
}
class product {
var $productId;
function product($productId=NULL) {
$this->productId = $productId;
}
}

$product_packages = select the products from product_package table
//$product_packages = array('pacakge_id','product_id') <- just to have
an idea of how that array looks like
foreach($product_packages as $packageId =$productArray) {
foreach($productArray as $productId =$productValue) {
$product[$productId] = new product($productId);
}
}

Anyway... that's not like the full code but you could get an Idea of
how I am working...
I hope it makes sense.
Can you just tell me which way could be better ? more efficient
probably ...

At the moment for every Many to Many relationship I am using a
seperate class e.g. product_package

Thanks
Angelos

Feb 8 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.