0

I am trying to remove the empty json data being posted. I tried the php function array_filter, but in all honesty, I am not sure if that is the right move.

 data[0][]  Test1
 data[0][]  91.00
 data[0][]  14.00
 data[1][]  Test2
 data[1][]  23.01
 data[1][]  14.00
 data[2][]  Test3
 data[2][]  32.00
 data[2][]  14.00
 data[3][0] 
 data[3][1] 
 data[3][2]
Aurora
  • 4,384
  • 3
  • 34
  • 44
Riaan
  • 538
  • 1
  • 4
  • 14
  • where is your php code? – Awlad Liton Feb 14 '14 at 19:59
  • The code (`?`) in your question doesn't make any sense. – Amal Murali Feb 14 '14 at 19:59
  • $colMap = array( 0 => 'Item', 1 => 'Price', 2 => 'Vat' ); if (isset($_POST['changes']) && $_POST['changes']) { foreach ($_POST['changes'] as $change) { $rowId = $change[0] + 1; $colId = $change[1]; $newVal = $change[3]; if (!isset($colMap[$colId])) { echo "\n spadam"; continue; } $select = $conn->prepare('SELECT SlipEntry_Idx FROM slip_data WHERE SlipEntry_Idx=? and Vendor_Slip_ID = 72 LIMIT 1'); $select->execute(array( $rowId )); – Riaan Feb 14 '14 at 20:02
  • I am trying to format the code LOL one moment – Riaan Feb 14 '14 at 20:04
  • Add the code to the question. – jeroen Feb 14 '14 at 20:05
  • If you need a bit of background, this example was for sqlite: http://warpech.github.io/jquery-handsontable/demo/php.html but i converted it to mysql pdo. What I am basically trying to achieve is preventing empty values from being saved causing: Integrity constraint violation: 1062 Duplicate entry '4' for key 'PRIMARY' – Riaan Feb 14 '14 at 20:15

1 Answers1

0

Use json_decode to convert your json array to a php array and use array_filter to remove all empty keys.

$json = '{"a":1,"b":2,"c":,"d":4,"e":5}';
$json = json_decode($json);
$jsonFiltered = array_filter($json);

Maybe you need to do a array_map for a multidimensional array!

$json = array_map('array_filter', $json);

Didn't test this code!

Timo002
  • 3,138
  • 4
  • 40
  • 65
  • I tried this: $colMap = json_decode($colMap); $colMap = array_filter($colMap); once again apologies.. newbie. trying to learn this json thing – Riaan Feb 14 '14 at 20:10
  • I think i will use this as a work around: http://stackoverflow.com/questions/812437/mysql-ignore-insert-error-duplicate-entry I can not get the array filter to work – Riaan Feb 14 '14 at 20:36
  • This did the trick, instead of trying to filter the json, I just make sure the empty data does not get writing to the database causing duplicate errors using INSERT IGNORE INTO – Riaan Feb 14 '14 at 20:40