0% found this document useful (0 votes)
24 views

Lesson 1

Uploaded by

Krzysiek Ka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lesson 1

Uploaded by

Krzysiek Ka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

wtorek, 10 stycznia 2023

Tytuł

Temat

- Etiam sit amet est

• Aenean iaculis laoreet arcu

• Curabitur vulputate viverra pede

• Nulla rutrum commodo ligula

• Sed tellus suscipit in aliquam

- Nunc ut lectus

- Donec quis nunc

- Sdd

- Sd

- Sdsd

- Sd bhttps://www.scribd.com/document/381074787/Sprawdzian-SoleLorem
ipsum – Wikipedia, wolna encyklopedia

https://pl.wikipedia.org › wiki › Lorem_ipsum

Lorem ipsum – tekst składający się z łacińskich i quasi-łacińskich wyrazów, mający korzenie
w klasycznej łacinie, wzorowany na fragmencie traktatu Cycerona ...

• Downloads
• Documentation
• Get Involved
• Help

PHP 8.0.27 Released!


array_key_exists »
« array_intersect
• PHP Manual
• Function Reference
• Variable and Type Related Extensions
• Arrays

1
• Array Functions
Change language:
Submit a Pull Request Report a Bug
array_is_list
(PHP 8 >= 8.1.0)

array_is_list — Checks whether a given array is a list

Description ¶

array_is_list(array $array): bool


Determines if the given array is a list. An array is considered a list if its keys consist of
consecutive numbers from 0 to count($array)-1.

Parameters ¶

array
The array being evaluated.

Return Values ¶

Returns true if array is a list, false otherwise.

Examples ¶

Example #1 array_is_list() example

<?php

array_is_list([]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list([0 => 'apple', 'orange']); // true

// The array does not start at 0


array_is_list([1 => 'apple', 'orange']); // false

// The keys are not in the correct order


array_is_list([1 => 'apple', 0 => 'orange']); // false

// Non-integer keys
array_is_list([0 => 'apple', 'foo' => 'bar']); // false

// Non-consecutive keys

2
array_is_list([0 => 'apple', 2 => 'bar']); // false
?>
Notes ¶

Note:

This function returns true on empty arrays.

See Also ¶

• array_values() - Return all the values of an array


+add a note
User Contributed Notes 5 notes

up
down
32
phpnet at jesseschalken dot com ¶
11 months ago
Polyfills that call `array_keys()`, `array_values()` or
`range()` are inefficient because they create new arrays
unnecessarily.

Please use this polyfill instead which creates no new


arrays and only does a single pass over the given array.

<?php

if (!function_exists("array_is_list")) {
    function array_is_list(array $array): bool
    {
        $i = 0;
        foreach ($array as $k => $v) {
            if ($k !== $i++) {
                return false;
            }
        }
        return true;
    }
}

?>
up
down

3
11
divinity76+spam at gmail dot com ¶
8 months ago
slightly optimized version of phpnet at jesseschalken dot
com's excellent array_is_list:

<?php

if (!function_exists("array_is_list")) {
    function array_is_list(array $array): bool
    {
        $i = -1;
        foreach ($array as $k => $v) {
            ++$i;
            if ($k !== $i) {
                return false;
            }
        }
        return true;
    }
}

?>

benchmarks: https://3v4l.org/9BPqL

why is this faster you ask? because post-increment does


more,

here is what pre-increment actually means:


step 1: increment the value by 1.
step 2: return the value.

here is what post-increment actually means:


step 1: create a copy of the original value.
step 2: increment the original value by 1.
step 3: return the copy.

another question might be "why didn't you write `if ($k !


== ++$i) {` ?  ... that is a good question! turns out
that ++$i;if($k!==$i){...} is faster on PHP7 than if($k !
== ++$i){...} for reasons unknown to me.. (if you have an
answer, feel free to email me about it!)

4
(i have NOT checked if PHP8/jit auto-optimize this stuff,
but at least back in PHP7 it's true that pre-increment is
faster than post-increment, and this polyfill is
primarily for PHP7)
up
down
15
Matteo Galletti ¶
1 year ago
Polyfill implementation for PHP versions lower than 8.1.

<?php
if (!function_exists('array_is_list'))
{
    function array_is_list(array $a)
    {
        return $a === [] || (array_keys($a) === range(0,
count($a) - 1));
    }
}
?>
up
down
0
info at ensostudio dot ru ¶
1 year ago
old school polyfill (:
<?php
if (!function_exists('array_is_list')) {
    function array_is_list(array $array)
    {
        if ($array === []) {
             return true;
        }
        $keys = array_keys($array);
        return $keys === array_keys($keys);
    }
}
?>
up
down
-9
iradu at unix-world dot org ¶
11 months ago

5
// How about:

<?php

if(count(array_filter(array_keys($y_arr), 'is_string'))
=== 0) {
    echo 'non-associative ; is list';
} else {
    echo 'associative ; non list'
}
+add a note
• Array Functions
◦ array_ change_ key_ case
◦ array_ chunk
◦ array_ column
◦ array_ combine
◦ array_ count_ values
◦ array_ diff_ assoc
◦ array_ diff_ key
◦ array_ diff_ uassoc
◦ array_ diff_ ukey
◦ array_ diff
◦ array_ ll_ keys
◦ array_ ll
◦ array_ lter
◦ array_ ip
◦ array_ intersect_ assoc
◦ array_ intersect_ key
◦ array_ intersect_ uassoc
◦ array_ intersect_ ukey
◦ array_ intersect
◦ array_ is_ list
◦ array_ key_ exists
◦ array_ key_ rst
◦ array_ key_ last
◦ array_ keys
◦ array_ map
◦ array_ merge_ recursive
◦ array_ merge
◦ array_ multisort
◦ array_ pad
◦ array_ pop
◦ array_ product
◦ array_ push
◦ array_ rand
◦ array_ reduce
◦ array_ replace_ recursive

6
fl
fi
fi
fi
fi
◦ array_ replace
◦ array_ reverse
◦ array_ search
◦ array_ shift
◦ array_ slice
◦ array_ splice
◦ array_ sum
◦ array_ udiff_ assoc
◦ array_ udiff_ uassoc
◦ array_ udiff
◦ array_ uintersect_ assoc
◦ array_ uintersect_ uassoc
◦ array_ uintersect
◦ array_ unique
◦ array_ unshift
◦ array_ values
◦ array_ walk_ recursive
◦ array_ walk
◦ array
◦ arsort
◦ asort
◦ compact
◦ count
◦ current
◦ end
◦ extract
◦ in_ array
◦ key_ exists
◦ key
◦ krsort
◦ ksort
◦ list
◦ natcasesort
◦ natsort
◦ next
◦ pos
◦ prev
◦ range
◦ reset
◦ rsort
◦ shuf e
◦ sizeof
◦ sort
◦ uasort
◦ uksort
◦ usort
• Deprecated
◦ each
• Copyright © 2001-2023 The PHP Group

7
fl
• My PHP.net
• Contact
• Other PHP.net sites
• Privacy policy
-

You might also like