comparing values in two arrays please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

comparing values in two arrays please help

Post by gammaman »

I am working on the following question and do not really know where to start



Here is the question:

Compare the first element of array1 to the last element of array2
and so on, each time storing the larger when compared in a thrid array

so for example in array1 the first element is 12 and in
array2 the last element is 3 so 12 should be stored in the third array.



Please help me, It is driving me crazy

Here is the code I have so far

Code: Select all

 
<?php
 
  function arrays($array1,$array2)
  {
    if(count ($array1) != count ($array2))
    {
      echo "Number of item are not equal";
    }
    else
    {
       foreach($array1 as $itm1)
       {
         
        
    }
 
 
 
  }
$my_array1= array(12,3,7,6,);
$my_array2= array(1,9,7,3);
arrays($my_array1,$my_array2);
?>
 
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: comparing values in two arrays please help

Post by gammaman »

Here is my updated code, still need help please!!!

Code: Select all

 
<?php
 
  function arrays($array1,$array2)
  {
    if(count ($array1) != count ($array2))
    {
      echo "Number of item are not equal";
    }
    else
    {
       $array3= array();
       $countb=count($array2);
       foreach($array1 as $itm)
       {
          if ($itm > $array2[$countb])
          {
                $array3[]=$itm;
          }
          else
          {
            $array3 = $array2[$countb];
          }
          $countb--;
       }    
       
     }    
        
    
   return ($array3);
 
 
  }
$my_array1= array(12,3,7,6,);
$my_array2= array(1,9,7,3);
$solution = arrays($my_array1,$my_array2);
echo "$solution";
 
?>
 
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: comparing values in two arrays please help

Post by Sekka »

Couple of questions.

1) Do both arrays have to be equal length?
2) Am I right in understanding you go from start to end in one array, and end to start in the other?

If the answer to both is yes, this should suit your needs, I think... (untested)

Code: Select all

function arrays ($array1, $array2) {
    
    // Validate arrays
    if (!is_array ($array1) || !is_array ($array2) || count ($array1) != count ($array2)) {
        return false;
    }
    
    // Reverse second array
    $array2_reverse = array_reverse ($array2);
    
    // Compare the arrays
    $array_new = array ();
    $total = count ($array1);
    for ($n = 0; $n < $count; $n ++) {
        
        // Store correct result
        if ($array1[$n] < $array2_reverse[$n]) {
            $array_new[] = $array2_reverse[$n];
        } else {
            $array_new[] = $array1[$n];
        }
        
    }
    
    // Return result
    return $array_new;
    
}
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: comparing values in two arrays please help

Post by gammaman »

Yes you understand perfectly, have not tested your code yet, but will asap, thank you very much.
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: comparing values in two arrays please help

Post by gammaman »

when I use your code it does not work. All that prints is "Array"?

Code: Select all

 
<?php
 
  function arrays($array1,$array2)
  {
    if(count ($array1) != count ($array2))
    {
      echo "Number of item are not equal";
    }
    
     $reverse = array_reverse($array2);
     $array3= array();
     $total = count($array1);
     for ($n=0;$n<$toal;$n++)
     {
       if ($array1[$n] < $reverse[$n])
       {
          $array3[]=$reverse[$n];
       } 
       else
       {
          $array3[]=$array1[$n];
       }     
     }
   return ($array3);
 
 
  }
$my_array1= array(12,3,7,6,);
$my_array2= array(1,9,7,3);
 
echo arrays($my_array1,$my_array2);
 
?>
 
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: comparing values in two arrays please help

Post by Sekka »

That is because you altered my code and broke it.

See the for loop,

Code: Select all

for ($n=0;$n<$toal;$n++)
'$toal' should be '$total' according to your new naming convention.

Also, when posting PHP code, please use the

Code: Select all

[/php ] tags (minus spaces of course).
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: comparing values in two arrays please help

Post by Zoxive »

Sekka wrote:That is because you altered my code and broke it.

See the for loop,

Code: Select all

for ($n=0;$n<$toal;$n++)
'$toal' should be '$total' according to your new naming convention.

Also, when posting PHP code, please use the

Code: Select all

[/php ] tags (minus spaces of course).[/quote][b]
@Sekka[/b]
Actually your code had it named $total, but in the for loop you were using the variable $count which was undefined. Looks like he was trying to fix it but had a spelling error.

Anyway..

[b]@gammaman[/b]
His function returns an array, so if you are just echoing then it will echo array.
[url=http://www.php.net/print_r]print_r[/url]
[url=http://www.php.net/print_r]var_dump[/url]
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: comparing values in two arrays please help

Post by gammaman »

It still did not work so I tried another way, still won't print anything

Code: Select all

 
<?php
 
 
 
 
   
  function arrays($array1,$array2)
  {
 
   if ( count ($array1) != count ($array2) ){
    echo ' Arrays are not the same size ';
    }
    else {  
        $array3=array();
        $countb = 0;
        $array2_reverse = array_reverse($array2);
                
        foreach ($array1 as $indiv){
            if ($indiv > $array2_reverse[$countb]){
                $array3[] = $indiv;
            }
            else {
            $array3[] = $array2_reverse[$countb];
            }
        $countb++;
                        
        }
    }
    return ($array3);
   }
$myarray1 = array(12, 3, 7, 6);
$myarray2 = array(1, 9, 7, 3);
arrays($myarray1,$myarray2);
print_r ($array3);
?>
 
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: comparing values in two arrays please help

Post by gammaman »

Never Mind I got it

Code: Select all

 
<?php
 
 
 
 
   
  function arrays($array1,$array2)
  {
 
   if ( count ($array1) != count ($array2) ){
    echo ' Arrays are not the same size ';
    }
    else {  
        $array3=array();
        $countb = 0;
        $array2_reverse = array_reverse($array2);
                
        foreach ($array1 as $indiv){
            if ($indiv > $array2_reverse[$countb]){
                $array3[] = $indiv;
            }
            else {
            $array3[] = $array2_reverse[$countb];
            }
        $countb++;
                        
        }
    }
    return ($array3);
   }
$myarray1 = array(12, 3, 7, 6);
$myarray2 = array(1, 9, 7, 3);
print_r (arrays($myarray1,$myarray2));
 
?>
 
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: comparing values in two arrays please help

Post by Sekka »

Zoxive wrote:@Sekka
Actually your code had it named $total, but in the for loop you were using the variable $count which was undefined. Looks like he was trying to fix it but had a spelling error.
So I did. Apologies. But mine should work if you correct that line.

I did say I didn't test it! :D
Post Reply