Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:37:30+00:00 2026-06-09T20:37:30+00:00

Say i have a multidimensional array. For example: Array ( [0] => Array (

  • 0

Say i have a multidimensional array. For example:

 Array ( 
         [0] => Array ( 
             [animal_id] => 5494 
             [animal_name] => "Suzy"
             [animal_type] => "zebra" 
             [animal_location] => 0 
             [animal_awake] => 1
             [animal_age] => 3 ) 
         [1] => Array ( 
             [animal_id] => 5494 
             [animal_name] => "Joshua"
             [animal_type] => "panda"
             [animal_location] => 5
             [animal_awake] => 0
             [animal_age] => 8 )
        [2] => Array ( 
             [animal_id] => 5494 
             [animal_name] => "Debra"
             [animal_type] => "snake" 
             [animal_location] => 7 
             [animal_awake] => 1
             [animal_age] => 3 ) 
        [3] => Array ( 
             [animal_id] => 5495 
             [animal_name] => "Caleb"
             [animal_type] =>  "zebra"
             [animal_location] => 0
             [animal_awake] => 1
             [animal_age] => 3 ) 
        [4] => Array ( 
             [animal_id] => 5495 
             [animal_name] => "Joshua"
             [animal_type] =>  "panda"
             [animal_location] => 5 
             [animal_awake] => 0
             [animal_age] => 8 )    
        [5] => Array ( 
             [animal_id] => 5495 
             [animal_name] => "Debra"
             [animal_type] =>  "snake"
             [animal_location] => 7 
             [animal_awake] => 1
             [animal_age] => 3 ) 
        [6] => Array ( 
             [animal_id] => 5496 
             [animal_name] => "Emily"
             [animal_type] =>  "zebra"
             [animal_location] => 0
             [animal_awake] => 1
             [animal_age] => 3 ) 
        [7] => Array ( 
             [animal_id] => 5496 
             [animal_name] => "Joshua"
             [animal_type] =>  "panda"
             [animal_location] => 5 
             [animal_awake] => 0
             [animal_age] => 8 )    
        [8] => Array ( 
             [animal_id] => 5496 
             [animal_name] => "Debra"
             [animal_type] =>  "snake"
             [animal_location] => 7 
             [animal_awake] => 1
             [animal_age] => 3 )             
         )

And i want to compare all the snakes against the snakes, and all the panda’s against the panda’s etc (but not a snake against a panda) and put the unique elements into an array(all unique elements into a single array), how would i go about doing this. Since they are elements within an array, i’m a bit stumped. Also, i won’t know in advance how many different types there will be. For instance, one time i could be passed an multidimensional array with Panda, Bear, Snake — Next time i could be passed an array with Bird, Cat, Panda, Zebra.

Any ideas?

FINAL OUTPUT

      Array ( 
     [0] => Array ( 
         [animal_id] => 5494 
         [animal_name] => "Suzy"
         [animal_type] => "zebra" 
         [animal_location] => 0 
         [animal_awake] => 1
         [animal_age] => 3 ) 
     [1] => Array ( 
         [animal_id] => 5494 
         [animal_name] => "Joshua"
         [animal_type] => "panda"
         [animal_location] => 5
         [animal_awake] => 0
         [animal_age] => 8 )
    [2] => Array ( 
         [animal_id] => 5495 
         [animal_name] => "Caleb"
         [animal_type] =>  "zebra"
         [animal_location] => 0
         [animal_awake] => 1
         [animal_age] => 3 )    
    [3] => Array ( 
         [animal_id] => 5495 
         [animal_name] => "Debra"
         [animal_type] =>  "snake"
         [animal_location] => 7 
         [animal_awake] => 1
         [animal_age] => 3 ) 
    [4] => Array ( 
         [animal_id] => 5496 
         [animal_name] => "Emily"
         [animal_type] =>  "zebra"
         [animal_location] => 0
         [animal_awake] => 1
         [animal_age] => 3 )    

     )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:37:31+00:00Added an answer on June 9, 2026 at 8:37 pm

    Array keys must be unique, so lets use that to our advantage.

    function get_animal_key($animal) {
        return $animal['animal_type'] . '-' . $animal['animal_name'];
    }
    
    $uniques = array();
    foreach ($array as $animal) {
        $key = get_animal_key($animal);
        $uniques[$key] = $animal;
    }
    
    var_export($uniques);
    

    Gives the following array

    array (
      'zebra-Suzy' => 
      array (
        'animal_id' => 5494,
        'animal_name' => 'Suzy',
        'animal_type' => 'zebra',
        'animal_location' => 0,
        'animal_awake' => 1,
        'animal_age' => 3,
      ),
      'panda-Joshua' => 
      array (
        'animal_id' => 5496,
        'animal_name' => 'Joshua',
        'animal_type' => 'panda',
        'animal_location' => 5,
        'animal_awake' => 0,
        'animal_age' => 8,
      ),
      'snake-Debra' => 
      array (
        'animal_id' => 5496,
        'animal_name' => 'Debra',
        'animal_type' => 'snake',
        'animal_location' => 7,
        'animal_awake' => 1,
        'animal_age' => 3,
      ),
      'zebra-Caleb' => 
      array (
        'animal_id' => 5495,
        'animal_name' => 'Caleb',
        'animal_type' => 'zebra',
        'animal_location' => 0,
        'animal_awake' => 1,
        'animal_age' => 3,
      ),
      'zebra-Emily' => 
      array (
        'animal_id' => 5496,
        'animal_name' => 'Emily',
        'animal_type' => 'zebra',
        'animal_location' => 0,
        'animal_awake' => 1,
        'animal_age' => 3,
      ),
    )
    

    As you can see, this takes the animal’s type and name as the unique identifiers. Your question did not state what makes an animal unique, so alter the above to suit your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say I have a really complicated multidimensional array like this: Array ( [A]
Say I have a multidimensional array in PHP such as: this_array= array( string_name=>'string', string_array=>array(
say I have a multidimensional array like this: $data[0]['country'] = Angola $data[0]['report'] = Food
I have a multidimensional array, and I need to sort that array by a
Say I have an array such as: $arr[] = array(id => 11, name =>
Say i have an single dimension array (to keep it simple). Is there a
I am wondering how to use apply on a multidimensional array. I have something
Let say I have a multi-dimensional array like int a[10][10] int b[10][10] void arrayCopy(int*
I have a multidimensional array. The contents look like this in the debugger. The
Quick Question: I have the following array (multidimensional): Array ( [preorder] => 0 [data]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.