Jump to content

help with multidimensional arrays


nezbie

Recommended Posts

I've created a shitty csv editor to display and edit csv data. I'd need to be able to delete rows and colums (selected column for all the rows) with it. I've managed so far to delete rows only and I'm having a headache trying to think the column part through.. Although I believe I'm trying to do it the hard way - again..

The table consists of i[b]nputfields named data[rowIndex][columnIndex][/b], so when I have like 2 rows and 4 columns the output is following:

data[0][0] data[0][1] data[0][2] data[0][3]
data[1][0] data[1][1] data[1][2] data[1][3] and so on..


[code]
foreach ($_POST['data'] as $key=>$value) {
 
  // remove rows?
  if(!empty($_POST['delete'])) {
   
    // current row will NOT be deleted - if it is deleted do nothing (do not save info to $parsedData -variable   
    if(!array_search($key, $_POST['delete'])) {
     
      $parsedData .= implode(';', $value) . "\r\n";

    }

  }

  // no rows are selected for deletion - save all information to $parsedData normally
  else {

    $parsedData .= implode(';', $value) . "\r\n";
   
  }
 
} // END foreach
[/code]

Columns to be deleted are selected with checkboxes, named as an array $deleteColumn[valueOfColumnIndex] like $selected[2]

What would be the easiest (and hopely most performanced) way to check (and delete all array data with that index), for example if deleteColumn has a value of "2"

So the hoped output therefore woud be (using the output example above):

data[0][0] data[0][1] (deleted) data[0][3]
data[1][0] data[1][1] (deleted) data[1][3] and so on..

Thank you so very much for your ideas and help on this one! :)
Link to comment
https://forums.phpfreaks.com/topic/30905-help-with-multidimensional-arrays/
Share on other sites

try this (note chkbox names so data is sent in an array)
[code]
<?php
$mycsv = 'myfile.csv';      // pur your csv file name here

/**
* read data and store in array
*/
$lines = file($mycsv);
$data = array();
foreach ($lines as $line) {
    $data[] = explode (',', trim($line));
}
/**
* remove deleted rows and columns
*/
if (isset($_GET['action'])) {
    if (isset($_GET['delrow'])) {
        foreach ($_GET['delrow'] as $dr) {
            unset ($data[$dr]);
        }
    }
    if (isset($_GET['delcol'])) {
        foreach ($data as $row=>$items) {
            foreach ($_GET['delcol'] as $dc) {
                unset ($data[$row][$dc]);
            }
        }
    }
}

/**
* rewrite the csv file
*/
$fp = fopen($mycsv, 'w');
foreach ($data as $line) {
    fwrite ($fp, join(',', $line)."\r\n");
}
fclose ($fp);

/**
* display data
*/
$colCount = count($data[0]);
echo '<form>';
echo '<table border="1">';

echo '<tr><td>DELETE</td>';
for ($c = 0; $c < $colCount; $c++) {
    echo "<td><input type='checkbox' name='delcol[]' value='$c'></td>"  ;
}
echo '</tr>';

foreach ($data as $r => $items) {
    echo '<tr>';
    echo "<td><input type='checkbox' name='delrow[]' value='$r'></td>";
    foreach ($items as $val) {
        echo "<td>$val</td>";
    }
    echo '</tr>';
}
echo '</table>';
echo "<input type='submit' name='action' value='Delete selected'>";
echo '</form>';
?>[/code]

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.