Reputation: 49
I have an array like this
Array
(
[data1] => Array
(
[name] => jhon
[status] => 1
[address] => 1
)
[data2] => Array
(
[name] => mayer
[status] => 1
[date] => 0
[height] => 180
)
[data3] => Array
(
[name] => kilo
[status] => 0
[weight] => 0
[Age] => 18
)
)
And then show the array in my template like this
<form action="" method="post">
<input type="hidden" name="act" value="do_add">
foreach ($datas as $data){
echo $data['name'];
foreach ($data as $key => $value): ?>
echo $key.' <input name="$key[]" type="text" value="$value">';
}
}
</form>
And I put all of that code in my php file. The foreach inside the form submit.
The code was perfectly show me everything, but I have an error result as the submited data.
The submitted data give me an array like this.
Array
(
[name] => Array
(
[0] => jhon
[1] => mayer
[2] => kilo
)
[status] => Array
(
[0] => 1
[1] => 1
[2] => 0
)
[address] => Array
(
[0] => street
)
[date] => Array
(
[0] => 2019
)
[height] => Array
(
[0] => 190
)
[weight] => Array
(
[0] => 80
)
[age] => Array
(
[0] => 20
)
)
The question is, how can I get result as the same as my array. I need my old array to be updated to new value as the submit form clicked. Just like the array format before submit data.
Anyone can help me?
Upvotes: 0
Views: 559
Reputation: 2188
You are writing your HTML code incorrectly, if you want to have indexed values:
echo $key.' <input name="$key[]" type="text" value="$value">';
This line is parsing to, ex, name="status[]"
which is not what you want.
Instead, modify the lines to read as:
<?php
foreach ($datas as $k => $data) {
echo $data['name'];
foreach ($data as $key => $value) {
echo $key.' <input name="$k[$key]" type="text" value="$value">';
}
}
?>
Now this parses as name="data1[status]"
Upvotes: 1