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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:04:21+00:00 2026-05-26T09:04:21+00:00

I’ve two arrays like this: $arr = Array (1, 2, 3 ,4 ,5, 6

  • 0

I’ve two arrays like this:

$arr = Array (1, 2, 3 ,4 ,5, 6 ,7 ,8 ) ;

and this:

$arr2 = Array (7, 6, 5,8 ,3 ,2 ,1, 4 )  

The pairs of these arrays are the numbers with the same key ($arr[0]-$arr2[0] ecc.)

1-7
2-6
3-5
4-8
5-3
6-2
7-1
8-4

as you can see there are some duplicate pairs like 1-7 and 7-1 , 2-6 and 6-2 , 3-5 and 5-3, 4-8 and 8-4.

I need a function who recives these two arrays and return one array with each single pairs.

For example this is what the function should returns:

Array ( [0] => 1 [1] => 7 [2] => 2 [3] => 6 [4] => 3 [5] => 5 [6] => 4 [7] => 8 ) 

As you can see the pairs ares: 1-7 , 2-6, 3-5 and 4-8.

I’ve made this function who doesnt work properly:

function free_pairs($arr,$arr2){
$ok = 0;
$ris = array();
$indice_ris=0;
for ($i=1; $i <=count($arr) ; $i++) { 
    $x1 = $arr[$i];
    $x2 = $arr2[$i];
    for ($j=1; $j <= count($arr2) ; $j++) { 
        $y1 = $arr[$j];
        $y2 = $arr2[$j];
        if($x1 != $y2 && $x2 != $y1){
            $ok = 1;
        } else {
            $ok = 0;
        }
    }
    if ($ok == 1) {
        $ris[$indice_ris] = $x1;
        $ris[$indice_ris+1] = $x2;
        $indice_ris = $indice_ris+2;
        $ok = 0;
    }
    return $ris;
}

I think the problem is in this if:

if($x1 != $y2 && $x2 !=$y1)

What do you think about?

  • 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-05-26T09:04:21+00:00Added an answer on May 26, 2026 at 9:04 am

    After some corrections on your original script, this seems to work:

    $arr1 = Array (1, 2, 3, 4 ,5, 6 ,7 ,8);
    $arr2 = Array (7, 6, 5, 8 ,3 ,2 ,1, 4);
    $res = free_pairs($arr1, $arr2);
    print_r($res);
    
    function free_pairs($arr,$arr2){
        $ris = array();
        for ($i = 0; $i < count($arr); $i++) { 
            $x1 = $arr[$i];
            $x2 = $arr2[$i];
            $ok = 0;
            for ($j = $i+1; $j < count($arr2); $j++) { 
                $y1 = $arr[$j];
                $y2 = $arr2[$j];
                if($x1 == $y2 && $x2 == $y1){
                    $ok = 1;
                }
            }
            if ($ok == 0) {
                $ris[] = $x1;
                $ris[] = $x2;
            }
        }
        return $ris;
    }
    

    output:

    Array
    (
        [0] => 5
        [1] => 3
        [2] => 6
        [3] => 2
        [4] => 7
        [5] => 1
        [6] => 8
        [7] => 4
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I would like to count the length of a string with PHP. The string

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.