Connecting Tech Pros Worldwide Help | Site Map

How to iterate throut an array?

  #1  
Old August 9th, 2006, 04:55 PM
Guest
 
Posts: n/a
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


  #2  
Old August 9th, 2006, 05:45 PM
Benjamin Esham
Guest
 
Posts: n/a

re: How to iterate throut an array?


talthen.z-serwera.o2 wrote:
Quote:
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
bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
Go not to Usenet for counsel, for they will say both yes and no.
  #3  
Old August 9th, 2006, 08:15 PM
mootmail-googlegroups@yahoo.com
Guest
 
Posts: n/a

re: How to iterate throut an array?


talthen.z-serwera.o2@nospam.pl wrote:
Quote:
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.

  #4  
Old August 9th, 2006, 10:15 PM
Guest
 
Posts: n/a

re: How to iterate throut an array?


Benjamin D. Esham wrote:
Quote:
>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:
Quote:
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


  #5  
Old August 10th, 2006, 09:55 AM
Guest
 
Posts: n/a

re: How to iterate throut an array?


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


  #6  
Old August 10th, 2006, 10:55 AM
Guest
 
Posts: n/a

re: How to iterate throut an array?


Ok, array_walk works fine, but I cannot delet elements ;/

Regards,
Talthen


Closed Thread