Php compact
From w3cyberlearnings
Contents |
PHP function compact
This function creates array from variable and its value.
Syntax compact
- v1: variable 1
- v2: variable 2
compact(v1, v2,...);
Return
- When the variable is not existed, the variable names will be skiped.
Example 1
<?php $ella = 30; $amelia = 2; $liam = 21; $user_score = compact("ella", "amelia", "liam"); print_r($user_score); ?>
Output
Array ( [ella] => 30 [amelia] => 2 [liam] => 21 )
Example 2
<?php $ella = 30; $amelia = 2; $liam = 21; $bob = 40; $johy = 41; $name_s = array('bob', 'johy'); $user_score = compact($name_s, "ella", "amelia", "liam"); print_r($user_score); ?>
Output
Array ( [bob] => 40 [johy] => 41 [ella] => 30 [amelia] => 2 [liam] => 21 )
Related Links
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys