PHP: Get n-th item of an associative array

2019-01-09 00:42发布

If you have an associative array:

Array
(
    [uid] => Marvelous
    [status] => 1
    [set_later] => Array
        (
            [0] => 1
            [1] => 0
        )

    [op] => Submit
    [submit] => Submit
)

And you want to access the 2nd item, how would you do it? $arr[1] doesn't seem to be working:

foreach ($form_state['values']['set_later'] as $fieldKey => $setLater) {
    if (! $setLater) {
        $valueForAll = $form_state['values'][$fieldKey];
        $_SESSION[SET_NOW_KEY][array_search($valueForAll, $form_state['values'])] = $valueForAll; // this isn't getting the value properly
    }
}

This code is supposed to produce:

$_SESSION[SET_NOW_KEY]['status'] = 1

But it just produces a blank entry.

6条回答
趁早两清
2楼-- · 2019-01-09 00:50

Here's a one line way to do it with array_slice and current

$value = current(array_slice($array, 1, 1)); // returns value only
查看更多
可以哭但决不认输i
3楼-- · 2019-01-09 00:51

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];
查看更多
ら.Afraid
4楼-- · 2019-01-09 00:51

Every one of the responses here are focused on getting the second element, independently on how the array is formed.

If this is your case.

Array
(
    [uid] => Marvelous
    [status] => 1
    [set_later] => Array
        (
            [0] => 1
            [1] => 0
        )

    [op] => Submit
    [submit] => Submit
)

Then you can get the value of the second element via $array['status'].

Also this code

foreach ($form_state['values']['set_later'] as $fieldKey => $setLater) {
    if (! $setLater) {
        $valueForAll = $form_state['values'][$fieldKey];
        $_SESSION[SET_NOW_KEY][array_search($valueForAll, $form_state['values'])] = $valueForAll; // this isn't getting the value properly
    }
}

I don't understand what are you trying to do, care to explain?

查看更多
Melony?
5楼-- · 2019-01-09 00:54

You can use array_slice to get the second item:

$a= array(
 'hello'=> 'world',
 'how'=> 'are you',
 'an'=> 'array',
);

$second= array_slice($a, 1, 1, true);
var_dump($second);
查看更多
劫难
6楼-- · 2019-01-09 00:59

If the array you provide in the first example corresponds to $form_state then

$form_state['values']['set_later'][1]

will work.

Otherwise

$i = 0;
foreach ($form_state['values']['set_later'] as $fieldKey => $setLater) {
    if ($i == 1) {
        $valueForAll = $form_state['values'][$fieldKey];
        $_SESSION[SET_NOW_KEY][$fieldKey] = $setLater;
        continue;
    }
    $i++;
}
查看更多
女痞
7楼-- · 2019-01-09 01:08

I am a bit confused. Your code does not appear to have the correct keys for the array. However, if you wish to grab just the second element in an array, you could use:

$keys = array_keys($inArray);
$key = $keys[1];
$value = $inArray[$key];

However, after considering what it appears you're trying to do, something like this might work better:

$ii = 0;
$setLaterArr = $form_state['values']['set_later'];
foreach($form_state['values'] as $key => $value) {
    if($key == 'set_later')
        continue;
    $setLater = $setLaterArr[$ii];
    if(! $setLater) {
        $_SESSION[SET_NOW_KEY][$key] = $value;
    }
    $ii ++;
}

Does that help? It seems you are trying to set the session value if the set_later value is not set. The above code does this. Instead of iterating through the inner array, however, it iterates through the outer array and uses an index to track where it is in the inner array. This should be reasonably performant.

查看更多
登录 后发表回答