A multidimensional array is an array that has a value of an element as another array. Each element in the main array can also be an array. And each element in the sub-array can be an array.
- Syntax:$arr[index1] = array(key1=>value1, key2=>value2); $arr[index2] = array(key1=>value1, key2=>value2); ...$arr is the name of the main array.
<?php $life[] = 'nature'; $life['fruits'] = array('apples', 'banana', 'apricots'); $life['vegetables'] = array('tomatoes', 'cabbage', 'pumpkin'); ?>As you can notice, you can combine numeric and associative arrays. The key of the 'nature' is 0. The value of the other elements is another array (called sub-array).
$main_array[key_main_array][key_sub_array]This syntax is for a two-dimensional array. You can also use it to modify the value of a sub-array element or add more items in the sub-array.
<?php // define a two-dimensional array $life[] = 'nature'; $life['fruits'] = array('apples', 'banana', 'apricots'); $life['vegetables'] = array('tomatoes', 'cabbage', 'pumpkin'); // gets the value of first element of the 'fruits' sub-array $fruit1 = $life['fruits'][0]; echo 'I like '. $fruit1; // I like apples // modifies the value of first element of the 'fruits' sub-array $life['fruits'][0] = 'grapes'; echo '<br /> I like '. $life['fruits'][0]; // I like grapes // adds one more item in the 'vegetables' sub-array $life['vegetables'][] = 'onions'; echo '<br /> Now, the "vegetables" sub-array has '. count($life['vegetables']). ' elements'; ?>This code will output:
<?php // define a two-dimensional array $life[] = 'nature'; $life['fruits'] = array('apples', 'banana', 'apricots'); $life['vegetables'] = array('tomatoes', 'cabbage', 'pumpkin'); foreach ($life as $k => $v) { // if $v is an Array, appllies another foreach() loop to it, to get its elements if(is_array($v)) { foreach ($v as $key => $val) { echo '<br />'. $k. ' - '. $key. ' : '. $val; } } else echo '<br />'. $k. ' - '. $v; } ?>The first foreach() loop gets the key and the value of each element of the main array ($life). Inside this loop we check if the value of the current element is an array (with "is_array()"). If this function returns True, we create a second foreach() to traverse that sub-array, getting its keys and values.
If you want to get only the values of the array, you can use: foreach ($array as $value). It will ignore the key portion.
If you want to get only the keys of the array, you can use: foreach(array_keys($a) as $key)
<?php $life[] = 'nature'; $life['fruits'] = array('apples', 'banana', 'apricots'); $life['vegetables'] = array('tomatoes', 'cabbage', 'pumpkin'); print_r($life); echo '<hr />'; var_export($life); ?>This code will output:
<?php $code = array(10=>"Perl", 20=>"PHP", 21=>"Python", 30=>"JavaScript"); $curent = current($code); echo "Output:
the current() function returned: $curent"; $nxt = next($code); echo "
the next() function returned: $nxt"; $nxt = next($code); echo "
the next() function returned: $nxt"; $prv = prev($code); echo "
the prev() function returned: $prv"; $last = end($code); echo "
the end() function returned: $last"; ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>' https://coursesweb.net'); sort($lang); // sorts the array var_export($lang); // array ( 0 => 'HTML', 1 => 'JavaScript', 2 => 'PHP', 3 => ' https://coursesweb.net', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); rsort($lang); // sorts the array var_export($lang); // array ( 0 => 'coursesweb.net', 1 => 'PHP', 2 => 'JavaScript', 3 => 'HTML', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); asort($lang); // sorts the array var_export($lang); // array ( 1 => 'HTML', 20 => 'JavaScript', 10 => 'PHP', 'site' => 'coursesweb.net', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>' https://coursesweb.net'); arsort($lang); // sorts the array var_export($lang); // array ( 'site' => ' https://coursesweb.net', 10 => 'PHP', 20 => 'JavaScript', 1 => 'HTML', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>' https://coursesweb.net'); ksort($lang); // sorts the array var_export($lang); // array ( 'site' => ' https://coursesweb.net', 1 => 'HTML', 10 => 'PHP', 20 => 'JavaScript', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); krsort($lang); // sorts the array var_export($lang); // array ( 20 => 'JavaScript', 10 => 'PHP', 1 => 'HTML', 'site' => 'coursesweb.net', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); natsort($lang); // sorts the array var_export($lang); // array ( 1 => 'HTML', 20 => 'JavaScript', 10 => 'PHP', 'site' => 'coursesweb.net', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); natcasesort($lang); // sorts the array var_export($lang); // array ( 1 => 'HTML', 20 => 'JavaScript', 10 => 'PHP', 'site' => 'coursesweb.net', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); shuffle($lang); // sorts the array var_export($lang); // array ( 0 => 'PHP', 1 => 'coursesweb.net', 2 => 'HTML', 3 => 'JavaScript', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); $keys = array_keys($lang); // gets the keys var_export($keys); // array ( 0 => 1, 1 => 10, 2 => 20, 3 => 'site', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); $lang2 = array('ActionScript', 'Ruby'); $lang_merge = array_merge($lang, $lang2); // merge $lang and $lang2 var_export($lang_merge); // array ( 0 => 'HTML', 1 => 'PHP', 2 => 'JavaScript', 'site' => 'coursesweb.net', 3 => 'ActionScript', 4 => 'Ruby', ) ?>
<?php $nums = array(1, 7, 8, 25, 33, 78); $nums_sum = array_sum($nums); // gets the sum echo $nums_sum; // 152 ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); $val = 'PHP'; if(in_array($val, $lang)) echo $val. ' is in $lang array'; // PHP is in $lang array ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>'coursesweb.net'); unset($lang['site']); // sorts the array var_export($lang); // array ( 1 => 'HTML', 10 => 'PHP', 20 => 'JavaScript', ) ?>
<?php $lang = array(1=>'HTML', 10=>'PHP', 20=>'JavaScript', 'site'=>' https://coursesweb.net'); $str_lang = implode(' | ', $lang); // sorts the array echo $str_lang; // HTML | PHP | JavaScript | https://coursesweb.net ?>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net