Get the first element of an array
Original answer, but costly (O(n)):
array_shift(array_values($array));
In O(1):
array_pop(array_reverse($array));
Other use cases, etc...
If modifying (in the sense of resetting array pointers) of $array
is not a problem, you might use:
reset($array);
This should be theoretically more efficient, if a array "copy" is needed:
array_shift(array_slice($array, 0, 1));
With PHP 5.4+ (but might cause an index error if empty):
array_values($array)[0];
How to get the first element of an array?
like this
alert(ary[0])
Get the first element of System.array?
IndexOf
is for finding the index of the first element in the array that matches the value you passed. I think you're after [0]
.
String windowClass = BFS.Window.GetClass(myArray[0]);
Please note that if the array is empty this will throw an exception.
You could also use .First()
or FirstOrDefault()
from System.Linq
, but [0]
is made for what you need.
How to get the first element of a array?
I'm not familiar with pm2
, but if the array is JSON-compatible (which it looks like it is), you can use jq
, for example:
$ echo '[2]' | jq '.[0]'
2
$ echo '[3, 2]' | jq '.[0]'
3
Here's a related question with some other methods: get the first (or n'th) element in a jq json parsing
Get index from 2d array where first element is the index
One approach is to use next
on a generator expression
lst = [[0, {'dt_name': 'Go'}], [1, {'dt_name': 'Stop'}]]
res = next(i for (i, t) in lst if t['dt_name'] == 'Go')
print(res)
Output
0
This approach avoids building any additional list.
Some timings on list vs generator:
lst = [[0, {'dt_name': 'Go'}], [1, {'dt_name': 'Stop'}]] * 100
%timeit next(i for (i, t) in lst if t['dt_name'] == 'Go')
452 ns ± 2.18 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit [i for (i, t) in lst if t['dt_name'] == 'Go'][0]
12.1 µs ± 36.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In the above example using next on the generator is about 24 times faster.
Why does the first element outside of a defined array default to zero?
I'm a bit confused as to why that last element outside of the array
always "defaults" to zero.
In this declaration
int x[] = {120, 200, 16};
the array x
has exactly three elements. So accessing memory outside the bounds of the array invokes undefined behavior.
That is, this loop
for (int i = 0; i < 4; i++)
cout << x[i] << " ";
invokes undefined behavior. The memory after the last element of the array can contain anything.
On the other hand, if the array were declared as
int x[4] = {120, 200, 16};
that is, with four elements, then the last element of the array that does not have an explicit initializer will be indeed initialized to zero.
Related Topics
Fatal Error: Maximum Execution Time of 30 Seconds Exceeded
How to Create a Pdo Parameterized Query With a Like Statement
Pdo Prepared Inserts Multiple Rows in Single Query
How to Generate Xml File Dynamically Using PHP
What Are the "Standard" Timezone Abbreviations
How to Change the Session Timeout in PHP
Parse Xml With Namespace Using Simplexml
How to Generate a Random, Unique, Alphanumeric String
Convert PHP Array to JavaScript
How to Get File_Get_Contents() to Work With Https
Illegal String Offset Warning PHP
Reading/Writing a Ms Word File in PHP
Automatically Deleting Related Rows in Laravel (Eloquent Orm)