Skip to content

Instantly share code, notes, and snippets.

@BenWard
Forked from dsingleton/PHP multi-array export.php
Created December 1, 2009 23:53
Show Gist options
  • Save BenWard/246760 to your computer and use it in GitHub Desktop.
Save BenWard/246760 to your computer and use it in GitHub Desktop.
<?php
$aOutput = array();
$aLines = explode("\n", file_get_contents($argv[1]));
$aKeys = explode("\t", array_shift($aLines));
foreach($aLines as $line) {
if (!trim($line)) {
break;
}
$aValues = explode("\t", trim($line));
if (count($aKeys) > count($aValues) && count($aValues) > 6) {
// Fewer keys...
$aKeys = array_slice($aKeys, 0, count($aValues));
}
if (count($aValues) < count($aKeys)) {
// Or blank values...
$aValues = array_pad($aValues, count($aKeys), '');
}
$item = var_export(array_combine($aKeys, $aValues), true);
// mung mung mung, get an array var export on to 1 line, improve readability
$item = str_replace("\n", "", $item);
$item = str_replace(" ", " ", $item);
$item = str_replace("array ( ", "array(", $item);
$item = str_replace(",)", ")", $item);
$aOutput[] = $item;
}
echo "array(\n\t" . join($aOutput, ",\n\t") . "\n);";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment