PHP function to remove array element based on array key

Here is a small and simple PHP function to remove one or more than one array elements based on the keys of the array. You need to pass the original array along with the keys. Keys may be only one as a string or it may the array of keys. Resulting array would be the filtered array and preserved with array keys. If the given key is not found original array will be returned.

Here is the PHP code.

Example 1

Output :

Example 2

Output :

How to get JSON data easily using jQuery [AJAX]

AJAX is such a powerful technique which helps for creating fast and dynamic web pages. It allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
And to implement AJAX, you cant believe how jQuery has made that easy. There is now no need of writing browser dependent Ajax code. jQuery will take care of that.

While retrieving data from server side through ajax, we can use different various types of datatypes like html, text, xl, json, etc. I want to show here an short and simple example of using ajax and jquery and you will me amazed how easy that is. Lets grab the twitter feed of an id in the json format using ajax and jQuery. I have used $.getJSON for simplicity. You can use $.ajax in more flexible way with more parameters.

DEMO

In this example, there is only two parameters in $.getJSON function. Other required parameters in the ajax call will be handled by jQuery itself. One is the URL to be called and other is the callback function which will be called after successfull ajax event. After getting JSON response, $.each is used to access it and make output text ready. Then the html is replaced in the div.
 
DEMO
So if you are planning to use AJAX in your application and you are cosidering to use javascript library then jQuery would be one of your best options.
Its easy. Go and try it.

Photo : tutsplus.com

URI.js – javascript library for working with URLs

URL is the address of World Wide Web page. A URL is a formatted text string used by Web browsers, email clients and other software to identify a network resource on the Internet. Network resources are files that can be plain Web pages, other text documents, graphics, or programs. URI and URL are sometimes used as synonyms but there is little difference between them. URL is technically a type of URI. For example: http://www.example.com/index.php is a URL. URL consists of scheme (or protocol), authority, path and query string. There may also fragment in the URL which the string after # sign in the URL.

We may sometime need to manipulate the url in our site. We can manipulate the URL string from the Javascript side also. Using pure javascript to manipulate is not impossible but very tedious. URI.js is the Javascript library for working with URLs. It offers a “jQuery-style” API (Fluent Interface, Method Chaining) to read and write all regular components and a number of convenience methods like .directory() and .authority().

URI.js offers simple, yet powerful ways of working with query string, has a number of URI-normalization functions and converts relative/absolute paths. While URI.js provides a jQuery plugin. URI.js itself does not rely on jQuery. You don’t need jQuery to use URI.js.


How to use it?
First you need to include the javascript library in your page. You can download it from here. You can find URI.js file in the src folder. Now you are ready to use the awesome feature of this javascript library.
Lets take an example of manipulating URL string in ‘jQuery style’.

If you want to add ‘username’ in the URL string the use username() function and the library will place the name in the correct place and you dont need to worry a bit. Lots of API functions are available in the library like directory(), suffix(), hash(), fragment(), search(), tld(), hostname(), port(), domain(), subdomain(), etc. This plugin surely makes our life easy in manipulation of URL. relativeTo() and absoluteTo() functions are there to calculate the relative and absolute paths. Also you can easily normalize and clean the URL.

Want to compare two URLs. Dont worry! equals() function will check the URLs and return output as true or false. Moreover parsing URN will also be piece of cake using this library. In the latest version, it also offers jQuery integration. For the plugin to work, you need to regularly loadURI.js, as well as jquery.URI.js. URI.js does not depend on jQuery unless you want to use the URI.js jQuery Plugin.

We must appreciate the author of the library Rodney Rehm for the creation of such useful library.

You can find whole API documentation here [API Documentation]. If you want to understand more about the URL, visit this link [Understanding URIs].

There are some alternatives of URL manipulation library, if you dont like this URI.js.

New Built-in web server in PHP 5.4

Out of several awesome features of new PHP 5.4, built-in web server is most impressive. It runs from command line and it can be used in any OS like Windows, Mac or Linux. PHP manual stresses that it is only for development purpose only, but it has great possibilities for the future. For example, you could distribute portable web applications on CD ROMs or USB sticks, or even as desktop applications, all created with PHP without needing GTK or other graphic libraries.

This web server is designed for developmental purposes only, and should not be used in production.

So, how to use it then. First download the new PHP 5.4 from the PHP’s official site. Extract it in a folder in your hard drive. For example C:php is the folder where our extracted files are kept.
If you are using Windows, then you need to set environment variables to access the executable file from any place. In Windows 7, open Control Panel and click System. Then click Advanced systems settings. There comes a pop up box. Click Environment variables. In the System Variables create new variable with variable name PATH and variable value ;C:php and save it.

To start a web server

This is the simplest and basic command to start the webserver. You have to run this command from root project folder. This will return either index.html or index.php. If neither file exists then 404 response code will be raised.
If you want to start with a specific document root directory then yoiu need to use -t flag followed by the path to the new docroot.

You can also have router script. Here is a sample provided in the PHP manual.

Enter command like this:

Server will display the requests for images but requests for html files will display “Welcome to PHP”.
Having builtin webserver has also one interesting advantage. You can directly download and test the script without any need to reconfigure apache or anything else. If its good then you can use it in the project. Though it is NOT recommended for production sites, we have to agree it is one nice and impressive addition in PHP.