There are two types of arrays, associative arrays, and indexed arrays.
Indexed arrays are where you access the items in order, for example
$myArray[0] would be the first item in the array, $myArray[1] would be the second item, and so on.
You can create an indexed array like this:
$myArray = array("item1","item2","item3");
echo $myArray[0]; //item1
echo $myArray[2]; //item3
You can also set your own indexes:
$myArray = array(0=>"item1", 1=>"item2", 5=>"number 5");
echo $myArray[0]; //item1
echo $myArray[2]; //null or doesnt exist, i cant remember
You can also add items after using the square-brackets. If you include a number, that index is set, otherwise it just adds it to the end of the array.
$myArray[9] = "set index 9";
$myArray[] = "add to the end of the array";
Associative arrays use strings instead of numbers.
$colors = array("cat"=>"brown", "dog"=>"yellow", "fish"=>"purple");
echo $colors["cat"]; //brown
echo $colors["fish"]; //purple
And you can add more with the square-brackets again.
$colors["camel"] = "green";
To loop through both types of arrays you can use a foreach loop, or to loop through indexed arrays you can get the length into a variable and a do normal for loop.
Numeric array has numbers(+integers) that represent the values Associative array has strings that represent the values
To empty an array in PHP you need to use the unset function like shown below: <?php $array = array('hello', 'hi', 'weee'); unset($array); // empty ?>
To find the size of an array in PHP you can either use the count() function or the sizeof() function as they will produce the same result. <?php $array = array(1, 2, 3, 4, 5, 6, 7); echo count($array); // outputs 7 echo sizeof($array); // outputs 7 ?>
To pick random results from an array in PHP you will need to use the array_rand() function, you can select the amount of results you wish it too select too. <?php $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $rand_results = array_rand($array, 3); // will select 3 random results from the array and store in a new array ?>
build an array of vowels then do a foreach on the array and then explode the string on the array value and the answer is -1 of the result
An associative array is one of a number of array-like data structures where the indices are not limited to integers.
Numeric array has numbers(+integers) that represent the values Associative array has strings that represent the values
To empty an array in PHP you need to use the unset function like shown below: <?php $array = array('hello', 'hi', 'weee'); unset($array); // empty ?>
A key is the name of a variable in an array ($array["key"]) and the index is the position it's at ($array = ["key" => 0], the index would be 0). Keys and indices are the same if the array is not associative though ($array = [true], the key holding the value true is named 0 and is at index 0).
A numericial array is an array with keys made up of only integers. An associative array is an array with keys made up of anything that is not an integer. In some languages, it is possible to mix integer keys and non-integer keys into a mixed array.
To shuffle an array in PHP is easy to do, all you need to use is the shuffle() function like shown below: <?php $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); shuffle($array); // array results will be randomly shuffled ?>
To find the size of an array in PHP you can either use the count() function or the sizeof() function as they will produce the same result. <?php $array = array(1, 2, 3, 4, 5, 6, 7); echo count($array); // outputs 7 echo sizeof($array); // outputs 7 ?>
AWK, Perl and PHP are three examples.
To pick random results from an array in PHP you will need to use the array_rand() function, you can select the amount of results you wish it too select too. <?php $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $rand_results = array_rand($array, 3); // will select 3 random results from the array and store in a new array ?>
To reverse the results in a PHP array is really simple, all you need to do is use the array_reverse() function like shown below: <?php $my_array = array(1, 2, 3); $reverse_me = array_reverse($my_array); // will be stored as 3, 2, 1 now ?>
$foo = array(1.41421356, 1.61803399, 2.71828183, 3.14159265);
To separate a sentence into an array of words, you can use PHP's explode() function. If $text is your sentence, then use the following: $result = explode(" ", $text);