views:

287

answers:

5

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.

+1  A: 

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);
pygorex1
+1  A: 

Use array_slice

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

// or

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

// or

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

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++;
}
Elise van Looij
A: 

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.

Perry Munger
A: 

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?

metrobalderas