0

I have two array.
How to check different and correct the order in a new array?

EG.

Array_a
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5

Array_b
0 => 1
1 => 2
2 => 3
3 => 3
4 => 5

You see different between Array_a and Array_b is
Array_a {3=>4}
Array_b {3=>3}

So I want to make a new array Array_c to make order in this change

Array_c
0 => 1
1 => 2
3 => 3
2 => 4
5 => 5

How can I process it?
Thanks~

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Biiim
0

Have a look at the array functions at php.net. array_diff() etc.

0

why not use for loop since they have same elements

$Array_c = array();
for($=0; count($Array_a); $i++){
    if(Array_a[$i]!=Array_b[$i]){
        //correct in array C 
    }
}
0

I still don't understand what you're trying to achieve.

0

Just give some example

Array_a
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5

Array_b
0 => 1
1 => 2
2 => 3
3 => 3
4 => 5

So compare with Array_a {3=>4} and Array_b{3=>3} is different
Then I need to make Array_c to generate a new array to customize Array_a and Array_b.
I want to Array_c as below

Array_c
0 => 1
1 => 2
2 => 4
3 => 3
4 => 5

=========================

Another case

Array_a
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5

Array_b
0 => 5
1 => 2
2 => 3
3 => 4
4 => 5

So compare with Array_a {0=>1} and Array_b{0=>5} is different
Then I need to make Array_c to generate a new array to customize Array_a and Array_b.
I want to Array_c as below

Array_c
0 => 2
1 => 3
2 => 4
3 => 5
4 => 1

==========================

Another case

Array_a
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5

Array_b
0 => 1
1 => 2
2 => 3
3 => 4
4 => 1

So compare with Array_a {4=>5} and Array_b{4=>1} is different
Then I need to make Array_c to generate a new array to customize Array_a and Array_b.
I want to Array_c as below

Array_c
0 => 5
1 => 1
2 => 2
3 => 3
4 => 4

=======================

Another case

Array_a
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5

Array_b
0 => 1
1 => 2
2 => 1
3 => 4
4 => 5

So compare with Array_a {2=>3} and Array_b{2=>1} is different
Then I need to make Array_c to generate a new array to customize Array_a and Array_b.
I want to Array_c as below

Array_c
0 => 3
1 => 1
2 => 2
3 => 4
4 => 5

=================

Sorry, I don't how to explan it. I hope these example you can understand what me need
Thank you~

Edited by erik216: n/a

0

Think i get what you want, you want the changed value to be first in arrayc and the rest pushed down?

<?php
$array1 = array(0 => 1,1 => 2,2 => 3,3 => 4,4 => 5);
$array2 = array(0 => 1,1 => 2,2 => 8,3 => 4,4 => 5);
$arrayresult = array(0 => 8,1 => 1,2 => 2,3 => 4,4 => 5);

$array3 = array();
$i = 0;
$c = count($array1);
while($i<$c){
	if($array1[$i] != $array2[$i]){
		$firstval = $array1[$i];
	}
	$i++;
}
$array3[0] = $firstval;
$i = 0;
while($i<$c){
	if($array1[$i] != $firstval){
		array_push($array3,$array1[$i]);
	}
	$i++;
}
var_dump($array1);
echo '<br/><br/>';
var_dump($array2);
echo '<br/><br/>';
var_dump($array3);
echo '<br/><br/>';
var_dump($arrayresult);
?>

that works for me on the one array i tested

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.