$new_values is an array, if you print that it will display this: Array ( [0] => cat [1] => company_name [2] => email_address [3] => address [4] => city [5] => state [6] => postal_code [7] => phone_number [8] => fax_number [9] => sic_code [10] => sic_description ) Now i have a for loop. for($x=0; $x < count($new_values); $x++) { if($new_values[$x] == "sic_description") echo 'test'; } The problem here is the if statement will never be true. But as you can see sic_description is the last element of my array. Please help me whats wrong with this. I appreciate your help. Thanks!
looooool Use "foreach" to loop through arrays, and everything will be OK! e.g.: foreach($new_values as $item){ if($item == "text") echo "Yahoo!!"; }
@kajfat: FYI, i did that already but it still doesn't work. @artus.systems: Actually i'm trying to compare 2 arrays here. Say, i have array1 and array2. I want to know if all elements of array1 is in array2. If there is one element in array1 that is not present in array2 the comparison should stop. It will return a flag whether all elements in array1 are present in array2 or not. i tried using in_array but i don't know why the last element of array1 can't be compared. $array1 = array('COMPANY_NAME','EMAIL_ADDRESS','ADDRESS','CITY','STATE','WEBSITE'); $array2 = array('COMPANY_NAME','EMAIL_ADDRESS','ADDRESS','CITY','STATE','PHONE_NO','FAX_NO','WEBSITE'); Having this, flag will always return 1.
Hope this helps... <?php $array1 = array('COMPANY_NAME','EMAIL_ADDRESS','ADDRESS','CITY','STATE','WEBSITE'); $array2 = array('COMPANY_NAME','EMAIL_ADDRESS','ADDRESS','CITY','STATE','PHONE_NO','FAX_NO','WEBSITE'); $result = array_diff($array1, $array2); //print_r($result); $count=count($result); if($count>0) { $flag=1; } else { $flag=0; } ?> ?> Code (markup):
I figured out why the last element of the array will always not equal its because of the array terminator. What i did if the loop reached the last element of the array to be compared i removed the terminator by substr('string', 0, -2). Now when i do array_diff(array1, array2) it will return correctly. @artus.systems, thanks for your reply. I do appreciate it.