简体   繁体   中英

get key and value from array in php

using this array data:

$tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
        );

i want to be able to get the key and value, i tried

$tokens["day"];

but its not returning anything.

how can i get each item but not inside a loop? (foreach for example)

That's because day is the value , not the key.

$tokens["86400"] is equal to day .

You need to rearrange your array so that 'day' => 86400 , not the other way around!

An easy way to do this is using array_flip .

If you want to access the key, via a known value, then you can simply flip the array with array_flip :

$flipped = array_flip($tokens);

echo $flipped['day']; //86400

Or, just create the array in the correct manner in the first place if you have access to the code that does so:

$tokens = array (
    'year'   => 31536000,
    'month'  => 2592000,
    'week'   => 604800,
    'day'    => 86400,
    'hour'   => 3600,
    'minute' => 60,
    'second' => 1
    );

$key = array_search('day', $tokens); You can get a index using value

$tokens["day"]; this will not returning any thing because 'day' is not index it is a value

You can get value of array using index like $token['86400'] this will return value "day"

Well, there are several options here.

  1. You can use a foreach loop
  2. You can use array search func passing the value and getting the key.
  3. You can use a for loop (but it's kinda tuff)

Let's start with the first one, the simplest:

$my_array = [
2334325 => "val_1",
4354524 => "val_2",
3213144 => "val_3",
3232412 => "val_4"
];

#here we use a "trick" for the foreach loop
foreach($my_array as $key => $val)
{
    echo "Key: $key <br/>Value: $val <br/><br/>";
}

With this loop you can access in a quick way keys and values.

Let's take a look to the second method, even if i suggest you to use the first one:

$my_array = [
2334325 => "val_1",
4354524 => "val_2",
3213144 => "val_3",
3232412 => "val_4"
];

#let's make it dynamic. We create a function

function arrSrc($arg)
{
    #we declare a index
    $index;

    #we search the index by given value
    $index = array_search($arg);

    return $index;
}

echo arrSrc("val_1");

For the rest i don't want to go too deep even with the for loop. But i think that this can help you.

-Keep on hacking!

You must interchange the array key and values using array_flip . It is the easy way to get the answer.

$flipped_array = array_flip($tokens);
echo $flipped_array['day'];

Result is: 86400

There are so many solutions already available, so i am adding my solution here. You can also use array_keys() as:

$tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
        );

$arrayKeys = array_keys($tokens);
$requiredVal = $tokens[$arrayKeys[3]];
echo $requiredVal; // result "day"

Explanation:

Use array_keys function for getting keys and than use in original array as $tokens[$arrayKeys[3]] because this index have your required value ( day ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM