Web Analytics Made Easy -
StatCounter array_combine modification - CodingForum

Announcement

Collapse
No announcement yet.

array_combine modification

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • array_combine modification

    When I print_r($_POST) to determine what's being posted from my form, I get this:

    PHP Code:
    Array ( [name] => Array ( [0] => Joe [1] => [2] => [3] => [4] => [5] => ) [animal] => Array ( [0] => Dog  [1] => Cat [2] => [3] => [4] => [5] => ) [submit] => Validez 
    Then, I array_combine to (duh) combine the arrays, and I get this:

    PHP Code:
    Array ( [Joe] => Dog [] => ) 

    I'd like to get this:

    PHP Code:
    Array ( [Joe] => Dog [] => Cat
    Anyone know how I can accomplish this?

  • #2
    If I understand you right, you want to combine the elements in the array assigned to $_POST['name'] and the elements in the array assigned to $_POST['animal'], so that the $_POST['name'] elements become the keys and $_POST['animal'] elements become the values.

    Is that right?

    I think array_combine will work for you. You said you tried it but you didn't show us the code you used.
    PHP Code:
    if (is_array($_POST['name']) && is_array($_POST['animal'])) {
        
    $newArray array_combine($_POST['name'], $_POST['animal']);

    That should work fine.

    p.s. Wrapping print_r() in <pre> tags makes it readable.
    PHP Code:
    print "<pre>\n";
    print_r($_POST);
    print </
    pre>\n"; 

    Comment

    Working...
    X