www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] How to identify unique values in multiple sub-arrays of an array

  1. #1
    Join Date
    Dec 2013
    Location
    Melbourne, Australia
    Posts
    26

    resolved [RESOLVED] How to identify unique values in multiple sub-arrays of an array

    I have an array which looks like this:

    Code:
    Array
    (
        [0] => Array
            (
                [TechID] => 2602
                [TechName] => JOHN DOE
                [SaleType] => CLEANING
                [NonProdHrs] => 3.93
                [NonProdDollars] => 77.19
            )
    
        [1] => Array
            (
                [TechID] => 2602
                [TechName] => JOHN DOE
                [SaleType] => DRIVEWAY DUTIES
                [NonProdHrs] => 8.56
                [NonProdDollars] => 168.12
            )
    
        [2] => Array
            (
                [TechID] => 2602
                [TechName] => JOHN DOE
                [SaleType] => MAINTENANCE
                [NonProdHrs] => 0.77
                [NonProdDollars] => 15.12
            )
    
        [3] => Array
            (
                [TechID] => 2636
                [TechName] => JANE DOSE
                [SaleType] => DRIVEWAY DUTIES
                [NonProdHrs] => 13.82
                [NonProdDollars] => 323.25
            )
    
        [4] => Array
            (
                [TechID] => 2698
                [TechName] => BILL MAN
                [SaleType] => CLEANING
                [NonProdHrs] => 0.44
                [NonProdDollars] => 11.05
            )
    
        [5] => Array
            (
                [TechID] => 2698
                [TechName] => BILL MAN
                [SaleType] => DRIVEWAY DUTIES
                [NonProdHrs] => 7.27
                [NonProdDollars] => 182.62
            )
    
        [6] => Array
            (
                [TechID] => 2729
                [TechName] => PAUL PETERS
                [SaleType] => CLEANING
                [NonProdHrs] => 10.76
                [NonProdDollars] => 264.91
            )
    )
    What I want to find, quickly and easily is the number of all UNIQUE SaleType's and what they are. I'm hoping there is a simple function like that will let me access and combine all field's of SaleType in each sub-array and then group and remove duplicates?

    My desired results from this array would be to create another array which looks like this:

    Code:
    Array
    (
        [0] => CLEANING
        [1] => DRIVEWAY DUTIES
        [2] => MAINTENANCE
    )
    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    First thing that comes to my mind:
    PHP Code:
    $temp = array();
    foreach(
    $yourArray as $data) {
        
    $temp[] = $data['SaleType'];
    }
    $uniqueSaleTypes array_unique($temp); 
    Possibly a bit faster as long as you're sure each SaleType value can be used as a PHP array key:
    PHP Code:
    $temp = array();
    foreach(
    $yourArray as $data) {
        
    $temp[$data['SaleType']] = 1;
    }
    $uniqueSaleTypes array_keys($temp); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles