473,699 Members | 2,834 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
15 1929
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
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($a rray) {
if(is_array($ar ray)) {
foreach($array as $arrayValue) {
$tempArray = recurseArray($a rrayValue);
}
}
elseif(is_objec t($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($a rray) {
if(is_array($ar ray)) {
foreach($array as $arrayValue) {
$tempArray = recurseArray($a rrayValue);
}
}
elseif(is_objec t($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($packag eId=NULL,$title =NULL,$descript ion=NULL) {
$this->packageId = $packageId;
$this->title = $title;
$this->description = $description;

$productPackage Array = product_package ::select(array( 'package_id'=>
$packageId));
foreach($produc tPackageArray[$packageId] as $contentId =>
$publishedArray ) {
foreach($publis hedArray as $published =$productValue) {
$_ObjProduct = product::select (array('content _id'=>
$contentId,'pub lished'=>$publi shed));
$this->_productArra y[$_product[$contentId][$published]-
>productTypeI d] = $_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]-
>_productArra y[$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($packag eId=NULL,$title =NULL,$descript ion=NULL) {
$this->packageId = $packageId;
$this->title = $title;
$this->description = $description;

$productPackage Array = product_package ::select(array( 'package_id'=>
$packageId));
foreach($produc tPackageArray[$packageId] as $contentId =>
$publishedArray ) {
foreach($publis hedArray as $published =$productValue) {
$_ObjProduct = product::select (array('content _id'=>
$contentId,'pub lished'=>$publi shed));
$this->_productArra y[$_product[$contentId][$published]-
>productTypeI d] = $_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]-
>_productArra y[$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*******@attgl obal.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($packag eId=NULL,$title =NULL,$descript ion=NULL) {
$this->packageId = $packageId;
$this->title = $title;
$this->description = $description;

$productPackage Array =
product_packag e::select(array ('package_id'=>
$packageId)) ;
foreach($produc tPackageArray[$packageId] as $contentId =>
$publishedArra y) {
foreach($publis hedArray as $published =$productValue) {
$_ObjProduct = product::select (array('content _id'=>
$contentId,'pu blished'=>$publ ished));
$this->_productArra y[$_product[$contentId][$published]-
>>productType Id] = $_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]-
>>_productArr ay[$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*******@attgl obal.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($packag eId=NULL) {
$this->packageId = $packageId;
$this->productArray = product::select (....);
}
}
class product {
var $productId;
var $attributesArra y = array();
function product($produc tId=NULL) {
$this->productId = $productId;
$this->attributesArra y = attribute::sele ct(....);
}
}
2nd way *************** *************** *
*************** *************** ***********
class package {
var $packageId;
function package($packag eId=NULL) {
$this->packageId = $packageId;
}
}
class product {
var $productId;
function product($produc tId=NULL) {
$this->productId = $productId;
}
}

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

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
5031
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
2220
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" xmlns:xs="http://www.w3.org/2001/XMLSchema">
8
10230
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 = new Array(); GridArrayName1 = new Array ('test-value'); GridArrayName1 = GridArrayName1...
1
1474
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
2269
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 type but it could be any array or any type of comparison regex or whatever. Also I'm sure these can...
6
1353
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
1102
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 object to existing ones in my object. I can do that using the members list, one by one. I suspect that...
4
3203
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. I've done some initial googling and found PLENTY of samples of disabling all form items, but I...
0
8685
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9171
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.