Not easy! Best way to clean an array of arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fluk@tluk.it

    Not easy! Best way to clean an array of arrays

    Hi Guys, I hope someone can help me with this, because i'm getting
    crazy to find a good way to do that!

    This is what I got by querying a db.

    $arr1 = array("site", "descriptio n", "area1" , "activity1" );
    $arr2 = array("site", "descriptio n", "area1" , "activity2" );
    $arr3 = array("site", "descriptio n", "area2" , "activity2" );
    $arr4 = array("site1", "descriptio n1", "area3" , "activity2" );

    $my_array = array($arr1, $arr2, $arr3, $arr4);

    Site and description are linked because they are in the same row of
    the same table.
    But every site can have 1 or more areas and every area can have 1 or
    more activity.

    I'd like to clean $my_array in a way I can have a compact one without
    doubles

    //pseudocode!
    $new = array (
    array('site','d escr') =array (
    array('area','a ctivity')
    )
    );
    Is it possible??

    Thanks for your precious help
    Francesco
  • Jerry Stuckle

    #2
    Re: Not easy! Best way to clean an array of arrays

    fluk@tluk.it wrote:
    Hi Guys, I hope someone can help me with this, because i'm getting
    crazy to find a good way to do that!
    >
    This is what I got by querying a db.
    >
    $arr1 = array("site", "descriptio n", "area1" , "activity1" );
    $arr2 = array("site", "descriptio n", "area1" , "activity2" );
    $arr3 = array("site", "descriptio n", "area2" , "activity2" );
    $arr4 = array("site1", "descriptio n1", "area3" , "activity2" );
    >
    $my_array = array($arr1, $arr2, $arr3, $arr4);
    >
    Site and description are linked because they are in the same row of
    the same table.
    But every site can have 1 or more areas and every area can have 1 or
    more activity.
    >
    I'd like to clean $my_array in a way I can have a compact one without
    doubles
    >
    //pseudocode!
    $new = array (
    array('site','d escr') =array (
    array('area','a ctivity')
    )
    );
    Is it possible??
    >
    Thanks for your precious help
    Francesco
    Rather I think I'd make both the site name and the area indexes. You
    can still access the keys of those items, and it will make building your
    array much easier.

    use something like:

    $myarray = array($sitename =>
    array('descript ion' =$description,
    'area' =>
    array($area =>'activities' =>
    array($activity ))));

    $sitename is the actual sitename retrieved from the database. This
    would give you an array similar to

    [site1] = Array {
    [description] = First site's description
    [area] = Array {
    [area1] = Array {
    [0] = first activity
    [1] = second activity
    }
    }
    }


    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • fluk@tluk.it

      #3
      Re: Not easy! Best way to clean an array of arrays

      On 30 Mag, 13:33, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      f...@tluk.it wrote:
      Hi Guys, I hope someone can help me with this, because i'm getting
      crazy to find a good way to do that!
      >
      This is what I got by querying a db.
      >
      $arr1 = array("site", "descriptio n", "area1" , "activity1" );
      $arr2 = array("site", "descriptio n", "area1" , "activity2" );
      $arr3 = array("site", "descriptio n", "area2" , "activity2" );
      $arr4 = array("site1", "descriptio n1", "area3" , "activity2" );
      >
      $my_array = array($arr1, $arr2, $arr3, $arr4);
      >
      Site and description are linked because they are in the same row of
      the same table.
      But every site can have 1 or more areas and every area can have 1 or
      more activity.
      >
      I'd like to clean $my_array in a way I can have a compact one without
      doubles
      >
      //pseudocode!
      $new = array (
      array('site','d escr') =array (
      array('area','a ctivity')
      )
      );
      Is it possible??
      >
      Thanks for your precious help
      Francesco
      >
      Rather I think I'd make both the site name and the area indexes. You
      can still access the keys of those items, and it will make building your
      array much easier.
      >
      use something like:
      >
      $myarray = array($sitename =>
      array('descript ion' =$description,
      'area' =>
      array($area =>'activities' =>
      array($activity ))));
      >
      $sitename is the actual sitename retrieved from the database. This
      would give you an array similar to
      >
      [site1] = Array {
      [description] = First site's description
      [area] = Array {
      [area1] = Array {
      [0] = first activity
      [1] = second activity
      }
      }
      }
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===
      Hey Jerry!
      Many thanks for your help!

      Comment

      • C. (http://symcbean.blogspot.com/)

        #4
        Re: Not easy! Best way to clean an array of arrays

        On May 30, 2:33 pm, "f...@tluk. it" <francesco.camp ana...@gmail.co m>
        wrote:
        On 30 Mag, 13:33, Jerry Stuckle <jstuck...@attg lobal.netwrote:
        >
        >
        >
        f...@tluk.it wrote:
        Hi Guys, I hope someone can help me with this, because i'm getting
        crazy to find a good way to do that!
        >
        This is what I got by querying a db.
        >
        $arr1 = array("site", "descriptio n", "area1" , "activity1" );
        $arr2 = array("site", "descriptio n", "area1" , "activity2" );
        $arr3 = array("site", "descriptio n", "area2" , "activity2" );
        $arr4 = array("site1", "descriptio n1", "area3" , "activity2" );
        >
        $my_array = array($arr1, $arr2, $arr3, $arr4);
        >
        Site and description are linked because they are in the same row of
        the same table.
        But every site can have 1 or more areas and every area can have 1 or
        more activity.
        >
        I'd like to clean $my_array in a way I can have a compact one without
        doubles
        >
        //pseudocode!
        $new = array (
        array('site','d escr') =array (
        array('area','a ctivity')
        )
        );
        Is it possible??
        >
        Thanks for your precious help
        Francesco
        >
        Rather I think I'd make both the site name and the area indexes. You
        can still access the keys of those items, and it will make building your
        array much easier.
        >
        use something like:
        >
        $myarray = array($sitename =>
        array('descript ion' =$description,
        'area' =>
        array($area =>'activities' =>
        array($activity ))));
        >
        $sitename is the actual sitename retrieved from the database. This
        would give you an array similar to
        >
        [site1] = Array {
        [description] = First site's description
        [area] = Array {
        [area1] = Array {
        [0] = first activity
        [1] = second activity
        }
        }
        }
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstuck...@attgl obal.net
        =============== ===
        >
        Hey Jerry!
        Many thanks for your help!
        But really you should look at your DB and queries if you're getting
        duplicates returned and fix it there.

        C.

        Comment

        • Jerry Stuckle

          #5
          Re: Not easy! Best way to clean an array of arrays

          C. (http://symcbean.blogspot.com/) wrote:
          On May 30, 2:33 pm, "f...@tluk. it" <francesco.camp ana...@gmail.co m>
          wrote:
          >On 30 Mag, 13:33, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          >>
          >>
          >>
          >>f...@tluk.i t wrote:
          >>>Hi Guys, I hope someone can help me with this, because i'm getting
          >>>crazy to find a good way to do that!
          >>>This is what I got by querying a db.
          >>>$arr1 = array("site", "descriptio n", "area1" , "activity1" );
          >>>$arr2 = array("site", "descriptio n", "area1" , "activity2" );
          >>>$arr3 = array("site", "descriptio n", "area2" , "activity2" );
          >>>$arr4 = array("site1", "descriptio n1", "area3" , "activity2" );
          >>>$my_array = array($arr1, $arr2, $arr3, $arr4);
          >>>Site and description are linked because they are in the same row of
          >>>the same table.
          >>>But every site can have 1 or more areas and every area can have 1 or
          >>>more activity.
          >>>I'd like to clean $my_array in a way I can have a compact one without
          >>>doubles
          >>>//pseudocode!
          >>>$new = array (
          >>> array('site','d escr') =array (
          >>> array('area','a ctivity')
          >>> )
          >>> );
          >>>Is it possible??
          >>>Thanks for your precious help
          >>>Francesco
          >>Rather I think I'd make both the site name and the area indexes. You
          >>can still access the keys of those items, and it will make building your
          >> array much easier.
          >>use something like:
          >> $myarray = array($sitename =>
          >> array('descript ion' =$description,
          >> 'area' =>
          >> array($area =>'activities' =>
          >> array($activity ))));
          >>$sitename is the actual sitename retrieved from the database. This
          >>would give you an array similar to
          >> [site1] = Array {
          >> [description] = First site's description
          >> [area] = Array {
          >> [area1] = Array {
          >> [0] = first activity
          >> [1] = second activity
          >> }
          >> }
          >> }
          >>--
          >>============= =====
          >>Remove the "x" from my email address
          >>Jerry Stuckle
          >>JDS Computer Training Corp.
          >>jstuck...@att global.net
          >>============= =====
          >Hey Jerry!
          >Many thanks for your help!
          >
          But really you should look at your DB and queries if you're getting
          duplicates returned and fix it there.
          >
          C.
          No, it's quite possible to get duplicates in a 1-to-many situation. For
          instance, one customer may have several invoices. You print out all of
          the invoices for the month, and you may very easily get multiple entries
          for a customer, each with a different invoice.

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • C. (http://symcbean.blogspot.com/)

            #6
            Re: Not easy! Best way to clean an array of arrays

            On May 30, 7:00 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            C. (http://symcbean.blogspot.com/) wrote:
            On May 30, 2:33 pm, "f...@tluk. it" <francesco.camp ana...@gmail.co m>
            wrote:
            On 30 Mag, 13:33, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            >
            >f...@tluk.it wrote:
            >>Hi Guys, I hope someone can help me with this, because i'm getting
            >>crazy to find a good way to do that!
            >>This is what I got by querying a db.
            >>$arr1 = array("site", "descriptio n", "area1" , "activity1" );
            >>$arr2 = array("site", "descriptio n", "area1" , "activity2" );
            >>$arr3 = array("site", "descriptio n", "area2" , "activity2" );
            >>$arr4 = array("site1", "descriptio n1", "area3" , "activity2" );
            >>$my_array = array($arr1, $arr2, $arr3, $arr4);
            >>Site and description are linked because they are in the same row of
            >>the same table.
            >>But every site can have 1 or more areas and every area can have 1 or
            >>more activity.
            >>I'd like to clean $my_array in a way I can have a compact one without
            >>doubles
            >>//pseudocode!
            >>$new = array (
            >> array('site','d escr') =array (
            >> array('area','a ctivity')
            >> )
            >> );
            >>Is it possible??
            >>Thanks for your precious help
            >>Francesco
            >Rather I think I'd make both the site name and the area indexes. You
            >can still access the keys of those items, and it will make building your
            > array much easier.
            >use something like:
            > $myarray = array($sitename =>
            > array('descript ion' =$description,
            > 'area' =>
            > array($area =>'activities' =>
            > array($activity ))));
            >$sitename is the actual sitename retrieved from the database. This
            >would give you an array similar to
            > [site1] = Array {
            > [description] = First site's description
            > [area] = Array {
            > [area1] = Array {
            > [0] = first activity
            > [1] = second activity
            > }
            > }
            > }
            Hey Jerry!
            Many thanks for your help!
            >
            But really you should look at your DB and queries if you're getting
            duplicates returned and fix it there.
            >
            C.
            >
            No, it's quite possible to get duplicates in a 1-to-many situation. For
            instance, one customer may have several invoices. You print out all of
            the invoices for the month, and you may very easily get multiple entries
            for a customer, each with a different invoice.
            >
            The task at hand is to get rid of duplicates in a list. So 'SELECT
            DISTINCT customer....' or 'SELECT....GROU P BY Customer' - relational
            DBMS and SQL were specifically designed for solving this kind of
            problem - less code complexity and better performance than carrying
            out the same task in PHP.

            Yes, in another context these might not be duplicates, so use a
            different query. But if they are always duplicates then the problem is
            better solved by DDL and pre-empting occurrences.

            C.

            Comment

            Working...