How to iterate throut an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    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($tres c['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($t resc,0,1);
    }

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

    Regards,
    Talthen


  • Benjamin Esham

    #2
    Re: How to iterate throut an array?

    talthen.z-serwera.o2 wrote:
    How to iterate through all elements and delete some of them?
    Take a look at



    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.c om | AIM: bdesham128 | Jabber: same as e-mail
    Go not to Usenet for counsel, for they will say both yes and no.

    Comment

    • mootmail-googlegroups@yahoo.com

      #3
      Re: How to iterate throut an array?

      talthen.z-serwera.o2@nosp am.pl wrote:
      The function looks like this:
      >
      function Del(&$tresc)
      {
      if (is_array($tres c['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($t resc,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.

      Comment

      • Guest's Avatar

        #4
        Re: How to iterate throut an array?

        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


        Comment

        • Guest's Avatar

          #5
          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


          Comment

          • Guest's Avatar

            #6
            Re: How to iterate throut an array?

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

            Regards,
            Talthen


            Comment

            Working...