PHP - Get Key Name of Array Value

PHP - Get key name of array value

If you have a value and want to find the key, use array_search() like this:

$arr = array ('first' => 'a', 'second' => 'b', );
$key = array_search ('a', $arr);

$key will now contain the key for value 'a' (that is, 'first').

PHP Get Name of Associative Array from Key Value

What you're lookin for is array_search(), that provides the functionality you're looking for:

$key = array_search(str_replace("LP", "", $country), $yourArray);

Which returns: Singapore

If you're looking for a quick and dirty way to return the data (not recommended), then you could even do this:

echo array_flip($a)[str_replace("LP", "", $country)];

Provided the data is always present

How to get an array of specific key in multidimensional array without looping

Since PHP 5.5, you can use array_column:

$ids = array_column($users, 'id');

This is the preferred option on any modern project. However, if you must support PHP<5.5, the following alternatives exist:

Since PHP 5.3, you can use array_map with an anonymous function, like this:

$ids = array_map(function ($ar) {return $ar['id'];}, $users);

Before (Technically PHP 4.0.6+), you must create an anonymous function with create_function instead:

$ids = array_map(create_function('$ar', 'return $ar["id"];'), $users);

How to get the key name in PHP?

Read foreach manual

There are two syntaxes for foreach

foreach (iterable_expression as $value)
statement

foreach (iterable_expression as $key => $value)
statement

From php.net,

The first form traverses the iterable given by iterable_expression. On each iteration, the value of the current element is assigned to $value.

The second form will additionally assign the current element's key to the $key variable on each iteration.

I would rewrite your code like this

printEach($movies);

function printEach($array) {
echo "<br><br>";
foreach ($array as $key => $value) {
echo $key;
if (is_array($value)) {
printEach($value); // Recursive
} else {
echo " - ".$value."<br><br>";
}
}
}

get key value for each single array element

You can print key name like this:

$arr = ['night' => 'black', 'sun' => 'light', 'she' => 'gold'];

foreach ($arr as $key => $el) {
echo $key. ' <br>';
}

Output:

night 
sun
she

Get array value with unknown key name

You can use current():

$value = current($array);

or, if you want the key as well, each():

list($key, $value) = each($array);

How to get array key from corresponding array value?

You could use array_search() to find the first matching key.

From the manual:

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

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

How to get keys name from array in laravel

You can make use of array_keys to get all attributes from a model.

$users = Users::all();
$user = $users->first();
$attributes = array_keys($user->toArray());

Alternatively you can use this approach to get the column names based from a certain table in your database.

PHP array's, how to access keys and values

Try this -

$newarray = array();
foreach($shop as $key=>$value) {
$newarray[$key]['Title'] = $value['Title'];
$newarray[$key]['Number'] = $value['Number'];
}
echo "<pre>";print_r($newarray);

Here, $newarray will give you output like this.

Array
(
[0] => Array
(
[Title] => rose
[Number] => 15
)

[1] => Array
(
[Title] => daisy
[Number] => 25
)

[2] => Array
(
[Title] => orchid
[Number] => 7
)

)

Get value from php array that have the same key name

you can create a function for that.

function custom_search($search_for='',$search_in=array())
{
if($search_for=='' OR empty($search_in)) return '';
foreach($search_in as $val)
{
if($val['name']==$search_for) {return $val['value'];}
}
return '';
}
$telephone=custom_search("infotel",$array);


Related Topics



Leave a reply



Submit