Jump to content

If statements and arrays


mystic_bovine

Recommended Posts

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

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.