0

I process a CSV file and when all's said and done, I end up with something like this

Array
(
    [0] => Array
        (
            [Title] => 11AA002
            [Supplier Name] => Supplier A
            [Supplier Name2] => 
            [Effects2] => Bass
            [Effects3] => Percussion
            [Effects4] => Jazz / Piano
            [Effects5] => Drums
            [Components2] => Kit
            [Components3] => 
            [Components4] => 
            [Technology Areas2] => Something
            [Technology Areas3] => 
            [Technology2] => Cold 
            [Technology3] => Hot 
            [Technology4] => 
            [Technology5] => 
            [Briefs & Brands2] => 
            [Briefs & Brands3] => 
        )
    //Other elements    
)

What I am now trying to do is process the array of data. The first thing I am trying is to remove anything that has an empty value. For this I am attempting the following

foreach($csvArray as $row) {
    foreach($row as $key => $value) {
        if(empty($value)) {
            unset($key);    
        }
    }
}

This does not seem to remove the element from the array though. So with the above example, things like Supplier Name2 should be removed.

There are a couple of other things I am trying to sort out but not sure where to start. The Title should always be in the following format YYSUPCCC where YY is the Year, SUP is the first 3 letters of the first supplier, and CC is the count. So the above example is incorrect, it should be 16SUP001. The next array element that has the same supplier will be 002.

Would something like this be possible? Any advice appreciated.

Thanks

1
  • You received some answer to fixe the unset() issue but you'll have to post new Questions for any other problems you face. Commented Oct 12, 2016 at 17:58

3 Answers 3

2

Using a reference & to $row to modify the array directly, just filter it:

foreach($csvArray as &$row) {
    $row = array_filter($row);
}

That's it.

Sign up to request clarification or add additional context in comments.

Comments

1

unset($key);

you are trying to unset the variable named after the key (which makes no effect as such variable does not exist), not array element as you wanted. To do the latter, you should work on your source array instead. ie.:

foreach($csvArray as $k=>$row) {
    foreach($row as $key => $value) {
        if(empty($value)) {
            unset($csvArray[$k][$key]);    
        }
    }
}

instead.

5 Comments

Lol, a downvote... StackOverflow serious business :)
Just be fair please. It's exactly the same answer as mine... I need 1000 rep to be able to see who downvote my posts. ; )
@AdrienLeber: Rep doesn't matter you will NEVER see who downvoted you.
Grown up - it's pretty elementary question OP is asking, what other answer you expect here to show up? As for downvote - sounds like you did that, but I do not care really. I am just having fun seeing such childish behavior. BTW: you will NEVER see who downvoted you - you quite sure and can bet a limb? What if I just ask someone, whom I know personally, who has necessary admin perms on this site, to just peek the SO's database to check that for me? Think about that next time you do childish revenge downvote - it does not really affect me. It affects you.
You talk to much... It's not revenge downvote, it's fair downvote. Ask your friend to be sure about that. See another answer there.
0

You have to unset the key from the original array :

foreach($csvArray as $key1 => $row) {
    foreach($row as $key => $value) {
        if(empty($value)) {
            unset($csvArray[$key1][$key]);    
        }
    }
}

Hope it helps.

4 Comments

how are you getting $row data?
@RubabHossain Thanks. I fixed the typo.
Is there any way to check the Title so it is in the correct format? Thanks
@kate_hudson, as I wrote, you'll have to ask a new answer for that. I think you could use some regular expressions checks...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.