Page 1 of 1
comparing values in two arrays please help
Posted: Thu Mar 06, 2008 7:16 am
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);
?>
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 8:43 am
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";
?>
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 8:51 am
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;
}
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 10:11 am
by gammaman
Yes you understand perfectly, have not tested your code yet, but will asap, thank you very much.
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 12:14 pm
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);
?>
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 2:10 pm
by Sekka
That is because you altered my code and broke it.
See the for loop,
'$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).
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 2:41 pm
by Zoxive
Sekka wrote:That is because you altered my code and broke it.
See the for loop,
'$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]
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 3:34 pm
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);
?>
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 3:42 pm
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));
?>
Re: comparing values in two arrays please help
Posted: Thu Mar 06, 2008 6:16 pm
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!
