Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to compare the values of array from another array. Something I did, but I do not know how to preserve keys.

$razeni=Array(0=>1,1=>2,2=>0,3=>3);
$myservices=Array(0=>"text0", 1=>"text1", 2=>"text2", 3=>"text3", 4=>"text4", 5=>"text5", 6=>"text6", 7=>"text7");

Now compare

foreach ($razeni as $key=>$value) {
  $myservices_[$value] = $myservices[$value];
  unset($myservices[$value]);    
}

if (isset($myservices_))
{
  $myservices = array_merge($myservices_, $myservices);
}

and result:

Array
(
    [0] => text1
    [1] => text2
    [2] => text0
    [3] => text3
    [4] => text4
    [5] => text5
    [6] => text6
    [7] => text7
)

But I needed this result

Array
(
    [1] => text1
    [2] => text2
    [0] => text0
    [3] => text3
    [4] => text4
    [5] => text5
    [6] => text6
    [7] => text7
)
share|improve this question

1 Answer 1

up vote 1 down vote accepted

instead of using array_merge use

$myservices = $myservices_ + $myservices;

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator.

share|improve this answer
1  
Thanks, I'm stupid:) –  Medvidek Feb 13 '11 at 14:42
    
your welcome, glad to help –  Haim Evgi Feb 13 '11 at 14:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.