kate_hudson kate_hudson - 1 year ago 57
PHP Question

Formatting array data

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

[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)) {

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.


Answer Source

You have to unset the key from the original array :

foreach($csvArray as $key1 => $row) {
    foreach($row as $key => $value) {
        if(empty($value)) {

Hope it helps.