473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to iterate throut an array?

Hello,
I have an array like this:

Array
(
[0] =Array
(
[1] =Array
(
[ID] =7A585DC0
[Co] =Array
(
[C] =nat
[b] =7A585DC0
)

[T] =Array
(
[1] =Array
(
[ID] =>7A585DC0

I want to delete some items from it. I think I have to do it recursively,
but PHP says:
"Fatal error: Only variables can be passed by reference in c:\www\p.php on
line 51"

The function looks like this:

function Del(&$tresc)
{
if (is_array($tresc['T'])) Del($tresc['T']);
else if (!$tresc['T'])
{
for ($x=0;$x<count($tresc);$x++)
{
Del($tresc[$x]); //THIS IS LINE 51
}
}
else if (($tresc['Co']['C']=='hl') and
(!$tresc['Co']['n']))
array_splice($tresc,0,1);
}

How to iterate through all elements and delete some of them?

Regards,
Talthen
Aug 9 '06 #1
5 1685
talthen.z-serwera.o2 wrote:
How to iterate through all elements and delete some of them?
Take a look at

http://www.php.net/manual/en/control...es.foreach.php

You should be able to set up a function that iterates through the array and
does what you want.

HTH,
--
Benjamin D. Esham
bd*****@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
Go not to Usenet for counsel, for they will say both yes and no.
Aug 9 '06 #2
ta******************@nospam.pl wrote:
The function looks like this:

function Del(&$tresc)
{
if (is_array($tresc['T'])) Del($tresc['T']);
else if (!$tresc['T'])
{
for ($x=0;$x<count($tresc);$x++)
{
Del($tresc[$x]); //THIS IS LINE 51
}
}
else if (($tresc['Co']['C']=='hl') and
(!$tresc['Co']['n']))
array_splice($tresc,0,1);
}

How to iterate through all elements and delete some of them?

Regards,
Talthen
Well, I'm not sure if it is causing the specific error you mentioned,
but one of your problems is the line:
for ($x=0;$x<count($tresc);$x++)

In the array you gave as an example, you have some non-numeric key
values. Iterating a variable in order to do $tresc[$x] only works if
you have integer keys in order, such as
$tresc[0],$tresc[1],$tresc[2]...etc. For non-numeric keys, use the
foreach control structure.

Aug 9 '06 #3
Benjamin D. Esham wrote:
>You should be able to set up a function that iterates through the array and
does what you want.
mootmail-googlegroups[a_t]yahoo.com wrote:
In the array you gave as an example, you have some non-numeric key
values. Iterating a variable in order to do $tresc[$x] only works if
you have integer keys in order, such as
$tresc[0],$tresc[1],$tresc[2]...etc. For non-numeric keys, use the
foreach control structure.
Hm... thank you. I didn't know that. I thought it will enumerate all
possible keys. Will try with foreach.

Thank you both,
Talthen
Aug 9 '06 #4
Foreach is not a good solution in this task. I need to delete parent of
elements with specified value.
Let's say I have an array:
[0]-->[a]-->[A]
\-[b]
[1]-->[b]

Now I need to delete element [a] when its elements [A] and [b] have values
of, let's say, 1.
When I do foreach I cannot say what's the value of a parent or child of
current element.

Do you know how can I solve that?

Regards,
Talthen
Aug 10 '06 #5
Ok, array_walk works fine, but I cannot delet elements ;/

Regards,
Talthen
Aug 10 '06 #6

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

Similar topics

6
by: ChronoFish | last post by:
Hi there, I want to iterate through an array starting at a known index. However the indexes are not linear. For example I have an array of events keyed by timestamp. $eventList = array...
2
by: deko | last post by:
I have a file that contains the output of time(). A different time is on each line of the file, and each line represents a visit to the website. I want to calculate the total visits per day,...
1
by: | last post by:
I'm going a little crazy trying to learn how to use arrays as properties in VBScript classes. Hopefully someone can help. First, I can't figure out whether it's possible to iterate through the...
4
by: nicolas | last post by:
Hello,everybody I get a problem: 1. Have an Int Array; 2. Run iterate over a range of Array once; 3. Get Minimum result and No.2 minimum result;
5
by: Martin Pöpping | last post by:
Hello, I want to iterate the second dimension of a 2-dim-array. Let´s say I have an array: double myArray and a given index: int i. Assume my index is given in want to iterate my array...
7
by: Jacob JKW | last post by:
I need to iterate over combinations of n array elements taken r at a time. Because the value of r may vary quite a bit between program invocations, I'd like to avoid simply hardcoding r loops. I...
5
by: Manish Tomar | last post by:
Hi, I know that using for in loop to iterate over array elements is a bad idea but I have a situation where it is tempting me to use it. I have a two dimensional sparse array say "arr" which...
1
by: nagarwal | last post by:
hi frnds, I am facing a prblm in displaying the contents of an ArrayList which contains 'String' Objects using logic:iterate tag.. example: in the Action : String s=null; String s1=null;...
25
RMWChaos
by: RMWChaos | last post by:
Any JSON experts out there? I'd like to know if it is possible, and if so how, to iterate through a JSON property list so that each iteration selects the next value for each object. Here is an...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.