php多维数组作为带ID和数组值的单选按钮

时间:2015-10-23 14:11:10

标签: php multidimensional-array radio-button

我是php noob。我过去几天彻底搜索了谷歌并且无法解决这个问题。

我有多维数组我必须转换为具有唯一ID和值的单选按钮,但我似乎无法做到。

json数组:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [available] => 1
                [courier] => 1
                [type] => 1
                [price] => 42.89
                [transitDays] => 3
            )

        [1] => Array
            (
                [available] => 1
                [courier] => 1
                [type] => 3
                [price] => 50.50
                [transitDays] => 4
            )

        [2] => Array
            (
                [available] => 0
            )

        ...

    )

[1] => Array
    (
        [0] => Array
            (
                [available] => 1
                [courier] => 2
                [type] => 1
                [price] => 111
                [transitDays] => 11
            )

        [1] => Array
            (
                [available] => 0
                [courier] => 2
                [type] => 4
                [price] => 22
                [transitDays] => 22
            )

        ...
    )
)

我需要将每个['available']==1数组的每个输出值放到单选按钮中,然后选择能够在表单提交后检索数据。

<p class="row"><input type="radio" id="option-<?php echo $i ?> value="service-<?php echo $i ?>" name="type" "> <?php echo $service['type']; ?> will cost <?php echo $service['price']; ?> and take <?php echo $service['days']; ?></p>

我尝试过扁平化数组并调出可用结果,但我无法分配unique ID's。 我试过了

  foreach ($providers as $provider) {
  $mergeProvider = array_merge($provider);
  foreach ($provider as $services){
    $service = array_merge($services);
      if( $service['available'] == 0 ) { unset($service); }
      $serviceCount = count($service);
      else {
          include('offer.php'); //where is input type="button"
      }

但这不允许我使用唯一ID。

如果我这样做:

foreach ($providers as $provider) {
  $mergeProvider = array_merge($provider);
  foreach ($provider as $services){
    $service = array_merge($services);
        $serviceCount = count($services);
        for( $i = 1; $i < $serviceCount; $i++ ) {
        echo "<pre>";
        echo $serviceCount . "</pre>";

它会喷出$serviceCount个不同的选项,其中相同的选项中包含不同的ID。

我该怎么办?

2 个答案:

答案 0 :(得分:0)

也许你可以根据foreach循环的键创建一个唯一的密钥。

然后当您发布表单时,名称字段将包含一个唯一的值,如service-00,service-01,service-10

例如:

$items = array(
    0 => array(
        0 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 1,
            "price" => 42.89,
            "transitDays" => 3
        ),
        1 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 3,
            "price" => 50.50,
            "transitDays" => 4
        ),
    ),
    1 => array(
        0 => array(
            "available" => 1,
            "courier" => 2,
            "type" => 1,
            "price" => 111,
            "transitDays" => 11
        ),
        1 => array(
            "available" => 0,
            "courier" => 2,
            "type" => 4,
            "price" => 22,
            "transitDays" => 22
        ),
    )
);

//然后遍历$ items并创建唯一键

foreach($items as $key => $item) {
    foreach($item as $subKey => $subItem) {
        if ($subItem["available"] === 1) {
            $uniqueKey = $key . $subKey;
            echo sprintf(
                '<p class="row"><input type="radio" id="option-%1$s" value="service-%1$s" name="type">%2$s will cost %3$s and take %4$s</p>',
                $uniqueKey,
                $subItem["type"],
                $subItem["price"],
                $subItem["transitDays"]
            );
        }
    }
}

答案 1 :(得分:0)

作为对你评论中问题的回答:

你的意思是如何将服务-10映射回阵列? 然后你需要一种从字符串'service-10'获得'10'的方法。但是当数字大于10时,这可能会出错。例如110(1和10)。 所以我添加了另一个如何做到这一点的例子。我用管道更新了代码以分隔$ key和$ subkey: $ uniqueKey = $ key。 '|' 。 $子项;

我还添加了一个var_dump,以便您可以看到它匹配的映射数据。

//例如,这是你的index.php

<html>
<head></head>
<body>
<form id="theForm" name="theForm" method="POST" action="submit.php">
    <?php
    $items = array(
        0 => array(
            0 => array(
                "available" => 1,
                "courier" => 1,
                "type" => 1,
                "price" => 42.89,
                "transitDays" => 3
            ),
            1 => array(
                "available" => 1,
                "courier" => 1,
                "type" => 3,
                "price" => 50.50,
                "transitDays" => 4
            ),
        ),
        1 => array(
            0 => array(
                "available" => 1,
                "courier" => 2,
                "type" => 1,
                "price" => 111,
                "transitDays" => 11
            ),
            1 => array(
                "available" => 0,
                "courier" => 2,
                "type" => 4,
                "price" => 22,
                "transitDays" => 22
            ),
        )
    );

    foreach($items as $key => $item) {
        foreach($item as $subKey => $subItem) {
            if ($subItem["available"] === 1) {
                $uniqueKey = $key . '|' . $subKey;
                echo sprintf(
                    '<p class="row"><input type="radio" id="option-%1$s" value="service-%1$s" name="type">%2$s will cost %3$s and take %4$s</p>',
                    $uniqueKey,
                    $subItem["type"],
                    $subItem["price"],
                    $subItem["transitDays"]
                );
            }
        }
    }
    ?>
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>

例如,这是你的submit.php

<?php
$items = array(
    0 => array(
        0 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 1,
            "price" => 42.89,
            "transitDays" => 3
        ),
        1 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 3,
            "price" => 50.50,
            "transitDays" => 4
        ),
    ),
    1 => array(
        0 => array(
            "available" => 1,
            "courier" => 2,
            "type" => 1,
            "price" => 111,
            "transitDays" => 11
        ),
        1 => array(
            "available" => 0,
            "courier" => 2,
            "type" => 4,
            "price" => 22,
            "transitDays" => 22
        ),
    )
);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['type'])) {
        $type = $_POST['type'];
        $positionDash = strpos($type, '-');
        $positionPipe = strpos($type, '|');
        if (false !== $positionDash && false !== $positionPipe) {
            $tail = substr($type, $positionDash+1);
            $tree = explode('|', $tail);
            $mappedData = $items[$tree[0]][$tree[1]];
            var_dump($mappedData);
        }
    }
}