PHP array concept
An array is a collection of several data stored in one variable. So, unlike the previous variable that only stores one value, an array can hold more than one value. For example, we will store our friends' data in one variable $friends. There are three friends that we will put into the variable. Visually, it can be described as follows:
Each value in an array (called an element) is accessed using an index. PHP recognizes two types of indexes, namely numeric indexes and associative indexes. Numeric indexes use numbers to mark each element, while associative indexes give each element a name. The use of indexes will be discussed in more detail below.
1. Initialize array
Initialization (filling in values) of the $teman array above is done in the following way:
$teman = array("Charlie", "Ani", "Budi");
The above code will make $teman a numeric indexed array. In addition to the above method, we can also create the same array by:
$teman[] = "Charlie";
$teman[] = "Ani";
$teman[] = "Charlie";
The result is a numeric indexed array which can be described as follows:
To create an associative indexed array, we need a label or name for each element. The example above will be expanded to store each friend's phone number. The visual representation is as follows:
in the image above, each friend's name will be used as a label for the stored phone number value. The program code is as follows:
$rekan = array ("Charlie"=>123, "Ani"=>528, "Budi"=>456);
or
$rekan = array("Charlie"=>123);
$rekan ["Ani"] = 528;
$rekan ["Budi"] = 456;
2. Array navigation
Array navigation (accessing values) can be done by looping. For example, to display the contents of the numeric indexed array above, we will use the following code:
for($i=0; $i<length($teman);$i++){
echo("Array \$teman index ke [$i] adalah $teman[$i]"); echo("<br>");
}
To access an associative indexed array, we cannot use a loop like above, because the element label/index is stored in string form. The loop to access an associative indexed array can be seen in the code sample below:
while(list($index, $nilai) = each($rekan)){ echo("Array \$rekan index $index berisi nilai $nilai"); echo("<br>");
}
There are two functions involved above, each and list. The each function is useful for accessing each element in an array. The resulting elements are key and value pairs, where key is the index and value is the content. The pair will be separated by the list function and inserted into the $index and $nilai variables.
3. Array functions
sort
Elements in an array can be sorted using the sort() function. An example of its use is as follows:
sort($teman);
for($i=0; $i<length($teman);$i++){
echo("Array \$teman index ke [$i] adalah $teman[$i]"); echo("<br>");
}
will produce the output:
Array teman index ke 0 adalah Ani
Array teman index ke 1 adalah Budi
Array teman index ke 2 adalah Charlie
asort and ksort
Asort is used to sort associative arrays according to their contents. Usage examples:
asort($rekan);
while(list($index, $nilai) = each($rekan)){ echo("Array \$rekan index $index berisi nilai $nilai"); echo("<br>");
}
will produce the output:
Array $rekan index Charlie berisi nilai 123
Array $rekan index Budi berisi nilai 456
Array $rekan index Ani berisi nilai 528
ksort is used to sort associative arrays according to their index. Example of use:
ksort($rekan);
while(list($index, $nilai) = each($rekan)){ echo("Array \$rekan index $index berisi nilai $nilai"); echo("<br>");
}
will produce the output:
Array $rekan index Ani berisi nilai 528
Array $rekan index Budi berisi nilai 456
Array $rekan index Charlie berisi nilai 123
The story is that I found a case study involving strings and arrays, but it wasn't easy for me because I had to create an array whose values were taken from string sentences separated by a comma separator.
What is the right way to do this?
Example:
String
9, admin@example.com, 8
Being this array
['9', 'admin@example', '8']
Solutip
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);
Output
Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)