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 8796425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:37:14+00:00 2026-06-13T23:37:14+00:00

I have some Problems reducing a multidimensional array into a normal one. I have

  • 0

I have some Problems reducing a multidimensional array into a normal one.

I have an input array like this:

Array
(
[0] => Array (
        [0] => 17
        [1] => 99
    )
[1] => Array (
        [0] => 17
        [1] => 121
    )
[2] => Array (
        [0] => 99
        [1] => 77
    )
[3] => Array (
        [0] => 45
        [1] => 51
    )
[4] => Array (
        [0] => 45
        [1] => 131
    )

So I have a multidimensional array with some overlaps in the values (eg 17,99 and 17,121)
Now I want to have an output like this:

Array
(
[0] => Array (
        [0] => 17
        [1] => 99
        [2] => 121
        [3] => 77
    )
[2] => Array (
        [0] => 45
        [1] => 51
        [3] => 131
    )

I want to save, which articles are the same in my database this way. The output array shpuld still be a multidimesional array, but every number on the second level should be unique in the array.

I’m trying to solve this for more than a week now, but I dont get it to work. I know it should be easy…but anyway – I dont get it 😀

This is what i got so far:

$parity_sorted = array();
    foreach($arr as $key => $a){
        if(count($parity_sorted) > 0){
        foreach($parity_sorted as $key2 => $arr_new){
            if(in_array($a[0], $arr_new) || in_array($a[1], $arr_new)){
                if(!in_array($a[0], $arr_new)){array_push($parity_sorted[$key2], $a[0]);}
                if(!in_array($a[1], $arr_new)){array_push($parity_sorted[$key2], $a[1]);}
            } else {
                array_push($parity_sorted, array($a[0],$a[1]));
            }
        }
        } else {
            array_push($parity_sorted, array($a[0],$a[1]));
        }

    }

Did you maybe already solve problem like this or is there a much easier way? Maybe I just think too complicated (It’s not my first try, but this code was the last try)

Any help would be appreciated. Thanks a lot

  • 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-13T23:37:16+00:00Added an answer on June 13, 2026 at 11:37 pm

    Here is my revised code given your comment and a DEMO of it working as expected. ( http://codepad.org/CiukXctS )

    <?php
    
    $tmp = array();
    foreach($array as $value)
    {
        // just for claraty, let's set the variables
        $val1 = $value[0];
        $val2 = $value[1];
        $found = false;
        foreach($tmp as &$v)
        {
            // check all existing tmp for one that matches
            if(in_array($val1, $v) OR in_array($val2, $v))
            {
                // this one found a match, add and stop
                $v[] = $val1;
                $v[] = $val2;
                // set the flag
                $found = true;
                break;
            }
        }
        unset($v);
    
        // check if this set was found  
        if( ! $found)
        {
            // this variable is new, set both
            $tmp[] = array(
                    $val1,
                    $val2,
                    );
        }
    }
    
    // go trough it all again to ensure uniqueness
    $array = array();
    foreach($tmp as $value)
    {
        $array[] = array_unique($value); // this will eliminate the duplicates from $val2
    }
    

    ORIGIN ANSWER

    The question is badly asked, but I’ll attempt to answer what I believe the question is.

    You want to gather all the pairs of arrays that have the same first value in the pair correct?

    $tmp = array();
    for($array as $value)
    {
        // just for claraty, let's set the variables
        $val1 = $value[0];
        $val2 = $value[1];
    
        if(isset($tmp[$val1])) // we already found it
        {
            $tmp[$val1][] = $val2; // only set the second one
        }
        else
        {
            // this variable is new, set both
            $tmp[$val1] = array(
                $val1,
                $val2,
            );
        }
    }
    // go trough it all again to change the index to being 0-1-2-3-4....
    $array = array();
    foreach($tmp as $value)
    {
        $array[] = array_unique($value); // this will eliminate the duplicates from $val2
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problems inserting a const 1 into a textbox which is gridview.
I have some problems with this code? Here is my code that creates text
I have some problems with animated buttons in android. This is my code: private
I have some problems with this script and I would appreciate some help to
I have some problems with the codes below. The one that is giving me
Have some problems with NSIS uninstall code. RMDir /r $SMPROGRAMS\${PRODUCT_NAME} In Windows 7 this
i have some problems with a Query seem IN dosen't work with Group_concat, that
I have some problems with Javascript. In fact, I'm just newbie in that script
I have some problems with header() function. It works and doesn't work at the
I have some problems with my first Open Graph custom action: I've created a

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.