Php: Get N-Th Item of an Associative Array

PHP: Get n-th item of an associative array

Use array_slice

$second = array_slice($array, 1, 1, true);  // array("status" => 1)

// or

list($value) = array_slice($array, 1, 1); // 1

// or

$blah = array_slice($array, 1, 1); // array(0 => 1)
$value = $blah[0];

Get nth key of associative php array

array_keys produces a numerical array of an array's keys.

$keys = array_keys($array);
$key = $keys[1];

If you're using PHP 5.4 or above, you can use a short-hand notation:

$key = array_keys($array)[1];

PHP - how to get nth(first, second, etc) key and value for array?

You can store all the keys and values in variables using array_keys and array_values i.e.,

$keys = array_keys($test);
$values = array_values($test);

And then, use them as follows:

echo 'first array key is ' . $keys[0]
echo 'first array value is ' . $values[0]

How to access N-th element of an array in PHP

If your keys are numeric, then it works exactly the same:

$arr = ['one', 'two', 'three']; // equivalent to [0 => 'one', 1 => 'two', 2 => 'three']
echo $arr[1]; // two

If your keys are not numeric or not continuously numeric, it gets a bit trickier:

$arr = ['one', 'foo' => 'bar', 42 => 'baz'];

If you know the key you want:

echo $arr['foo']; // bar

However, if you only know the offset, you could try this:

$keys = array_keys($arr);
echo $arr[$keys[1]];

Or numerically reindex the array:

$values = array_values($arr);
echo $values[1];

Or slice it:

echo current(array_slice($arr, 1, 1));

Most likely you want to be looping through the array anyway though, that's typically what you do with arrays of unknown content. If the content is unknown, then it seems odd that you're interested in one particular offset anyway.

foreach ($arr as $key => $value) {
echo "$key: $value", PHP_EOL;
}

How to get the i'th element of an associative array?

This should work for you:

Here you just can use array_keys() to access the keys of your associative array as a numerical indexed array, which you then again can use as key for the array.

$arr = array(2=>'0',3=>'1',5=>'3',4=>'4',7=>'2');
echo $arr[array_keys($arr)[3]];
//^ This is your 'i'

output:

4

when the solution is a[i] <- You can't get it that simple, but you get close with the solution above. (Note, that since the array with the keys is 0-based index, the 4th element is index 3)

You can use a variable and then subtract one, to get your logic, that 4 => 4.

$i = 4;
$arr = array(2=>'0',3=>'1',5=>'3',4=>'4',7=>'2');
echo $arr[array_keys($arr)[$i-1]];

PHP get first and forth value of an associative array

use array_keys?

$keys = array_keys($your_array);

echo $your_array[$keys[0]]; // 1st key
echo $your_array[$keys[3]]; // 4th key

How to get first n keys in associative array in PHP?

Sort the array in reverse order keeping association with arsort(). Then take a slice of the array (first three elements).

arsort( $scores );
$topScores = array_slice( $scores, 0, 3 );

You can then use implode to generate a string from the sliced array.


Rizier123 pointed out you wanted the keys in the string, so you'd need to implode the keys. Something like

$topScoresStr = implode( ', ', array_keys( $topScores ) );

Return first n values of associative PHP array?

You need to use array_slice() for slice your array at a fixed length and then use array_keys.

$length = 50;
$start = 0;
$new_arr = array_slice($engagement, $start, $length);
$keys = array_keys($new_arr);
foreach($keys as $key){
echo $new_arr[$keys[$key]];
}

Without the array_keys:

foreach($new_arr as $key => $value){
echo $key." => ".$value;
}

Remember to use true when you don't use the array_keys like:

$new_arr = array_slice($engagement, $start, $length, true);

More about: array_slice

If you don't use the true the slice array index will be reset and you
lose the keys.

Updates:

As you are asking for only the array keys, By default array_slice reset the array keys, if you want to preserve then you need to use true as the forth parameter of array_slice.

$new_arr = array_slice($engagement, $start, $length, true);
$keys = array_keys($new_arr); // here is the list of the keys


Related Topics



Leave a reply



Submit