Manipulating CSV Data into Arrays with PHP

While I was working on the DoomCRM project today, I began to think about how we are going to migrate our vast amounts of historic data into our new database, which has a wildly different schema. Well, instead of having people go through and upload everything through the forntend, I decided it would be much easier to have them output the files as a CSV, then have PHP parse it into a usable format.  Well, here’s how I did it (with some easy to understand comments)!

<?php
/*
* Let's take the header and put it into a header variable
*/

$header = ["Player name","Player ID","Team","Season","Start","End"];
/*
* Let's place our csv data into a variable. In real-world practice, you'd likely use fopen to open the csv file, assign the first line to the header, and the rest to a different variable.
*/

$str = "
Rude,35,5,1,2014-07-02,2014-08-24
Capodecima,7,5,1,2014-07-02,2014-08-24
Intoxicate,24,5,1,2014-07-02,2014-08-24
Dranzer,16,5,1,2014-07-02,2014-07-14
Zakken,49,3,1,2014-07-02,2014-08-24
Yorick,48,3,1,2014-07-02,2014-08-24
Bless,50,3,1,2014-07-02,2014-07-07
Legion,27,3,1,2014-07-02,2014-08-24
Ralphis,31,2,1,2014-07-02,2014-08-24
Hex,20,2,1,2014-07-02,2014-08-24
Starrk,38,2,1,2014-07-02,2014-08-24
RottKing,34,2,1,2014-07-02,2014-08-24
DemonSphere,10,4,1,2014-07-02,2014-08-24
Reckless,32,4,1,2014-07-02,2014-08-24
Shane,37,4,1,2014-07-02,2014-08-24
Perrondon,30,4,1,2014-07-02,2014-08-24
Water,45,1,1,2014-07-02,2014-08-24
Tai,39,1,1,2014-07-02,2014-08-24
Pollution,51,1,1,2014-07-02,2014-08-24
Alt_Stab,2,1,1,2014-07-02,2014-08-24
Climhazzard,41,6,1,2014-07-02,2014-08-24
Wazzup,46,6,1,2014-07-02,2014-08-24
nub_hat,29,6,1,2014-07-02,2014-08-24
Psyych,52,6,1,2014-07-02,2014-07-07
IdeIdoom,22,3,1,2014-07-07,2014-08-24
TMD,40,6,1,2014-07-07,2014-08-24
Bless,50,5,1,2014-07-14,2014-08-24
";

/*
* This bit of code breaks apart $str into an array wherein $line[$x] will contain a single line from the csv. The explode command breaks $str when it sees PHP_EOL, or, in human terms, an end of line character. We then use array_filter to get rid of the blank array entries that the explode command creates. We will need the $count variable to assign a key to our outer array.
*/

$line = array_filter(explode(PHP_EOL,$str));
$count = 0;

/*
* Now we take each $line as value $a and break it up into array $b using the comma character
*/

foreach($line as $a) 
{
  $b=explode(",",$a);

  /*
  * So now we have the following..
  *
  * $b[0]=Player Name
  * $b[1]=Player ID
  * $b[2]=Team
  * $b[3]=Season
  * $b[4]=Start
  * $b[5]=End
  *
  * We can now create the loop to assign our values to our new array.
  * The loop below takes the array size of the header (in this case it's 6), and assigns our comma separated values, which are stored in array $b, to the $out array, with a key of the the header key value.
  */

  for($i=0;$i<sizeof($header);$i++)
  {
    $out[$count][$header[$i]] = $b[$i];
  }
  $count++;
}
print_r($out);
?>

Quesitons? Feel free to contact me on Twitter – @shark_rad


Posted in Tutorials

No Comments »

Let’s Play Doom – Compiling Odamex in Ubuntu 16.04 – 3 Easy Steps!

Everybody loves the demon-bashing, heart-racing, classic FPS Doom. Sometimes, when you feel the urge for some of them classic old-school FPS feels, you just gotta play some Doom. Fortunately, Odamex, will allow you to do just that. Odamex is a online multiplayer port for Doom fully delivers, and unlike other Doom ports that try and turn the game into Quake, Odamex feels  authentic. Now, if you’re a Linux junkie like myself, you’ll be happy to know that Odamex is one of the few multiplayer ports that will run in it.  So here’s how to install Odamex in 3 easy steps.

1. First, let’s update the apt-get repositories and install some dependencies.

$ sudo apt-get update
$ sudo apt-get install cmake gcc g++ git libsdl2-dev libsdl2-mixer-dev libwxgtk3.0-dev

WARNING: If you are installing Odamex 0.7.0 or lower, ie, not the master branch on GitHub, you will need to install libsdl1.2-dev and libsdl-mixer1.2-dev, and NOT the sdl2 libraries.

2. Next, we’ll enter your Home directory,  clone the Odamex repository into our home folder, prep our build folder, and call cmake to generate the makefile.

$ cd ~
$ git clone https://github.com/odamex/odamex.git
$ cd odamex
$ mkdir build
$ cd build
$ cmake ..

3. And now, our last step! If everything went smoothly, we can now compile Odamex with

$ make

Once your compiler does its job you will end up with client, odalaunch, master, and server folders inside your your build folder. You can now run Odamex issuing the following:

$ cd client
$ ./odamex


RIP AND TEAR!


Posted in Tutorials

No Comments »

Utilizing BASH and AWK to Merge Text Files

Sometimes, you need BASH to do some oddball things. This is one such task.

Scenario:

You are given two text files, file P,  which contains an alpha descending list of people, and file F, which contains a list of filenames belonging to that person. You would like to merge these files into a single, tab-delimited text file for later processing. Well, here’s how you can use BASH to do so!

$ nano ~/merge.sh
#!/bin/bash
echo "********** DIP FILE PREPARER **********"
echo ""
echo "
This program accepts 2 input files, an index file (f) containing the split PDF files and an index file (p) which contains the ids of the people to be matched with their file.  Please make sure that both files are sorted by alpha-descending (A to Z) order and that both files have the same number of lines, or the script will be unable to match them properly.
"
echo "==============================================================="
echo "1. Prepare ref or file index file"
echo "2. Merge ref_index.txt and file_index.txt"
echo "3. Display example pre-processed index file"

echo "What would you like to do?"
read action

case $action in
  "1") 	echo "Preparing file name index file"
       	echo "Please input full or relative file path to your index file containing a list of files: "
       	read file_index
       	echo "Please enter an output file name (no file extension is necessary): "
       	read output_file_index
       	awk '{ printf "\x22%s\x22\n",$0}' $file_index > $output_file_index.txt
       	echo "File output to $output_file_index.txt"
       	;;
  "2")	echo "Please input location of index file"
	read file_index
	echo "Please input location of ref file"
	read ref_index

	# get line count of each file
	ln_file_index=$(awk 'END {print NR}' $file_index)
	ln_ref_index=$(awk 'END {print NR}' $ref_index)

	# check if line counts are equal
	if [ "$ln_file_index" == "$ln_ref_index" ]; then
		echo "Files match!"
		echo "Please input Round Name (RG 2017, FED 2017, etc.)"
		read round
		echo "Preparing parser_output.txt"
		echo -e "\x22ref\x22\t\x22filename\x22\t\x22round\x22" >> parser_output.txt
		# loop and output lines into output file
		for (( x=1; x<=$ln_file_index; x++ ))
		do
			echo "Processing $file_index line $x"
			file_line=$(awk "NR==$x{print;exit}" $file_index)
			ref_line=$(awk "NR==$x{print;exit}" $ref_index)
			echo -e "$ref_line\t$file_line\t\x22$round\x22" >> parser_output.txt
		done
		echo "Processing complete"
	else
		echo "Files do not match!  Please make sure that the files have a matching line count"
		echo "$file_index line count  $ln_file_index"
		echo "$ref_index line count $ln_ref_index"
	fi
	;;
  "3")	cat sample_index.txt|less
	;;
esac

Posted in Tutorials

No Comments »

Installing Laravel 5.3 On a Fresh Copy of Ubuntu 16.10

In this video, we’ll go step-by-step and install Laravel 5.3 onto Ubuntu 16.10. We’ll tackle Laravel’s dependencies, as well as understand what they do. This tutorial should work for earlier versions of Ubuntu as well.

If you’ve enjoyed this video please stop by to comment or subscribe to my channel.

Happy Coding!


Posted in Tutorials

No Comments »

Hello world!

Welcome to WordPress Code Chirper! I’ve created this site not only to help others in the development world with tutorials, but to hopefully start a community of developers, tech junkies, and general IT enthusiasts. More to come!

Help Me Stack Overflow.. You're My Only Hope


Posted in Announcements

No Comments »