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
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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.