473,569 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ 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($a rray) {
if(is_array($ar ray)) {
foreach($array as $arrayValue) {
recurseArray($a rrayValue);
}
}
elseif(is_objec t($array)) {
$object = $array;
return $object;
}
}

the problem with that is that when i try to do
$_product = recurseArray($p roductArray)
print_r($_produ ct);

it won't pring anything. But if i do the pring inside the method like
that
....
elseif(is_objec t($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 1904
Hi,

Please try: return recurseArray($a rrayValue);

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($a rray) {
if(is_array($ar ray)) {
foreach($array as $arrayValue) {
recurseArray($a rrayValue);
}
}
elseif(is_objec t($array)) {
$object = $array;
return $object;
}

}

the problem with that is that when i try to do
$_product = recurseArray($p roductArray)
print_r($_produ ct);

it won't pring anything. But if i do the pring inside the method like
that
...
elseif(is_objec t($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($a rray) {
if(is_array($ar ray)) {
foreach($array as $arrayValue) {
recurseArray($a rrayValue);
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_ar ray($var){
$return = array();
if(is_object($v ar)){
$return[] = $var;
}
if(is_array($va r)){
$return = array_merge($re turn,objects_fr om_array($var)) ;
}
return $return;
}
--
Rik Wasmus
Feb 7 '07 #3
On Feb 7, 2:53 pm, "petersprc" <peters...@gmai l.comwrote:
Hi,

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

Feb 7 '07 #4
function objects_from_ar ray($var){
$return = array();
if(is_object($v ar)){
$return[] = $var;
}
if(is_array($va r)){
$return = array_merge($re turn,objects_fr om_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_ar ray($var){
$return = array();
if(is_object($v ar)){
$return[] = $var;
}
if(is_array($va r)){
$return = array_merge($re turn,objects_fr om_array($var)) ;
Oops,
if(is_array($va r){
foreach($var as $item){
$return = array_merge($re turn, objects_from_ar ray($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_ar ray($var){
$return = array();
if(is_object($v ar)){
$return = $var;
}
if(is_array($va r)){
foreach($var as $item){
$return = array_merge($re turn, objects_from_ar ray($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_ar ray($var){
$return = array();
if(is_object($v ar)){
$return = $var;
}
if(is_array($va r)){
foreach($var as $item){
$return = array_merge($re turn, objects_from_ar ray($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($a rray) {
if(is_array($ar ray)) {
foreach($array as $arrayValue) {
recurseArray($a rrayValue);
-----------------------^^

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($a rrayValue), 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.comwro te:
Aggelos <djje...@gmail. comwrote:
function objects_from_ar ray($var){
$return = array();
if(is_object($v ar)){
$return = $var;
}
if(is_array($va r)){
foreach($var as $item){
$return = array_merge($re turn, objects_from_ar ray($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($a rray) {
if(is_array($ar ray)) {
foreach($array as $arrayValue) {
recurseArray($a rrayValue);
-----------------------^^

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($a rrayValue), 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...@gmai l.comwrote:
>Hi,

Please try: return recurseArray($a rrayValue);

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*******@attgl obal.net
=============== ===
Feb 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5016
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' => '10.10.10.10/32', },
1
2201
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 validates. ============== BASE XMLSCHEMA ================= <?xml version="1.0"?> <xs:schema targetNamespace="test" xmlns="test"...
8
10206
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 it is assigned a text literal?? HOW can I change the array value to upper case then? What other method exists for arrays? Ex: var GridArrayName1...
1
1467
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: Parent Child Data 10 15 123 10 16 456 20 25 789 20 28 246
2
2259
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 recurses too deep are there other reasons for this that I should know about? This example returns the position of the leftmost element of a specific...
6
1346
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
1097
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 values, then I need to get another object, created somewhere else, for example by serialization method, then to assign all values from this external...
4
3193
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 search a parent element(in my case a <table>) and search for elements of a certain type (<input>, <textarea>, etc) and perform operations on them. ...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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 we have to send another system

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.