rfc:array_column

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:array_column [2013/01/12 17:17] – Added Example #4 ramseyrfc:array_column [2013/01/12 17:27] – Adding Example #5 ramsey
Line 64: Line 64:
         'first_name' => 'Jane',         'first_name' => 'Jane',
         'last_name' => 'Jones'         'last_name' => 'Jones'
 +    ),
 +    array(
 +        'id' => 5623,
 +        'first_name' => 'Peter',
 +        'last_name' => 'Doe'
     )     )
 ); );
Line 79: Line 84:
     [1] => Sally     [1] => Sally
     [2] => Jane     [2] => Jane
 +    [3] => Peter
 ) )
 </code> </code>
Line 111: Line 117:
 <code php> <code php>
 <?php <?php
-// Array representing a possible record set returned from a database +// Using the $records array from Example #1
-$records array+
-    array( +
-        'id' => 2135, +
-        'first_name' => 'John', +
-        'last_name' => 'Doe' +
-    ), +
-    array( +
-        'id' => 3245, +
-        'first_name' => 'Sally', +
-        'last_name' => 'Smith' +
-    ), +
-    array( +
-        'id' => 5342, +
-        'first_name' => 'Jane', +
-        'last_name' => 'Jones' +
-    ) +
-); +
 $lastNames = array_column($records, 'last_name', 'id'); $lastNames = array_column($records, 'last_name', 'id');
 print_r($lastNames); print_r($lastNames);
Line 142: Line 130:
     [3245] => Smith     [3245] => Smith
     [5342] => Jones     [5342] => Jones
 +    [5623] => Doe
 ) )
 </code> </code>
Line 213: Line 202:
     [0] => bar     [0] => bar
     [1] => garply     [1] => garply
 +)
 +*/
 +</code>
 +
 +=== Example #5: indexKey Collisions ===
 +
 +In the event that more than one row contains the same value for //**indexKey**//, then the last //**columnKey**// value for that //**indexKey**// will overwrite the previous value.
 +
 +<code php>
 +// Using the $records array from Example #1
 +$firstNames = array_column($records, 'first_name', 'last_name');
 +print_r($firstNames);
 +
 +/*
 +Array
 +(
 +    [Doe] => Peter
 +    [Smith] => Sally
 +    [Jones] => Jane
 ) )
 */ */
rfc/array_column.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1