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~

Recommended Answers

All 6 Replies

Member Avatar for diafol

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

array_diff() only can get the value key changed

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 
    }
}
Member Avatar for diafol

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

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~

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.