mystic_bovine Posted February 12, 2008 Share Posted February 12, 2008 The code below isn't working, so I guess an IF statement shouldn't be used. Can someone tell me what I should be using? $line2 = "TX, first, last, email, phone, zip, address, city, flooring_hardwood_install"; $array2 = explode(',', $line2); if ($array2[8] == "flooring_hardwood_install") { $trade = '51' ;} Thanks so much. Link to comment https://forums.phpfreaks.com/topic/90608-if-statements-and-arrays/ Share on other sites More sharing options...
resago Posted February 12, 2008 Share Posted February 12, 2008 the spaces are throwing you off. Link to comment https://forums.phpfreaks.com/topic/90608-if-statements-and-arrays/#findComment-464537 Share on other sites More sharing options...
kenrbnsn Posted February 12, 2008 Share Posted February 12, 2008 You're exploding on the "," character, but the items in your list are separated but ", ", so you're "if" condition isn't being met. Either explode on ", ", or use array_map(): <?php $line2 = "TX, first, last, email, phone, zip, address, city, flooring_hardwood_install"; $array2 = array_map('trim',explode(',', $line2)); if ($array2[8] == "flooring_hardwood_install") { $trade = '51' ;} echo $trade; ?> Ken Link to comment https://forums.phpfreaks.com/topic/90608-if-statements-and-arrays/#findComment-464538 Share on other sites More sharing options...
mystic_bovine Posted February 12, 2008 Author Share Posted February 12, 2008 Thanks for the replies. I used the $array2 = array_map('trim',explode(',', $line2)); and it worked. However the spaces in the example were my fault, It was last and I was tired. The data was actually coming from a Excel exported CSV file. When I looked at it in a plain text editor, I couldn't see any spaces. Would the fact it came from Excel have anything to do with it? Or because array[8] was at the end of the line? Link to comment https://forums.phpfreaks.com/topic/90608-if-statements-and-arrays/#findComment-464856 Share on other sites More sharing options...
kenrbnsn Posted February 12, 2008 Share Posted February 12, 2008 If you're reading a CSV file, you should look into using the fgetcsv() function. Using this function will probably simplify your code. Ken Link to comment https://forums.phpfreaks.com/topic/90608-if-statements-and-arrays/#findComment-464879 Share on other sites More sharing options...
mystic_bovine Posted February 12, 2008 Author Share Posted February 12, 2008 Thanks. I'll check it out. Link to comment https://forums.phpfreaks.com/topic/90608-if-statements-and-arrays/#findComment-464909 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.