1

I'm not sure how to search for the problem I'm facing, so that's why I'm asking here.

I want to know if there is a native php-function to transform following array I get from a html-form:

Original Array

$array = [
  'product_template_id' => [
    '0' => '1',
    '1' => '2'
  ],
  'amount' => [
    '0' => '50',
    '1' => '100'
  ]
]

Desired Array

$array = [
  '0' => [
    'product_template_id' => '1',
    'amount' => '50'
  ],
  '1' => [
    'product_template_id' => '2',
    'amount' => '100'
  ]
]

I know this can be done with a loop, but that's not what I'm asking for.

PS(not my main question, just a sidequestion): How can I bulk format code in stackoverflow? Is it always 4 spaces? How can I perform the formatting quicker?

edit: PS is not the main question, it is more of a sidenote which has already been answered by @RiggsFolly

Rahul
  • 17,770
  • 7
  • 39
  • 58
Dominik Vogt
  • 170
  • 5
  • 19
  • 1
    No, there is not any native function to do this. You have to use the exists PHP function and adding some logic (for loop etc....) – cpap Apr 19 '17 at 08:23
  • 2
    Format = `CTRL+K` – RiggsFolly Apr 19 '17 at 08:24
  • You could make your own function if you needed to this often enough. – Icewine Apr 19 '17 at 08:26
  • You can do something like this, its all one time, use sublime IDE, install plugin codeFormatter from package installer and create one dummy.php file, paste your code in that, then CTRL + ALT + F for formatting the code and copy that code from there, paste it in SO question and then select code you want to format and press CTRL + K. Thats it. – Rahul Apr 19 '17 at 08:28
  • what is the real issue? just formatting spaces? use your text editor – RomanPerekhrest Apr 19 '17 at 08:32
  • @RomanPerekhrest i think the question is clear. Its about reformatting the original array into the desired array using a programmatic approach with native php array functions. – Dominik Vogt Apr 19 '17 at 08:36
  • 2
    @DominikVogt, good luck with that – RomanPerekhrest Apr 19 '17 at 08:37

2 Answers2

2

Just as prove of concept you can use array_map:

$keys = array_keys($array);
// For < PHPv5.6
// $zip = call_user_func_array('array_map', array_merge([null], $array));
$zip = array_map(null, ...array_values($array));
$result = array_map(function ($item) use ($keys) {
    return array_combine($keys, $item);
}, $zip);

Here is working demo.

But in practice, there is too much overhead for so simple problem.

EDIT: Here I am zipping (getting one corresponding element from each array and creating an array of this elements) all child arrays. It is done with array_map using its property:

An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function

As array_map can take an arbitrary number of arrays, I supply them to it with '...' splat operator.

sevavietl
  • 3,644
  • 1
  • 13
  • 21
  • @mickmackusa, this is sometimes called `splat operator`. It is used for argument unpacking. You can read [this blog post](https://lornajane.net/posts/2014/php-5-6-and-the-splat-operator) for more info. – sevavietl Apr 19 '17 at 12:57
  • Upvoted this answer! I've been inspired by this technique and have been researching and experimenting with it. I have modified the OP's input array and had a play with your method, added a bunch of comments and supplied a couple links in this [Demo](http://sandbox.onlinephpfunctions.com/code/1a15c1ea71e1aee05bd8cabe7da05e75840a5f61). Some direct links: [PHP5.6Features](http://php.net/manual/en/migration56.new-features.php) , [Variable Arg List](http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list) , [Bishop's Answer](http://stackoverflow.com/a/39600743/2943403) – mickmackusa Apr 20 '17 at 02:26
0

This does what you need!

for ($x = 0; $x < count($array['amount']); $x ++) {
    $arr[$x] = [
        'product_template_id' => $array['product_template_id'][$x],
        'amount' => $array['amount'][$x],
    ];
}

See here for a working example https://3v4l.org/dokHK

delboy1978uk
  • 11,504
  • 2
  • 18
  • 37
  • :D :D :D :D :D Read last line of OP. It is not the question for which you gave answer. – Rahul Apr 19 '17 at 08:28
  • :-P Haha I see it now. Sure it could be done without a loop if your array always had the same length, but that won't be the case will it? – delboy1978uk Apr 19 '17 at 08:36
  • *How can I bulk format code in stackoverflow? Is it always 4 spaces? How can I perform the formatting quicker?* This is question. Op said, "I know this can be done with a loop, but that's not what I'm asking for.". So he knows how to do it, he wants to format code in SO. OP don't need code at all. OP needs steps or way to format code in SO. – Rahul Apr 19 '17 at 08:37
  • Actually I'd like to know that too, this 4 spaces nonsense is wasting precious time! – delboy1978uk Apr 19 '17 at 08:42
  • highlight text to be formatted and press `CTRL + K` – Dominik Vogt Apr 19 '17 at 08:44
  • 1
    Yes thats simple one, if needs more formatting like IDE, then follow my complete comment. – Rahul Apr 19 '17 at 08:46