php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49671 Change to first argument of array_map.
Submitted: 2009-09-25 18:56 UTC Modified: 2010-11-24 11:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: whatchildisthis at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.0 OS: Windows 2003
Private report: No CVE-ID: None
 [2009-09-25 18:56 UTC] whatchildisthis at gmail dot com
Description:
------------
Based on the rise of web based applications, it is common to have applications that allow a user to insert an indefinite number of rows of data (such as an invoice, contact list).

It would be great to see array_map take an array of keys as the first argument, also, so that it does the same thing as if the first argument was null, but uses the first array as the keys of the maped arrays, much like array_combine being called on each maped array after. This recycles an already existing function to do something I thought it would already be able to do without the callback.

PHP DATA GENERATED FROM HTML FORM OF AN INVOICE:
$_POST["Invoice"] = array(
  "Description" => array(
    "A widget",
    "A gadget"
  ),
  "Price" => array(
    '$1.00',
    '$3.00'
  ),
  "Units" => array(
    2,
    3
  )
);

NEW PHP CODE:
array_map(
  array("Description", "Price", "Units"),
  $_POST["Invoice"]["Description"],
  $_POST["Invoice"]["Price"],
  $_POST["Invoice"]["Units"]
);

NEW PHP RESULT:
array(
  0 => array(
    "Description" => "A widget",
    "Price" => "$1.00",
    "Units" => 2
  ),
  1 => array(
    "Description" => "A gadget",
    "Price" => "$3.00",
    "Units" => 3
  )
)


I don't think this feature would be difficult to implement and it would be a great addition to array_map's functionality.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-24 11:13 UTC] jani@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-11-24 11:13 UTC] jani@php.net
It's your form that needs fixing, not PHP in this case. Just pass the variables from the form with proper tags and you will get the arrays as you wish.
Something like this:

<form method="post">
  <input name="invoice[0][name]">
  <input name="invoice[0][price]">
  <input name="invoice[1][name]">
  <input name="invoice[1][price]">
  <input type="submit">
</form>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Dec 05 14:00:02 2025 UTC