简体   繁体   English

使用不同的键和值对多维数组进行排序

[英]Sort multidimensional array with different keys and values

I have a form where the user can add all the additional fields that they need.我有一个表单,用户可以在其中添加他们需要的所有其他字段。

Note: I would like to find a way to organize the code more efficiently, I will explain it in detail later.注意:我想找到一种更有效地组织代码的方法,我稍后会详细解释。

在此处输入图像描述

As it is structured in HTML, I have simplified it to make it easier to understand:由于它的结构是 HTML,因此我对其进行了简化以使其更易于理解:

<form action="" method="post">
    <h1>Products</h1>       
    <p>
        <input type="text" name="product_name[]" value="Product #1">
        <input type="text" name="product_sku[]" value="pro-001">
        <input type="text" name="product_price[]" value="$12.00">
        <input type="text" name="product_stock[]" value="10">
    </p>
    <p>
        <input type="text" name="product_name[]" value="Product #2">
        <input type="text" name="product_sku[]" value="pro-002">
        <input type="text" name="product_price[]" value="$12.00">
        <input type="text" name="product_stock[]" value="10">
    </p>
    <p><button type="submit">Add Product</button></p>
</form>

I need to process these received data, to later work with them more easily, for example adding it to the database.我需要处理这些接收到的数据,以便以后更轻松地使用它们,例如将其添加到数据库中。 But I get the code this way, a structure that doesn't make things much easier for working with that data.但是我以这种方式获取代码,这种结构不会使处理该数据的事情变得更容易。

Array
(
    [product_name] => Array
        (
            [0] => Product #1
            [1] => Product #2
        )

    [product_sku] => Array
        (
            [0] => pro-001
            [1] => pro-002
        )

    [product_price] => Array
        (
            [0] => $12.00
            [1] => $12.00
        )

    [product_stock] => Array
        (
            [0] => 10
            [1] => 10
        )

)

I wish I could receive the code like this:我希望我能收到这样的代码:

Array
(
    [0] => Array
        (
            [product_name] => Product #1
            [product_sku] => pro-001
            [product_price] => $12.00
            [product_stock] => 10
        )

    [1] => Array
        (
            [product_name] => Product #2
            [product_sku] => pro-002
            [product_price] => $12.00
            [product_stock] => 10
        )

)

I have achieved it in the following way, but I want to do it in a more optimal way.我通过以下方式实现了它,但我想以更优化的方式来实现它。

if(isset($_POST) && !empty($_POST)) {                
    // Total products to add
    $total_products = count($_POST["product_name"]);
    // Products ordered
    $products_created = [];

    for ($i=0; $i <$total_products ; $i++) {
        $products_created[$i] = array(
            'product_name' => $_POST["product_name"][$i],
            'product_sku' => $_POST["product_sku"][$i],
            'product_price' => $_POST["product_price"][$i],
            'product_stock' => $_POST["product_stock"][$i]
        );
    }

   echo "<pre>"; print_r($_POST); 
   echo "<pre>"; print_r($products_created); 
}

Complete example code:完整的示例代码:

<?php

if(isset($_POST) && !empty($_POST)) {                
    // Total products to add
    $total_products = count($_POST["product_name"]);
    // Products ordered
    $products_created = [];

    for ($i=0; $i <$total_products ; $i++) {
        $products_created[$i] = array(
            'product_name' => $_POST["product_name"][$i],
            'product_sku' => $_POST["product_sku"][$i],
            'product_price' => $_POST["product_price"][$i],
            'product_stock' => $_POST["product_stock"][$i]
        );
    }

   echo "<pre>"; print_r($_POST); 
   echo "<pre>"; print_r($products_created); 
}

 ?>

<form action="" method="post">
    <h1>Products</h1>       
    <p>
        <input type="text" name="product_name[]" value="Product #1">
        <input type="text" name="product_sku[]" value="pro-001">
        <input type="text" name="product_price[]" value="$12.00">
        <input type="text" name="product_stock[]" value="10">
    </p>
    <p>
        <input type="text" name="product_name[]" value="Product #2">
        <input type="text" name="product_sku[]" value="pro-002">
        <input type="text" name="product_price[]" value="$12.00">
        <input type="text" name="product_stock[]" value="10">
    </p>
    <p><button type="submit">Add Product</button></p>
</form>

Solution in PHP PHP中的解决方案

You could use a transpose function, that will turn your $_POST data into the desired data structure:您可以使用transpose function,这会将您的$_POST数据转换为所需的数据结构:

function transpose($array) {
    foreach ($array as $key => $values) {
        foreach ($values as $i => $value) $result[$i][$key] = $value;
    }
    return $result;
}

call as:调用为:

$products_created = transpose($_POST);

Solution in HTML HTML 中的解决方案

You could get the desired structure directly in PHP $_POST , if you change your HTML to this:如果您将 HTML 更改为此,您可以直接在 PHP $_POST中获得所需的结构:

<form action="" method="post">
    <h1>Products</h1>       
    <p>
        <input type="text" name="product[0][name]" value="Product #1">
        <input type="text" name="product[0][sku]" value="pro-001">
        <input type="text" name="product[0][price]" value="$12.00">
        <input type="text" name="product[0][stock]" value="10">
    </p>
    <p>
        <input type="text" name="product[1][name]" value="Product #2">
        <input type="text" name="product[1][sku]" value="pro-002">
        <input type="text" name="product[1][price]" value="$12.00">
        <input type="text" name="product[1][stock]" value="10">
    </p>
    <p><button type="submit">Add Product</button></p>
</form>

You would need some logic to inject that sequential number in the first bracket pairs in the name attributes.您需要一些逻辑来将该序列号注入name属性的第一个括号对中。 Assuming that this HTML is produced by PHP, it would not be hard to do that.假设这个 HTML 是由 PHP 生产的,那么做到这一点并不难。 If your HTML is dynamic on the client side, where rows can be added without interaction with the server, then you'll need to do this (also) in JavaScript.如果您的 HTML 在客户端是动态的,可以在不与服务器交互的情况下添加行,那么您(也)需要在 JavaScript 中执行此操作。

I suppose you'll have no problem in setting that up.我想你在设置它时不会有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM