array_push() 抛出错误
array_push() throws an error
这是我的数组:
Array (
[country_0] => E92000001
[country_1] => L93000001
[country_2] => M83000003
[county_0] => E10000002
[county_1] => E10000003
[county_2] => E10000006
[county_3] => E10000007
[gor_0] => A
[gor_1] => B
)
我想得到这样的东西:
Array
(
[country] => Array(
[0] => L93000001
[1] => M83000003
[2] => M83000003
)
[county] => Array(
[0] => E10000002
[1] => E10000003
[2] => E10000006
[3] => E10000007
)
[gor] => Array(
[0] => A
[1] => B
)
)
我目前的代码是这样的:
$converted_array = [];
foreach ($input as $key => $value) {
$underscore_position = strpos($key, "_"); //returns integer.
$stripped_key = substr($key, 0, $underscore_position); //returns word eg "country"
array_push($converted_array[$stripped_key], $value); //doesn't work
array_push($converted_array, $stripped_key[$value]); //doesn't work
array_push($converted_array, $stripped_key => $value); //doesn't work
}
print_r($converted_array);
我无法让我的任何 array_push()
工作。我一直收到语法错误或非法偏移错误。
也许这不是最好的方法。我基本上是在尝试操纵隐藏形式的 post 数据。每个隐藏字段看起来像这样:
<input name="country_0" type="hidden" value="E92000001">
country_
后面的数字只是为了保持每个输入的唯一性。所以也许最好有类似 <input name="country_E92000001" type="hidden" value="E92000001">
的东西并进行某种数组键拼接。
那么我怎样才能让我的代码工作或者有更好的方法吗?
编辑:
要生成隐藏的输入字段,我使用以下代码:
<input name="<?php echo $key; ?>" type="hidden" value="<?php echo $subvalue; ?>">
因此,添加 []
会出现 Cannot use [] for reading
错误。有什么办法可以解决这个问题吗?
只需将输入字段的名称属性更改为:
<input name="country[]" type="hidden" value="E92000001">
//^^
那么它会自动成为一个数组。
编辑:
根据更新后的代码,您可以使用以下代码将 []
添加到名称中:
<input name="<?php echo strtok($key, "_") . "[]"; ?>" type="hidden" value="<?php echo $subvalue; ?>">
你应该试试这个
<input name="country[]" type="hidden" value="E92000001">
<input name="country[]" type="hidden" value="L93000001">
<input name="country[]" type="hidden" value="M83000003">
它将生成这样的数组:
Array
(
[country] => Array(
[0] => L93000001
[1] => M83000003
[2] => M83000003
)
)
最佳解决方案是在输入字段中使用数组:
<input name="country[]" type="hidden" value="E9200" />
<input name="country[]" type="hidden" value="E9201" />
<input name="country[]" type="hidden" value="E9202" />
这是我的数组:
Array (
[country_0] => E92000001
[country_1] => L93000001
[country_2] => M83000003
[county_0] => E10000002
[county_1] => E10000003
[county_2] => E10000006
[county_3] => E10000007
[gor_0] => A
[gor_1] => B
)
我想得到这样的东西:
Array
(
[country] => Array(
[0] => L93000001
[1] => M83000003
[2] => M83000003
)
[county] => Array(
[0] => E10000002
[1] => E10000003
[2] => E10000006
[3] => E10000007
)
[gor] => Array(
[0] => A
[1] => B
)
)
我目前的代码是这样的:
$converted_array = [];
foreach ($input as $key => $value) {
$underscore_position = strpos($key, "_"); //returns integer.
$stripped_key = substr($key, 0, $underscore_position); //returns word eg "country"
array_push($converted_array[$stripped_key], $value); //doesn't work
array_push($converted_array, $stripped_key[$value]); //doesn't work
array_push($converted_array, $stripped_key => $value); //doesn't work
}
print_r($converted_array);
我无法让我的任何 array_push()
工作。我一直收到语法错误或非法偏移错误。
也许这不是最好的方法。我基本上是在尝试操纵隐藏形式的 post 数据。每个隐藏字段看起来像这样:
<input name="country_0" type="hidden" value="E92000001">
country_
后面的数字只是为了保持每个输入的唯一性。所以也许最好有类似 <input name="country_E92000001" type="hidden" value="E92000001">
的东西并进行某种数组键拼接。
那么我怎样才能让我的代码工作或者有更好的方法吗?
编辑:
要生成隐藏的输入字段,我使用以下代码:
<input name="<?php echo $key; ?>" type="hidden" value="<?php echo $subvalue; ?>">
因此,添加 []
会出现 Cannot use [] for reading
错误。有什么办法可以解决这个问题吗?
只需将输入字段的名称属性更改为:
<input name="country[]" type="hidden" value="E92000001">
//^^
那么它会自动成为一个数组。
编辑:
根据更新后的代码,您可以使用以下代码将 []
添加到名称中:
<input name="<?php echo strtok($key, "_") . "[]"; ?>" type="hidden" value="<?php echo $subvalue; ?>">
你应该试试这个
<input name="country[]" type="hidden" value="E92000001">
<input name="country[]" type="hidden" value="L93000001">
<input name="country[]" type="hidden" value="M83000003">
它将生成这样的数组:
Array
(
[country] => Array(
[0] => L93000001
[1] => M83000003
[2] => M83000003
)
)
最佳解决方案是在输入字段中使用数组:
<input name="country[]" type="hidden" value="E9200" />
<input name="country[]" type="hidden" value="E9201" />
<input name="country[]" type="hidden" value="E9202" />