3

I have the following loop:

 foreach($_POST as $key => $value) {

           echo "Key:" . $key . " Value: ". $value .   "<br />";         

           }

which produce the following result:

Key:1 Value: my value Key:8 Value: my some other value Key:9 Value: another value

What I am trying to do is to create an array that would look like this:

$editWhat = array(
                'field1'            => $key1,
                'field2'            => $key2,
                'field3'            => $key3,
                'field4'        => $value1,
                'field5'        => $value2,
                'field6'        => $value3
            ); 

Comma should be stripped in the last value pair line in the array, which cause me additional problems.

Any help will be deeply appreciated.

Regards, John

4
  • Are you trying to create an array with those key, value pairs, or are you simply trying to print them to look like an associative array? Commented Sep 23, 2013 at 14:02
  • please clearify your question Commented Sep 23, 2013 at 14:05
  • I am trying to create an array Commented Sep 23, 2013 at 14:06
  • Okay, then the answers below are correct. I just wasn't sure, because the code you posted is all about printing out the array. Commented Sep 23, 2013 at 14:07

7 Answers 7

3
$editWhat = array();
foreach($_POST as $key => $value)
    $editWhat['field'.(count($editWhat)+1)] = $key;
foreach($_POST as $value)
    $editWhat['field'.(count($editWhat)+1)] = $value;

will do exactly what you described. If you don't need the keys of the array you can do:

$editWhat = array_merge(array_keys($_POST), array_values($_POST));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. That was exactly what I needed. I will accept your answer in 40 seconds :-)
To the one how downvoted me: can you explain why? Is there a mistake or am I missing something?
2

Maybe I misunderstand but try this to get your desired array:

$editWhat = array();
$count = count($_POST);
foreach($_POST as $key => $value) {
    $editWhat['field'.($key+1)] = $key;
    $editWhat['field'.($key+$count+1)] = $value;
}

1 Comment

add a second loop storing the values and you get my +1 ;)
0

Just set the array values to the corresponding key within the loop:

$editWhat = array();

foreach($_POST as $key => $value) {

    $editWhat[$key] = $value;  

}

see http://php.net/manual/en/language.types.array.php

1 Comment

Hi Louis... I get $editWhat = array( 'key1' => $value1, 'key2' => $value2, ); which is not I want...take a look on my question
0
$editWhat=array();

foreach($_POST as $key => $value) {

           $editWhat[$key] = $value;         

           }

Comments

0

You can use array_keys() and array_values() functions to loop through keys and values of array and using two separate loops construct a new array.

Comments

0

Try this.

foreach($_POST as $key=>$value){
    $keys[]=$key;
    $values[]=$value;
}
foreach($keys as $editKey){
    $editWhat[]=$editKey;
}
foreach($values as $editValue){
    $editWhat[]=$editVlaue;
}

1 Comment

why do you store the keys and values before you loop over them? You could use array_values() and array_keys() or simply loop over the $_POST and directly insert into $editWhat.
0
 $array = array();
$n = 1;
foreach($_POST as $key => $value) {
      $array["field".$n] = $key; 
      $n++;
 }

foreach($_POST as $key => $value) { 
      $array["field".$n] = $value; 
      $n++; 
}

print_r($array);

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.