0

I am trying to loop through an array to take certain values and set them equal to a variable.

Here is my var_dump on my $_POST array

array(5) {
    ["voter"]=> string(2) "22"
    [1]=> string(1) "1"
    [2]=> string(1) "2"
    [3]=> string(1) "3"
    ["vote"]=> string(4)
)

I want the keys out of the key => value pair from the 2nd key value pair on out and then set that to a variable. How would I achieve this?

So from [1] -> string(1) "1" on out..Ignore the first pair.

6
  • So you want to skip voter and capture the rest? Commented Aug 9, 2011 at 15:14
  • 2
    Can you clarify? You want to set some variables to some values in an array using a foreach? Why not just use that array? Commented Aug 9, 2011 at 15:14
  • Do you want to skip just the first key, or only work on the second key? I'm not following you entirely, sorry :) Commented Aug 9, 2011 at 15:15
  • You mean $second_value = $your_array[1];? Commented Aug 9, 2011 at 15:16
  • I am trying to set my sql statement $sql_candidate = "select id, prefix, fname, lname, suffix, city, state, zip, bio, votes from stateChair_nominees where id="; I want the id to be equal to a variable that captures all of those keys. Basically everything after the first key. So my sql statement would be equal to id=1 id=2 id=3.. make sense? Commented Aug 9, 2011 at 15:18

3 Answers 3

1

Using the method provided by @Xeon06 will certainly work, but will require the $_POST data to be in the order you provided, and if that order changes, so will the results. This method does not care about the order.

function ext($array, array $keys, $default = NULL)
{
    $found = array();
    foreach ($keys as $key)
    {
        $found[$key] = isset($array[$key]) ? $array[$key] : $default;
    }
    return $found;
}

$keys = array(1, 2, 3, 'vote');
$my_vars = ext($_POST, $keys);

function ext($array, array $keys, $default = NULL) {
    $found = array();
    foreach ($keys as $key) {
        $found[$key] = isset($array[$key]) ? $array[$key] : $default;
    }
    return $found;
}

$_POST = array('voter' => 'TEST', 1 => 'ONE', 2 => 'TWO', 3 => 'THREE', 'vote' => 'HAMBURGER'); 
$keys = array(1, 2, 3, 'vote');
$my_vars = ext($_POST, $keys);
print_r($my_vars);

OUTPUT
Array

(
    [1] => ONE
    [2] => TWO
    [3] => THREE
    [vote] => HAMBURGER
)
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not 100% sure of what you're attempting to do, but this will give you an array containing all values but the first.

$vals = array_values(array_slice($_POST, 1));

The array_values part is to reset the indexes of your array, so that accessing $vals with [0] will return "1".

Comments

0
<?php

$str = '';

$arr = array(
    'voter' => '22',
    1 => '1',
    2 => '2',
    3 => '3', 
    'vote' => 'smth',
);

$arr = array_slice($arr, 1);

foreach($arr as $i) {
    $str .= 'id=' . $i . ' ';
}

echo $str; // id=1 id=2 id=3 id=smth

I have no idea how would you use it in a single sql query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.