MemoryRefresh!

Search



Questions Answers 0%

Pages   0 from 51
Questions   0 from 84

Reset



in_array() - Check if a value exists in an array
<?php $array = array('a'=>1, 'b'=>2, 'c'=>3); echo in_array(1, $array); // Output: 1 array_key_exists() - Checks if the given key exists in the array
<?php $arr = array ('a' => NULL, 'b' => 2); echo isset ($arr['a']); // False echo array_key_exists('a', $arr); // True array_sum() - sum of values in an array
<?php $arr = array(1,2,3); echo array_sum($arr); // 6 array_product() - factorial
<?php $n = 3; echo array_product(range(1, $n)); // Ouput: 6 = 3! = 1*2*3 array_search() - searches value, returns key
<?php // May return boolean false (always use indentical operator) $array = array(1,2,3,4); echo array_search(4, $array); // Output 3 // Wrong if (array_search(5, $array) == 0) { // !! true echo 'Found'; // Output: Found! } // Corect if (array_search(5, $array) === 0) { // false echo 'Found'; // Output nothing } array_keys() - Return all the keys
<?php // If the optional search_value is specified, only the keys for that value are returned $arr = array('a', 'b', 'a', 'c', 'a', '0', false); print_r(array_keys($arr)); // [ 0, 1, 2, 3, 4, 5, 6 ] // search_value provided print_r(array_keys($arr, 'a')); // [ 0, 2, 4 ] // strict search (identical comparation) print_r(array_keys($arr, '0')); // [ 5, 6 ] !! not true print_r(array_keys($arr, '0', true)); // [ 5 ] corect count() - Count all elements in an array
<?php echo count(array(1,2,3)); // Output: 3 // For a variable that isn't set echo count($arr); // Output: 0 // Caution for false echo count(false); // Output: 1
Questions

Related pages

Basics !
Operations !
Create !
Key-value !
Search !
Modify !
Pointer !
Sort !
Stacks !
Randomize !
Storage !


0% 100%
 
 
0 pages 51 pages
1) How do you check if a value exists in an array?




2) How do you check if a key exists in an array?




3) How do you search the key for a given value?




Last Review: n/a   Passed: n/a   Reviews: n/a   Next Review: n/a  


in_array() - Check if a value exists in an array
<?php $array = array('a'=>1, 'b'=>2, 'c'=>3); echo in_array(1, $array); // Output: 1 array_key_exists() - Checks if the given key exists in the array
<?php $arr = array ('a' => NULL, 'b' => 2); echo isset ($arr['a']); // False echo array_key_exists('a', $arr); // True array_sum() - sum of values in an array
<?php $arr = array(1,2,3); echo array_sum($arr); // 6 array_product() - factorial
<?php $n = 3; echo array_product(range(1, $n)); // Ouput: 6 = 3! = 1*2*3 array_search() - searches value, returns key
<?php // May return boolean false (always use indentical operator) $array = array(1,2,3,4); echo array_search(4, $array); // Output 3 // Wrong if (array_search(5, $array) == 0) { // !! true echo 'Found'; // Output: Found! } // Corect if (array_search(5, $array) === 0) { // false echo 'Found'; // Output nothing } array_keys() - Return all the keys
<?php // If the optional search_value is specified, only the keys for that value are returned $arr = array('a', 'b', 'a', 'c', 'a', '0', false); print_r(array_keys($arr)); // [ 0, 1, 2, 3, 4, 5, 6 ] // search_value provided print_r(array_keys($arr, 'a')); // [ 0, 2, 4 ] // strict search (identical comparation) print_r(array_keys($arr, '0')); // [ 5, 6 ] !! not true print_r(array_keys($arr, '0', true)); // [ 5 ] corect count() - Count all elements in an array
<?php echo count(array(1,2,3)); // Output: 3 // For a variable that isn't set echo count($arr); // Output: 0 // Caution for false echo count(false); // Output: 1



More from Php

Basics
Variables
Operators
Function
Array
String
Streams
Uncategorized


More from Programming

Php
jQuery




Powered by minte9.com