Ads removed from the pastie. \o/
Running costs stayed. :-(
Donations welcome!

Bitcoin: 1QC38uPBSy7WSYz1whM5SYTHAdhqkN6o8V
Ethereum: 0x0855778a6181a69cc7d5d08c71c4f5846010eb67
Verge: D8fo1fszrNmUK4kfQW5owhYMv3osPvgn2g
Recent entries
2020-07-12 18:31:33
Yellowstone 3x4 ~ New ser...
2020-07-12 18:31:08
ginkenyo
2020-07-12 18:30:45
itorioku
2020-07-12 18:30:25
rihiyozoku
2020-07-12 18:30:15
goyuzaoto
2020-07-12 18:27:05
https://www.residentadvis...
2020-07-12 18:24:56
tnesneseswtswt
2020-07-12 18:21:07
https://www.justgiving.co...
2020-07-12 18:17:12
https://www.residentadvis...
2020-07-12 18:13:16
ert rttyuiu
Permanent entries
2014-10-14 21:12:28
Boost property tree - Ite...
2013-10-18 08:24:21
Resize images in a folder...
2013-04-29 17:23:35
Find bad words in a text,...
2013-02-12 20:26:45
Merge two boost property ...
2012-12-18 16:21:40
OpenCV: Access/modify ima...
2012-11-19 10:48:40
Reading a boost::property...
2012-04-10 00:11:46
Convert hex to string or ...
2012-02-18 00:12:04
Screw up []-operator for ...
2012-02-16 03:02:24
Merge continuous numbers ...
2012-01-18 22:26:01
(PHP) Generate / Create (...
2011-04-19 16:05:25
Calculate one's age in ye...
2011-04-18 15:26:45
(PHP) Generate / Create r...
2010-08-26 15:18:00
Pick the first element (k...
2010-07-28 10:36:45
Quakenet - Snailbot comma...
2010-06-21 21:30:52
Using the MATLAB profiler...
2010-05-31 06:31:14
PHP truncation function
2010-05-06 20:49:26
Quakenet - Fishbot comman...
Paste ID: # 50c7c
Date posted: Thu, 26 Aug 2010 15:18:00 +0000
Date of expiration: never (permanent entry)
Description:

Pick the first element (key, value) of an associative array (i. e. using keys of type string) in PHP. Helpful when you didn't know the key to get the value.

  1. <?php
  2. /**PHP holds a wide variety of ways to get the key-value pair of
  3.  * array items. They are shown down below.
  4.  * This list is not intended to be exhaustive.
  5.  */
  6. // The sample array
  7. $array = array( "foo" => "bar", "b" => "baz", "c" => false, "d" => 31337 );
  8.  
  9. /*******************************************************************
  10.  * EITHER ...
  11.  *******************************************************************/
  12. // Reset internal pointer to the beginning and gain the value
  13. $value = reset( $array );
  14.  
  15. // Get key
  16. $key = key( $array );
  17.  
  18. // Another way to get the value when the pointer is at the beginning
  19. $value = current( $array );
  20.  
  21. /*******************************************************************
  22.  * OR ...
  23.  *******************************************************************/
  24. // Slice array's first element
  25. $first = array_slice( $array, 0, 1, true );
  26.  
  27. // Get key ...
  28. $key = key( $first );
  29.  
  30. // ... and value
  31. $value = current( $first );
  32.  
  33. /*******************************************************************
  34.  * AND ALSO ...
  35.  *******************************************************************/
  36. // Get key ...
  37. $key = array_shift( array_keys( $array ));
  38.  
  39. // ... and value
  40. $value = array_shift( array_values( $array ));
  41.  
  42. /*******************************************************************
  43.  * AND NOT TO FORGET THE MOST COMMON ...
  44.  *******************************************************************/
  45. // Get key ...
  46. $keys = array_keys( $array );
  47. $key = $keys[0];
  48. // PHP >= 5.5 shorthand
  49. $key = array_keys( $array )[0];
  50.  
  51. // ... and value
  52. $values = array_values( $array );
  53. $value = $values[0];
  54. // PHP >= 5.5 shorthand
  55. $value = array_values( $array )[0];
  56.  
  57. /*******************************************************************
  58.  * AND OF COURSE ...
  59.  *******************************************************************/
  60. $key = $value = ''; // Not really required (in PHP)
  61. // Get key and value concurrently
  62. foreach( $array as $key => $value )
  63.   break;  // We already have what we want
  64.  
  65. /*******************************************************************
  66.  * AND YET ANOTHER WAY ...
  67.  *******************************************************************/
  68. // If interal pointer is not at the beginning, reset() it beforehand
  69. // then get key and value concurrently
  70. list( $key, $value ) = each( $array );
  71.  
  72. /*******************************************************************
  73.  * If you need the value of the first element only and have an array
  74.  * with NUMERIC index there is - you know it - one more way to go
  75.  *******************************************************************/
  76. list( $value ) = $array; // works with NUMERIC indexes only
  77. ?>
You can paste a correction or amendment to the upper paste or a new paste down below.