Sunday 15 May 2011

arrays - PHP IF ELSE condition and Foreach -



arrays - PHP IF ELSE condition and Foreach -

i have little issue trying compare info 3 arrays, 1 of them source , other 2 conditions.

the scenario next:

$array1 = array('code' => '123', 'code' => '124', 'code' => '125', 'code' => '126', 'code' => '127'); $array2 = array( array('code1' => '123', 'country' => 'us', 'listed' => '0'), array('code1' => '124', 'country' => 'us', 'listed' => '1'), array('code1' => '125', 'country' => 'us', 'listed' => '1') ); $array3 = array( array('code2' => '123', 'country' => 'us', 'listed' => '1'), array('code2' => '126', 'country' => 'us', 'listed' => '0'), array('code2' => '127', 'country' => 'us', 'listed' => '1') ); $final = array_merge($array1,$array2,$array3); foreach ($final $f) { if ($f['code'] == $f['code1']) { if ($f['listed'] > 0) { $finallisted = $f['listed']; } } elseif ($f['code'] == $f['code2']) { if ($f['listed'] > 0) { $finallisted = $f['listed']; } } $newfinalarray = array( 'code' = $finalcode, 'listed' = $finallisted, 'country' = $finalcountry ); }

so need check first if code $array1 exist in $array2 , if if code $array2 listed if not check on $array3 , on.

so if code exist on $array2 , listed 1 update database values if not check $array3 if exist , listed 1 update values if not update values $array2

the thought $array2 1 site , $array3 another, so, if not in 1 sec if in both keek $array2

the problem cannot sort, have tried array_combine combines 2 arrays , parameters need exactly. array merge 3 arrays one, on foreach , on apply if conditions variable undefined.

first of see lots of issue in way arrays declared you

ex -

$array1 = array('code' = > '123', 'code' = > '124', 'code' = > '125', 'code' = > '126', 'code' = > '127');

is nil but

$array1 = array('code' => '127'); //because of same index consider lastly value

however have modified arrays , tried prepare solution might useful you. check 1 time below.

<?php $array1 = array('123', '124', '125', '126', '127'); $array2 = array(array('code' => '123', 'country' => 'us', 'listed' => '0'), array('code' => '124', 'country' => 'us', 'listed' => '1'), array('code' => '125', 'country' => 'us', 'listed' => '1')); $array3 = array(array('code' => '123', 'country' => 'us', 'listed' => '1'), array('code' => '126', 'country' => 'us', 'listed' => '0'), array('code' => '127', 'country' => 'us', 'listed' => '1')); function comparesitesandupdate($array1, $array2, $array3) { foreach($array1 $code) { if(iscodeexistsinarray($code,$array2)) { echo $code . ' in array2 , listed <br />'; } else { // ;( not in array2 check in array3 echo $code . ' not listed in array2 - checking in array3 <br />'; if(iscodeexistsinarray($code,$array3)) { echo $code . ' in array3 , listed <br />'; } else { echo $code . ' not listed in array3 - whatever want <br />'; } } } } //note $earray expected in format of $array2/$array3 //and key of $array2 , $array3 should 'code' - not necessary alter keys 2 diff arrays function iscodeexistsinarray($ecode, $earray) { foreach($earray $code_array) { if($ecode == $code_array['code']) { //code match found - check if listed if($code_array['listed'] == 1) { //got need - homecoming true , break homecoming true; } } } homecoming false; //any other case homecoming false; } comparesitesandupdate($array1, $array2, $array3); ?>

php arrays

No comments:

Post a Comment