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 check if a value is already in an array you will need to use the in_array function like shown below! <?php $values = array("pizza","hello","cheese","fred"); $newvalue = "hello"; if (in_array($newvalue, $values)) { echo "$newvalue is already in the 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
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 merge and sort an array in PHP you need to use the array_merge() and sort() functions like shown in the example below: <?php $array1 = array(1, 5, 3, 9, 7); $array2 = array(8, 2, 6, 4, 0); // merge the arrays $merge = array_merge($array1, $array2); // 1, 5, 3, 9, 7, 8, 2, 6, 4, 0 // sort the array sort($merge); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ?>
To check if a value is already in an array you will need to use the in_array function like shown below! <?php $values = array("pizza","hello","cheese","fred"); $newvalue = "hello"; if (in_array($newvalue, $values)) { echo "$newvalue is already in the 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
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 ?>
There are a few methods however the following is the method that will allow you to do the most with the information afterwards. foreach($array as $key => $value){ echo '[' . $key . '] ' . $value; #$key becomes the array key and value because what the current array item has inside. }
Creates an array using one value for keys and other for its values http://php.net/manual/en/function.array-combine.php
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 ?>
array_column was not introduced into PHP until 5.5.0, so calling it in 5.3.4 would result in a fatal error, calling an undefined function.
In the declaration of the receiving function, you add an ampersand. <?php function myWayCoolFunction( &$params) {.....} $x = array('1','2','3'); myWayCoolFunction($x) ?>
The recommended function (a construct, really, but works as a function) would be array("value", "value"), which is much more backwards compatible. However the short syntax of ["value", "value"] is also available and frequently used, as it has been for some time.
sort() will order the array by its values without preserving the keys. Use it when array is indexed numerically or when you do not care about the keys. asort() will also sort the array by its values, but it will preserve the key -> value association.
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 ?>