Learning MEL – Step #8 – Arrays (Part 2)

We already have covered arrays basic points. In this part I go over working with arrays.

When a new thing is introduced, in any place and field, from math to technology, in religion or politics, in CGI or in traditional painting, it comes with associated issues/advantages/components/tools etc. Therefore array also has it own functions and methods. Do not forgot that array is a hero in Computer Programming.

Related Functions:

Functions to be solely used with arrays are:

sort(), size() and clear();

Above three functions are general functions related to arrays. There are many functions used with array that are both complex and far from our point of study, thus I leave them uncovered. Let’s begin explaining our triple functions.

sort(): Use this function simply by placing the array inside the () of the function.

sort($array);

This function would sort the element in an array in “ascending order” and returns a sorted array. However, be aware that the array does not get sorted internally, only returned values from sort() function would sort the output. I return to this point later. Just for now, focus on this example:

string $examples[] = {“hello”, “laugh”, “that man”, “somewhere”, “awful”};
print $examples;

Regularly it outputs based on the order of declaration:

print $examples;
hello
laugh
that man
somewhere
awful

But if you use sort() function, the result is this:

sort($examples);

// Result: awful hello laugh somewhere that man //

However you if you like to keep the result of examples, you have two choices:

1 – Put the result in a new array, like this:

$examples = sort($examples);

Look at the above example. There is not need to use array curly brackets after $example, because MEL already knows it is an array, and you are not going to reintroduce it to MEL, but just what you indeed do is assigning a new set of data.

2- Declare a new array. look at below:

string $sorted_array[] = sort($example);

Look, do not mess up the above $example array with method number 1. The above $example variable is the array unsorted array.

Both of above methods work. Sometime you need only  and only direct printing without keeping the result necessarily in an array. In that specific case, use sort() directly.

size(): Use this function simply by placing the array inside the () of the function.

size($array);

size() is a very very very important function which returns the number of elements in an array. why is it important? Because it dynamically allows you to know about the size of an array whenever you want to. For instance, if you want to print the list of selected nodes, how you would do that? of course you don’t know, since I have not yet covered it, but I will cover it perfectly in next tutorial. title Loops.

Look, this function would return an int number.

clear(): Use this function simply by placing the array inside the () of the function.

This function as its name is explanatory, would clear the array. It does not, however, undeclare it, it just remove every elements in the array , and let you have an empty array without any element.

Therefore, if I do clear our $example array, it will output:

clear ($example);

print ($example);

// Result: //

I wish you learn whatever we do cover here, as these are free tutorials.