Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #18389
| From | "J.O. Aho" <user@example.net> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: value returned instead change the value of the variable |
| Date | 2020-10-16 20:51 +0200 |
| Message-ID | <huu8d0Ft5ceU1@mid.individual.net> (permalink) |
| References | <rmbvqm$lnh$1@gioia.aioe.org> |
On 16/10/2020 13.23, alex wrote: > // DESIGN 1 > echo "It doesn't work (return 1 vs array), but I like 'DESIGN 1' "; > $arr = [1, 2]; > print_r( > array_shift($arr) > ); > > Output: > > It doesn't work (return 1 vs array), but I like 'DESIGN 1': 1 > If you look at your original array, the cell 0 has an integer, so the return type will be int. See https://www.php.net/array_shift it say's: Return Values Returns the shifted value, or NULL if array is empty or is not an array. If you prompt want an array, either you make it to one afterwards or store the values as single cell arrays in the main array Alt 1: array(array_shit($arr)); Alt 2: $arr = [[1],[2]]; > // DESIGN 2 > echo "It works, but I don't like 'DESIGN 2': "; > $arr = [1, 2]; > array_shift($arr); > print_r( > $arr > ); > > Output: > > It works, but I don't like 'DESIGN 2': Array > ( > [0] => 2 > ) Sure we compare apples and oranges, the $arr will be an array even if you shift out the last value from it. try this: $arr = [1, 2]; array_shift($arr); print_r($arr); array_shift($arr); print_r($arr); echo "This will confuse you even more\n"; print_r(array_shift($arr)); -- //Aho
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-16 13:23 +0200
Re: value returned instead change the value of the variable Jerry Stuckle <jstucklex@attglobal.net> - 2020-10-16 11:11 -0400
Re: value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-16 18:36 +0200
Re: value returned instead change the value of the variable "J.O. Aho" <user@example.net> - 2020-10-16 20:51 +0200
Re: value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-17 09:26 +0200
Re: value returned instead change the value of the variable "J.O. Aho" <user@example.net> - 2020-10-17 10:16 +0200
csiph-web