php 多维数组作为具有 ID 和数组值的单选按钮
php multidimensional array as radio buttons with ID and array value
我是php菜鸟。我已经 Google 非常彻底地搜索了几天,但无法弄清楚。
我有一个多维数组,我必须将其转换为具有唯一 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
数组的一些值输出到单选按钮中,并且 select 能够在提交表单后检索数据。
<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。
我能做什么?
也许您可以根据 foreach 循环的键创建一个唯一键。
然后当您 post 表单时,名称字段将包含一个唯一值,如 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"]
);
}
}
}
作为对您评论中问题的回答:
你的意思是如何将 service-10 映射回数组?
然后,您需要一种方法从字符串 'service-10' 中获取“10”。但是当数字大于 10 时,这可能会出错。例如 110(1 和 10)。
所以我添加了另一个示例来说明如何做到这一点。我用管道更新了代码以分隔 $key 和 $subkey:
$唯一键 = $键。 '|' . $subKey;
我还添加了一个 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);
}
}
}
我是php菜鸟。我已经 Google 非常彻底地搜索了几天,但无法弄清楚。
我有一个多维数组,我必须将其转换为具有唯一 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
数组的一些值输出到单选按钮中,并且 select 能够在提交表单后检索数据。
<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。
我能做什么?
也许您可以根据 foreach 循环的键创建一个唯一键。
然后当您 post 表单时,名称字段将包含一个唯一值,如 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"]
);
}
}
}
作为对您评论中问题的回答:
你的意思是如何将 service-10 映射回数组? 然后,您需要一种方法从字符串 'service-10' 中获取“10”。但是当数字大于 10 时,这可能会出错。例如 110(1 和 10)。 所以我添加了另一个示例来说明如何做到这一点。我用管道更新了代码以分隔 $key 和 $subkey: $唯一键 = $键。 '|' . $subKey;
我还添加了一个 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);
}
}
}