Arrays (PHP): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
(6 tussenliggende versies door dezelfde gebruiker niet weergegeven) | |||
Regel 1: | Regel 1: | ||
+ | == Basis == | ||
+ | |||
Code: | Code: | ||
Regel 6: | Regel 8: | ||
$a = array | $a = array | ||
( | ( | ||
− | + | # 'a', 'b' & 'c' are the indexes. 'apple', etc. are the values | |
− | + | ############################################################## | |
− | + | # | |
− | + | 'a' => 'apple', | |
− | + | 'b' => 'banana', | |
− | + | 'c' => array | |
− | + | ||
− | + | # This sub-array has no indexes. Only values | |
+ | ############################################ | ||
+ | ( | ||
+ | 'x', | ||
+ | 'y', | ||
+ | 'z' | ||
+ | ) | ||
); | ); | ||
print_r ($a); | print_r ($a); | ||
print var_dump($a); | print var_dump($a); | ||
− | print_r($a['a']); | + | print_r($a['a']); # You can refer to the index. It's a string, hence the apostrophes |
+ | |||
+ | print_r($a['c']); # Display subarray | ||
+ | print_r($a['c'][0]); Display item 0 of the subarray. You can only refer by number because no other index has been defined | ||
+ | |||
</pre> | </pre> | ||
Regel 64: | Regel 76: | ||
apple | apple | ||
+ | |||
+ | # print_r($a['c']); - Subarray | ||
+ | ############################## | ||
+ | # | ||
+ | Array | ||
+ | ( | ||
+ | [0] => x | ||
+ | [1] => y | ||
+ | [2] => z | ||
+ | ) | ||
+ | |||
+ | # print_r($a['c'][0]); - Item 0 from the subarray | ||
+ | ################################################# | ||
+ | # | ||
+ | x | ||
+ | </pre> | ||
+ | |||
+ | == Arrays vs. objects == | ||
+ | |||
+ | Belangrijk om bewust te zijn van het verschil tussen ''arrays'' en ''objecten''. Dit is een voorbeeld van een object: | ||
+ | |||
+ | <pre> | ||
+ | WP_Term Object | ||
+ | ( | ||
+ | [term_id] => 26 | ||
+ | [name] => Merk | ||
+ | [slug] => merk | ||
+ | [term_group] => 0 | ||
+ | [term_taxonomy_id] => 26 | ||
+ | [taxonomy] => product_cat | ||
+ | [description] => | ||
+ | [parent] => 0 | ||
+ | [count] => 0 | ||
+ | [filter] => raw | ||
+ | ) | ||
+ | </pre> | ||
+ | |||
+ | En dit is een array: | ||
+ | |||
+ | <pre> | ||
+ | Array | ||
+ | ( | ||
+ | [a] => apple | ||
+ | [b] => banana | ||
+ | [c] => Array | ||
+ | ( | ||
+ | [0] => x | ||
+ | [1] => y | ||
+ | [2] => z | ||
+ | ) | ||
+ | ) | ||
</pre> | </pre> | ||
+ | |||
+ | == Rij toevoegen - array_push == | ||
+ | |||
+ | <pre> | ||
+ | $a=array | ||
+ | ( | ||
+ | 0 => array | ||
+ | ( | ||
+ | 'sku' => '0100', | ||
+ | 'post_id' => 112, | ||
+ | 'lang' => 'nl', | ||
+ | ), | ||
+ | 1 => array | ||
+ | ( | ||
+ | 'sku' => '0100', | ||
+ | 'post_id' => 113, | ||
+ | 'lang' => 'en' | ||
+ | ), | ||
+ | 2 => array | ||
+ | ( | ||
+ | 'sku' => '0100', | ||
+ | 'post_id' => 116, | ||
+ | 'lang' => 'de' | ||
+ | ) | ||
+ | ); | ||
+ | |||
+ | $b=array | ||
+ | ( | ||
+ | 'sku' => '0100', | ||
+ | 'post_id' => 116, | ||
+ | 'lang' => 'hu' | ||
+ | ); | ||
+ | |||
+ | print_r($a); | ||
+ | print_r($b); | ||
+ | |||
+ | array_push($a, $b); | ||
+ | |||
+ | print_r($a); | ||
+ | </pre> | ||
+ | |||
+ | Merk op dat $b een dimensie minder telt dan $a. | ||
+ | |||
+ | == Rij toevoegen - Simpler == | ||
+ | |||
+ | In het voorbeeld hierboven was de eerste dimensie een index (of zoiets). Het kan simpeler in simpelere gevallen: | ||
+ | |||
+ | <pre> | ||
+ | ################################################################### | ||
+ | # Add a simple row to an array | ||
+ | ################################################################### | ||
+ | # | ||
+ | # Define array | ||
+ | ############## | ||
+ | # | ||
+ | $a=array | ||
+ | ( | ||
+ | 'nl' => 123 | ||
+ | ); | ||
+ | |||
+ | # Add a row | ||
+ | ############ | ||
+ | # | ||
+ | $a=$a + array | ||
+ | ( | ||
+ | 'de' => 234 | ||
+ | ); | ||
+ | |||
+ | print_r($a); | ||
+ | </pre> | ||
+ | |||
+ | == Rij updaten == | ||
+ | |||
+ | Tata: https://stackoverflow.com/questions/15024616/php-foreach-change-original-array-values | ||
== Bronnen == | == Bronnen == |
Huidige versie van 28 apr 2020 om 17:36
Basis
Code:
<?php $a = array ( # 'a', 'b' & 'c' are the indexes. 'apple', etc. are the values ############################################################## # 'a' => 'apple', 'b' => 'banana', 'c' => array # This sub-array has no indexes. Only values ############################################ ( 'x', 'y', 'z' ) ); print_r ($a); print var_dump($a); print_r($a['a']); # You can refer to the index. It's a string, hence the apostrophes print_r($a['c']); # Display subarray print_r($a['c'][0]); Display item 0 of the subarray. You can only refer by number because no other index has been defined
Output (slightly formatted):
# print_r($a): ################## # Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) # var_dump ################## # array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) "banana" ["c"]=> array(3) { [0]=> string(1) "x" [1]=> string(1) "y" [2]=> string(1) "z" } } # print_r($a['a']) ################## # apple # print_r($a['c']); - Subarray ############################## # Array ( [0] => x [1] => y [2] => z ) # print_r($a['c'][0]); - Item 0 from the subarray ################################################# # x
Arrays vs. objects
Belangrijk om bewust te zijn van het verschil tussen arrays en objecten. Dit is een voorbeeld van een object:
WP_Term Object ( [term_id] => 26 [name] => Merk [slug] => merk [term_group] => 0 [term_taxonomy_id] => 26 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 0 [filter] => raw )
En dit is een array:
Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) )
Rij toevoegen - array_push
$a=array ( 0 => array ( 'sku' => '0100', 'post_id' => 112, 'lang' => 'nl', ), 1 => array ( 'sku' => '0100', 'post_id' => 113, 'lang' => 'en' ), 2 => array ( 'sku' => '0100', 'post_id' => 116, 'lang' => 'de' ) ); $b=array ( 'sku' => '0100', 'post_id' => 116, 'lang' => 'hu' ); print_r($a); print_r($b); array_push($a, $b); print_r($a);
Merk op dat $b een dimensie minder telt dan $a.
Rij toevoegen - Simpler
In het voorbeeld hierboven was de eerste dimensie een index (of zoiets). Het kan simpeler in simpelere gevallen:
################################################################### # Add a simple row to an array ################################################################### # # Define array ############## # $a=array ( 'nl' => 123 ); # Add a row ############ # $a=$a + array ( 'de' => 234 ); print_r($a);
Rij updaten
Tata: https://stackoverflow.com/questions/15024616/php-foreach-change-original-array-values