Get the Index of a Certain Value in an Array in PHP

Get the index of a certain value in an array in PHP

array_search is the way to do it.

array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : mixed

From the docs:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;

You could loop over the array manually and find the index but why do it when there's a function for that. This function always returns a key and it will work well with associative and normal arrays.

Get index of value from an array in php

Try this -

$tempStyleArray = array_map('trim', preg_split( "/[:;]+/", "width: 569px; height: 26.456692913px; margin: 0px; border: 2px solid black;" ));
var_dump($tempStyleArray);
$key = array_search('height', $tempStyleArray);
echo $key;

It was happening because there was space with the values in array. So needed to be trimmed. After splitting the string every value will be passed through the trim() so that the white spaces are removed.

Get the index of the same value of a array php

I SUPER would never use this "variable variables" technique in a professional project, but it does satisfy your brief. I will urge you to find the folly in your XY Problem.

Effectively, the snippet below will synchronously iterate over the two related input arrays and create variably-named result arrays to push values into.

Code: (Demo)

$one_array_key = ['A', 'A', 'A', 'B', 'B', 'B'];
$second_array_values = ['arn1', 'arn2', 'arn3', 'arn4', 'arn5', 'arn6'];

foreach ($one_array_key as $index => $value) {
$$value[] = $second_array_values[$index];
}

var_export($A);
echo "\n---\n";
var_export($B);

Output:

array (
0 => 'arn1',
1 => 'arn2',
2 => 'arn3',
)
---
array (
0 => 'arn4',
1 => 'arn5',
2 => 'arn6',
)

It makes much more sense to have a statically named variable containing keys and related subarrays. This way you can call array_keys() on the result, if you wish, to find out which groups were found in the original input.

Code: (Demo)

$result = [];
foreach ($one_array_key as $index => $value) {
$result[$value][] = $second_array_values[$index];
}
var_export($result);

Output:

array (
'A' =>
array (
0 => 'arn1',
1 => 'arn2',
2 => 'arn3',
),
'B' =>
array (
0 => 'arn4',
1 => 'arn5',
2 => 'arn6',
),
)

PHP - Getting the index of a element from a array

You should use the key() function.

key($array)

should return the current key.

If you need the position of the current key:

array_search($key, array_keys($array));

PHP - Get Array value by Index instead of Key

Try:

foreach ($array as $key => $value) {
echo "Key: $key";
}

Where $array is the array you want to loop through.

It will print out all the keys

Get index of row with qualifying value from a 2d array

If you have a 2D array, like in your example, you'll need to customise things a little:

function array_search2d($needle, $haystack) {
for ($i = 0, $l = count($haystack); $i < $l; ++$i) {
if (in_array($needle, $haystack[$i])) return $i;
}
return false;
}

$myArray = array(
array( 'username' => 'user1' ),
array( 'username' => 'user2' )
);
$searchTerm = "user1";

if (false !== ($pos = array_search2d($searchTerm, $myArray))) {
echo $searchTerm . " found at index " . $pos;
} else {
echo "Could not find " . $searchTerm;
}

If you wanted to search in just one particular field, you could alter the function to something like this:

function array_search2d_by_field($needle, $haystack, $field) {
foreach ($haystack as $index => $innerArray) {
if (isset($innerArray[$field]) && $innerArray[$field] === $needle) {
return $index;
}
}
return false;
}

getting value of array from certain index in php

If $status defined as a string, you can return php value of it with eval function like below:

$status = '[1,2]';
$status_array = eval('return ' . $status . ';');
$task_state = $status_array[0];

Caution
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

php get array index of object having specific key value

You can do a combination of array_column() and array_search(). Have a look here.

$records = [
[
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
],
[
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
],
[
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
],
[
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
]
];
$key = array_search(3245, array_column($records, 'id'));
echo $key;

How to find index of an array when I know only part of string?

Use foreach() to make an iteration over the array and strpos() for searching your needle in the elements of the array.

$array = [0 => 'blue pants', 1 => 'red pants', 2 => 'green pants', 3 => 'green pants'];

foreach ($array as $key => $value) {
if (strpos($value, 'red') !== false) {
echo "Key={$key}, Value: {$value}";
break;
}
}

Working demo.



Related Topics



Leave a reply



Submit