473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Recurse an array with an object/s

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
15 1887
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christopher | last post by:
I have a hash that is several levels deep. ie: 'vars' => { '$filers' => '10.10.10.10/32', '$networksa' => '10.10.10.10/32', '$networksb' => '10.50.0.0/16', '$wintel_boxes' =>...
1
by: Cat | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm getting a validation error when I try to restrict the content of nested groups with xs:redefine whereas the same restriction on xs:element's...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
1
by: Mr. B | last post by:
I've been beating my brains out on trying to figure out how to do Recurse (Parent/Child) code for my application (VB.net). Basically a User enters data into an array. The data is pretty simple:...
2
by: J. J. Cale | last post by:
In IE6 both these functions *seem* to be working. I don't see much recursion in this group except the occasional setTimeout problems. Besides the obvious stack problems that can occur if one...
6
by: Laszlo Szijarto | last post by:
What's the best way to recurse through the members of an enum? I want to take an enum and dump into a ListBox the enum's names. Thank you, Laszlo
2
by: Dmitri Shvetsov | last post by:
Hi, Does anybody know if I can use something like recurse to get the whole list of the public members of the class and their types/values? For example, I created the object with default...
4
by: SteveKlett | last post by:
I have a subset of form items that I need to perform different operations on (enable/disable, clear values, change style, etc) rather than hard code the IDs or names I would like to recursively...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.