PHP array_push, and other array manipulators

Every once and a while I like to take a closer look at a particular function that I use quite frequently. This let's me make sure I'm using that particular function to the best possible effect. I find it also shifts the center of attention from a task to how the function operates. A different look so to speak. Today, I want to cover the PHP array manipulator family functions that include: array_push, array_pop, array_shift and array_unshift.

All the examples below are visualized with my free, open-source, variable checker that you are welcome to download from my newchk site at www.newchk.com Documentation on how to use it is on the site.

ARRAY_PUSH

Array_push($new_array, $var1, $var2, ...) pushes one or more array elements on to the end of an array, in this case, we'll use $new_array as our array. Seems simple enough. I want to create a new array, and add some variables to it. Let's take a look.

Let's set up our new array.

$new_array = array();
$var1 = "buick";
$var2 = "ford";
$var3 = "toyota";
array_push($new_array, $var1, $var2, $var3);


Let's take a look at $new_array;

If $new_array already exists with elements in it, then new variables are appended to the end of the already existing array. Let's show that.

$var_food1 = "cheese";
$var_food2 = "meat";
$var_food3 = "potatoes";
array_push($new_array, $var_food1, $var_food2, $var_food3);


Here's what the $new_array looks like now.

Some things to note about the array_push() function. It is a function, so there is no equal sign used with array_push. The order the variables are pushed on to the end of the array are in the order their listed in array_push. We can also use associative keys with array_push(), like so.

$var_ball1 = "football";
$var_ball2 = "the rock";
$var_ball3 = "hardball";
$ball_array = array("ball_football" => $var_ball1, "ball_basketball" =>$var_ball2, "ball_baseball" =>$var_ball3);
array_push($new_array, $ball_array );


Here's what the $new_array looks like now.

We've done a little more here than just assign associative keys to the array. We've jumped to what is called a multi-dimensional array. We now have an array within an array. If you look at the code above you see when working with multi-dimensional arrays, you build the inner array first, in this case the $ball_array, and then do an array_push using $ball_array as the variable in the array_push.

What got me to write this article was I was trying to do an array_push in a foreach loop using a multi-dimensional array. This is not well documented on the Internet. I had to look for awhile to find the proper syntax. When that happens I tend to write an article about it. The trick is to make a separate array variable before doing the array_push, and then add that array as a variable to the array_push().

Where do you most often use the array_push() function? Most of the time it's with a foreach loop, where your repetitively looping through an array, and creating a new subset of the array with array_push. Let's see.

$for_array = array();
$i = 1;
foreach( $new_array as $row)
{
	if($i < 7 )
	{
		array_push($for_array, $row );
	}
	$i ++;
}

Here, with a foreach loop, we've removed the second array, and are back to a one dimensional array, called $for_array.

ARRAY_POP

Array_pop($array) pops an element off the end of an array. Let's do it


$spuds = "";
$spuds = array_pop($for_array);


Our $for_array now is minus the last element in the previous array. That element is now in a separate $variable called $spuds. Array_pop() removes the last element in the array, which you can then put into a separate variable.

ARRAY_SHIFT

What if we wanted to remove the first element in the array, instead of the last, like we do with array_pop(). We'll we use array_shift to do this, like so.


$gm = "";
$gm = array_shift($for_array);


The $for_array we started with is getting shorter as the first variable in the array is removed, and put, in this case, in the variable $gm.

ARRAY_UNSHIFT

Finally, since array_push added new variables to the end of the array, you probably have guessed that there is a function that adds variable to the front of the array, and you'd be right. I present to you, array_unshift(). Array_unshift() puts the new variable as the first element in your array. Let's do it.


$gm2 = "cadillac";
array_unshift($for_array, $gm2);


Our array now looks like this.

We've replaced the "buick" we've taken off the top of the array with array_shift, and upgraded to a "cadillac" with array_unshift. Nice trade-up.

If your working with multi-dimensional arrays, the key is to treat the nested arrays as a single array variable, and then use the four functions we've covered: array_push, array_pop, array_shift and array_unshift with that single array variable.

Posted in Sorting Arrays | Leave a comment

Welcome, the Cinnamon 1.1.3 Desktop

Well, Ubuntu really messed up, yep, well actually Gnome messed up too, and we have definitely opened Pandora's box and let the evil's out.  Everyone wants an edge, and the perception is the edge is the desktop.  I guess, if Microsoft can switch its desktop around, and make users eat it, why can't the Linux distros do the same.

Let's review.  In the beginning there was X Windows, and every one was happy, but then the software gurus got the brainy idea of doing something like Microsoft Windows for X Windows.  What followed was a series of initial desktop applications, like KDE, Gnome, Xfce, and LXDE.  There were a few others, along with a couple of bare window managers that sprang up.  With the wide array of choices, folks interested in Linux were confused as to which distro to use, eventually KDE and Gnome came to the forefront.

In the meantime, while Linux distros were competing for users, Microsoft took over the world.  The distro's started to stabilize around Gnome 2, and folks got used to that desktop. Linux started picking up some market share.  I personally feel Gnome 2 ended up being the desktop of choice, although no disrespect to KDE, a noble competitor, especially with their latest release.  The emphasis in Gnome 2 was on productivity, not on novice users.

If you follow DistroWatch, which has been ranking the distros now since May, 2001, Ubuntu has been the top distro for five years.  It ran with Gnome 2, was stable, and had good support.  It was the Microsoft of Linux distros. Now Linux Mint has taken over.

Cinnamon Desktop

 

What happened?  I like to think a bunch of egos got in each others way.  The Gnome team decided they had to match Microsoft's 3d graphics and transparent desktop and decided to build Gnome 3.  It was their design, and when Canonical, the maker of Ubuntu, saw it, they said, no way, we can do it better, and the world will follow us, because we're number one.  The Gnome team wouldn't give in to Canonical, and Canonical wouldn't give in to the Gnome team.  Canonical developed their own Unity desktop.  Both teams wanted to make the desktop more friendly to the dumb Window's users as they saw Microsoft stumble with Vista.

The result was both teams stumbled, where they might not have if they had worked together.  The losers were the already dedicated Linux users who just wanted to get their work done.  They didn't care about 3d or transparency, they wanted the productivity that was provided by Gnome 2.  So Ubuntu and Gnome both stumbled by taking Gnome 2 away from the user, and forcing the user to use their latest creations, Gnome 3 and Unity.  Each turned out to be not ready for prime time.  Heck, Microsoft can foist bugs on us why not buggy desktops, wrong.

Mint decided to take a different approach.  They couldn't stay with Gnome 2, because the Gnome team wouldn't support it.  They decided to make Gnome 3 as close to Gnome 2 as possible to ease their users problems with moving to the new desktop, and allow users to continue being productive as they were before.  The result was a flood away from the Ubuntu distro to the Mint distro.  Mint is now number one.

With the knowledge the Mint team gained developing their Gnome 2 version of Gnome 3, they used to create a new desktop offering, called Cinnamon.  Mint just released Cinnamon 1.1.3.  Whew, that was a long way to go to get here, but I thought the story was worth it.

Initially the Mint team with their program, called Mate, tried to mimic Gnome 2 using GTK+ and the Gnome shell, but that approach didn't provide what Mint needed in desktop functionality.

Cinnamon is a yet another new desktop, but it's different.  First, if you've loaded Mint, and like Mint, you'll like Cinnamon. Cinnamon's main focus is on productivity.  Unity and Gnome 3's focus is on making Linux easy to use for the new user.

Cinnamon offers a bottom panel you can configure and hide.  It has all the features of the current Mint desktop, including an advanced menu system with the same layout as the Mint menu.  It has a custom panel launcher, and an advanced sound control to manage your and control your music.

Cinnamon is still in the early stages of its life.  They are working on a graphical configuration tool, and extensions and themes.  The important thing is Cinnamon is stable.  Cinnamon can be loaded with, or replace, any of those other desktops.

Cinnamon bears watching. KDE's 4.7 release is nice, and if you're a Fedora fan, this is the way to go, but if you like the old productive Gnome 2 desktop, it looks to me like Cinnamon will be the desktop of choice down the road, not Unity.

There's something else important about Cinnamon.  Mint didn't come out and say, "Use us, it's your only choice, and tough luck if there's bugs", like Gnome 3 and Unity did.  You can use Mint, while Cinnamon is continuing development, and has the proper time to bake, and become very stable.

Posted in Linux | 2 Comments

PHP Tools

Happy New Year!  For the start of the new year, I thought I'd review some of the PHP tools you should be learning, and using, to up your game in the coming year.

One of the challenges of a technical career is that your always wondering if you learn a new technology, if its going to be a waste of time.  You're constantly learning new things, and every time you're presented with the challenge of implementing something you've never done before you have to wonder if this technology will be around in another three years.  Technology moves that fast.

As you gain more experience in your chosen expertise, you get more selective in the tools your willing to spend time to learn.  Along those lines I thought it might be nice to do a light over view of where I see PHP tools at this moment in time.  These are the tools that I hear currently mentioned the most on the web, in workshops, and conferences.  In other words, the current tools of choice, and the tools you should seriously consider working with and learning.

First of all, we all should be using object-oriented coding.  PHP 5.3 is the way to go for now. The old procedural coding, for the most part, is dead.  I believe colleges are all teaching object-oriented now, so this is not a big deal.

Although, it seems MySQL is still the database of choice, I've seen a lot of use of two other databases you should be considering.  A file based SQLite.  It works great, and is super quick, except when you start to get into heavy transactions.  It's used to replace configuration and XML files, works great for mobile devices, and for small and medium size web site databases.

You might think that MySQL kicks in about then, but if you want to consider an alternative, I've been seeing the PostgreSQL database coming on strong.  For one thing, it's not Oracle controlled.  Oracle starts charging after you cross over a certain usage line.  It is the database of choice for raw speed doing complex tasks, like constantly displaying data for constantly updating weather monitors.

For JavaScript, hands down jQuery has been adopted universally.  Other tools I hear mentioned are Mootools, and Dojo.   For Ajax applications, I again hear jQuery.  jQuery has become so popular that is incorporated into a lot of PHP frameworks.

Speaking of PHP frameworks, there is a lot of buzz surrounding Zend Framework 2. The developers have reworked this PHP Framework to improve overall performance and take advantage of all the new features in PHP 5.3.  There has been an open forum during the development to get the best ideas from other PHP developers and implement them.  Consideration has been given to improving performance every step of the way.  The framework is currently in Beta release.  The documentation on the web on how to use this new release is starting to swell.  There are a lot of other good frameworks out there, but you should still spend some time to get to know ZF2, as its called, because of the extensive libraries of code available to ease your coding tasks.

Second, PDO and ORM for the database to PHP object type mismatch are important technologies.  You use PDO, for example, if you started with a MySql database and wanted to switch to the PostgreSQL database.  PDO makes switching databases easier.  ORM translates SQL database output to PHP objects for use in your application.  It makes coding database applications faster and easier with less problem in getting SQL queries correct.

There are many ORM options out there, however, the new Zend Framework 2 has settled on using Doctrine 2.  This makes Doctrine 2 the de facto standard to learn for applying PDO and ORM to your application, and make no mistake, don't let those SQL bigots get to you.  It is a benefit to code with an ORM.  Take it from me that has done it both ways.

A lot of frameworks now include testing components in the framework.  The testing component that is fast becoming the de facto standard is PHPUnit.  One new technology you should gradually start applying is unit tests.  The best way to start is to set up the testing environment, and write a couple of tests for your project, then gradually write a couple of more, expanding your test suite. Pretty soon you'll get the hang of it and it will become second nature.

For version control the current hot software is Git.  You should be using version control for all your projects.  Because Git is so popular, GitHub has become a mecca for releasing open source software, install Git, and join GitHub. Git runs on all platforms, if you haven't used version control software it's time to start.

For documenting your code you should be following the phpDocumentor syntax.  phpDocumentor has been the standard for a couple of years now.  My time spent learning how to document my code properly was well worth the time I spent looking over the phpDocumentor documentation.

Coding to a standard, so that all your code is written in the same format is good coding.  To help you check your code for syntax errors, and format your code for your custom style automatically, use PHP Code Sniffer.  This will also check your  CSS, and JavaScript. PHP Code Sniffer is installed through the Pear library.

Incidentally, if you're wondering what good code formatting looks like, I can recommend two references that I've run across.  One is on the web, in the Kohana framework documentation, and the other is in a book, "Advanced PHP Programming" by George Schlossnagle.  The important thing on code formatting is to choose a style and consistently use it in all your coding.

If you'd like a report on your code base, the number of lines of code, the complexity, the percentage of comments, number of classes, possible coding violations, bad practices with a bunch of other metrics.  The tool I hear mentioned is Sonar.

To build your application, run your test suite, and run every thing else with one command, like a "make" file, the application you want is Phing.

If you would like to automate several projects and run them on a schedule, "CruiseControl," will do that for you.  CruiseControl offers flexible scheduling, notifications, and integrates with Phing with a "PHP Under Control" plugin.

To solve your scaling problems for those web sites that start out small and get bigger really fast, and to get off of having to depend on your own hardware solution, or that of a commercial hosting company, there is a lot of good reasons to try out Amazon Web Services. There are other services out there, but Amazon stands way above all the others in features and pricing.

Don't get me wrong.  In each one of these categories other companies and developers have spent their time creating tools that do the same thing.  These other tools may be just as good, or even better.  I don't mean to put any of these other tools down.  All I'm suggesting is, at this moment in time, this is a tool you should consider spending your time learning, and that it probably would not waste your time.  If you prefer another tool, go for it, and let me know so I can take a look at it too.

Posted in PHP | Leave a comment

Thoughts for the Holidays

Happy Holidays

I was in the process of writing a technical article, and all of a sudden the Christmas spirit came upon me.  I had to stop.  My wife usually does the Christmas shopping for the family and kids.  I buy for her.  I've been doing a lot of shopping at Amazon.  Times are changing.  Usually, I don't get the bug until a couple of weeks before the holidays. On-line shopping seems to delay catching the spirit.  It seems a little sterile.  But when I do get the spirit, I let it roll over me, and I sit back and take another look at my life.  I'm going to ramble a little, bear with me.

I'm thankful I am living, and relatively healthy.  There are many folks out there that have a much harder life, because of their health.  My son had a concert given at an assisted living home.  I took away from the concert that I am thankful that I don't need a wheelchair.  Although it made me conscious that I might, when I get older.  Those people who have lost limbs, because they were involved in political conflicts, or wars, surely have a harder life than I.  I am thankful for their sacrifice for the betterment of us all, and thankful that I have been blessed enough not to come in harms way myself.

Christmas Cat

I am thankful I am living, because this truly is a marvelous planet we live on that has a multitude of wonders.  I marvel at the beautiful sunsets, the ocean, and the mountains.  I marvel at our animals.  I like watching them move and respond to their surroundings.  I love their defensive natures and wonder what has caused them to be that way their genes or their childhood.  I wonder about their lives.  From watching my pets, and the nature channel, I am aware that they too have feelings.  That they care for their family.   I wonder if God has truly abandoned them at death, or if they have a heaven of their own.

Yes, we have free will, and can decide whether we believe, or not, which supposedly means for those that make that choice, their spirit will go on beyond death, but what about all of God's creatures.  What a diversity of animals we have on this planet.  Beautiful animals, vicious animals looking for food to stay alive.  All the animals and plants on this earth live in the moment.  They live in the now.  We humans live in the past, present, and future, and what a mess that creates.  I strive to live in the now and take one day at a time, one moment at a time.  It makes life so much more pleasant.  "What if" doesn't worry me any longer, or at least, I like to believe that.  It's tough to maintain that Buddha nature.

Even though I strive to live in the moment, I am keenly aware that we only go through life once.  You're only in 6th grade once, only 15 once, only 21 once, only 35 once, and only 65 once.  Each year of my life seems like my surroundings, my reaction to those surroundings, my goals, and my desires of the moment will go on forever.  It's an illusion.  A dream we foist on ourselves.  The man at 65 has different priorities then the boy at 21.  We can't go back.  I can't go back and date my dream of my high school sweetheart, when I'm 55.  Our memories trick us.  What we forget is our high school sweetheart is 55 now also, and probably has a family, and an entirely different life, and different desires than she had when you were slobbering all over her.  Your memory pictures her at 16, not 55. You can take solace in that, hopefully, you're a memory for her too, and then lament you never did get it together.

What would our lives have been if we had married some one else?  Well, you didn't, and it best to acknowledge the pleasure in your fantasy, and at the same time come back to earth, and tell your wife, or husband, you appreciate and love her.  You married her, because you wanted her as your life partner, honor that, it's Christmas.

My own limitations come to mind.  I was a late bloomer, it made me feel not equal through out my life.  That I somehow wasn't equal to those that were blessed with their families love throughout their childhood.  That I wasn't equal to the kids that went out for high school football, or for you girls, the kids that were high school cheerleaders.  This is a cross that we bear from our childhood environment and upbringing.  We may never be released from these crosses during our lives, until our deaths.  It's a cross we carry, like baggage, through out our lives.  That makes it important for me to tell my kids that I love them every day, and I do.

Family

We believe we will be better parents than our parents, but we too find that having and raising kids is a first time experience for us all.  Each of us has to figure out how to raise kids without an instruction manual. Some are better, than others.  In the end, it isn't all your doing.  Every kid, like every pet is different.  They have different personalities and quirks.  For those, who have kids that you wish were different, that they were good, instead of bad, that they didn't do drugs, or get in trouble with the law.  Take a moment, and forgive yourself.  Its not all your doing.  You walked through life, and did your best, or not, when you were raising your kids.  Yes, you want it back, you want to do it all over, but you can't.  And so that's baggage you carry, I hope you can find a way to lighten your load.  Forgive yourself, and then forgive your kids.

Knowing that you only go through life once, I wish you well, and hope that you find your passion.  What do you like to do, what do you find yourself doing when your not working.  If you truly love doing something, you should go toward it.  Work in a job where you do something your passionate about.  I love computers, and I am blessed that I can make a living working with computers.  I've loved them since I was young.  It makes coming to work, not work.  I don't mind staying late at times, when I get interested in what I'm doing.  You can't do that if you don't follow your passion.

Don't just work to earn a living.  It will make life so much less enjoyable and harder for you.  Get the degrees you need to be a full fledged member of a select group of people that have the same passion that you do, and then strive to be the best you can be at your passion.  Making a living will come from that passion.  Don't settle, have the life you want, not the life that was determined by chance.  Care about how you spend your time on earth, you are only here once.

I wish you and your family a nice holiday of enjoying each other, enjoy the food, enjoy the spirit.  Realize that we all have dysfunctional families and dysfunctional family members.  Rather than condemn the wayward son, instead accept him and realize that he, or she, is also living their life, the way they want to live it.  Every one of us has chosen our lives.  We have made the choices that put us right where we are at this moment.  Honor each others life, and allow it to be OK.

In closing, I want to thank my readers.  Your coming back to the site time and time again is what keeps me writing and caring.  It's my small way of giving back to others what has made my life so enjoyable.  Thank you.  Come back often.  Enjoy your family.  Enjoy the holidays.

Posted in Uncategorized | Leave a comment

Setting the Focus on the Google Home Page

This is just a quick hitter for those folks that use Firefox as their main browser, and Google as their home page.

The Problem: the Focus is in the upper URL bar not in the Google Search box in a new Tab window

 

I am getting tired of having to reconfigure my system every time we get automatic updates.  A couple of months ago, it was Microsoft, in the guise of a security update, adding a browser toolbar that populated new tab windows with the Bing search bar.  That was sneaky, and a sign of things to come.  I wrote an article on how to fix this problem, if you find the Bing search engine popping up in your browsers new tab window.

The next gaff from Microsoft was when they "inadvertently" removed the Chrome browser from your computer in yet another security update. You can read about that one here.

I have VLC loaded to view videos in Firefox.  I recently discovered that Apple changed Firefox to use QuickTime instead of VLC. You can see this, and change it back in Firefox by going to Firefox options in the upper left Firefox icon, and selecting the Applications tab. Scroll down and you'll see Apple's QuickTime on all your video files. If you click the Action setting you can change it back.

Changing back to VLC instead of Quicktime

 

The latest annoyance comes from Firefox on their latest browser update.  For those, like me, who have been using Google as their home page for years, instead of those distracting portals, you'll find that when you open a new tab window in Firefox, Google home page will come up, but the focus of where you start typing is in the Firefox's upper URL window, instead of in the Google Home pages search box where it always use to be, see the first picture above.

The problem is when you immediately start typing your search query, it types in the upper URL bar and messes the URL up and doesn't search properly.  Before the focus was on the Google home page's search box when opening a new tab window, and you could start typing immediately.

What this means is when you open a new browser tab window in Firefox, and the Google home page opens, you also have to click in the search box before you start typing your search query.  An extra annoying step.  This is a consistent annoyance for those who are use to just typing their search query as soon as the tab window opens.

If you would like to change it back, in the Firefox top URL window, type "about:config" and hit return.  Ignore the warning, and say you'll "be careful" and Firefox config will open.

In the top filter bar, type "focus".  I think Firefox knew they may be making a mistake in changing the focus, because the setting you need to change, "newtaburl.focus_urlbar" is in bold, hmmm.  Double click anywhere on that line, and the "true" will change to "false" which is what you want. Close the browser and restart it.

Changing back to the Google Search Bar Focus

 

Now when you go to a new tab window in Firefox, and your Google home page loads, you'll find you can just start typing, like you use to do, and the search query your typing will be in the Google search bar, not on the top URL bar.  Enjoy.

All Fixed Up with the focus back in the Google Search Bar

 

 

Posted in Browsers | 7 Comments