我有转换PHP $_POST
virables的问题。
我将此类信息发送到网站:
$_POST['name.1']='xxx';
$_POST['height.1']='100';
$_POST['qty.1']='2';
$_POST['op.1.0']='color';
$_POST['op.1.1']='size';
$_POST['opv.1.0.0']='red';
$_POST['opv.1.0.1']='blue';
$_POST['opv.1.1.0']='xl';
$_POST['opv.1.1.1']='l';
$_POST['opv.1.1.2']='xxl';
$_POST['name.2']='yyy';
$_POST['height.2']='10';
$_POST['qty.2']='4';
$_POST['number.2']='4';
$_POST['op.2.0']='color';
$_POST['op.2.1']='weight';
$_POST['opv.2.0.0']='red';
$_POST['opv.2.0.1']='silver';
$_POST['opv.2.1.0']='90';
$_POST['opv.2.1.1']='60';
$_POST['opv.2.1.2']='42';
我需要将此数据转换为以下格式:
$product[1]['name']='xxx';
$product[1]['height']='100';
$product[1]['qty']='2';
$product[1]['op'][0]='color';
$product[1]['op'][1]=size;
$product[1]['opv'][0][0]='red';
$product[1]['opv'][0][1]='blue';
$product[1]['opv'][1][0]='xl';
$product[1]['opv'][1][1]='l';
$product[1]['opv'][1][2]='xxl';
$product[2]['name']='yyy';
$product[2]['height']='10';
$product[2]['qty']='2';
$product[2]['number']='2';
$product[2]['op'][0]='color';
$product[2]['op'][1]='weight';
$product[2]['opv'][0][0]='red';
$product[2]['opv'][0][1]='silver';
$product[2]['opv'][1][0]='90';
$product[2]['opv'][1][1]='60';
$product[2]['opv'][1][2]='42';
first .1 is product nr.
second .1 is option nr.
third .1 is option value.
答案 0 :(得分:0)
$product = array();
foreach ($_POST as $key => $value) {
$key_array = explode('.', $key);
$name = array_shift($key_array);
$subscript = array_shift($key_array);
$target =& $product[$subscript][$name];
// If there are remaining elements, they're subscripts into nested array levels
foreach ($key_array as $next_subscript) {
$target =& $target[$next_subscript];
}
$target = $value;
}
答案 1 :(得分:0)
如果您无法修复表单,最懒的方法是通过以下方式转换数组:
Application
http_build_query
import Tkinter as tk
class Testing(tk.LabelFrame):
def __init__(self, parent, main):
self.buttonWidth = 10
self.parent=parent
self.main = main # save the instantiating class
tk.LabelFrame.__init__(self, self.parent,
text="Test Operations",
padx=10,
pady=10
)
self.taskButton = tk.Button(
self,
text="Do A Task",
width=self.buttonWidth,
command=self.doATask,
)
self.taskButton.pack()
def doATask(self):
#want to execute function in Results.getResult() but don't know how
self.main.results.getResult() #<--what you can do
class Results(tk.LabelFrame):
def __init__(self, parent, main):
self.parent = parent
self.main = main # save the instantiating class
tk.LabelFrame.__init__(self, self.parent, text="Visual Results")
self.resultLbl = tk.Label(self, text="Result")
self.resultLbl.pack()
def getResult(self):
self.resultLbl.config(bg='yellow')
class Application(tk.Frame):
def __init__(self, parent):
self.parent = parent
tk.Frame.__init__(self, self.parent)
self.testing = Testing(self.parent, self)
self.results = Results(self.parent, self)
self.testing.pack(fill=tk.X)
self.results.pack(fill=tk.X)
if __name__ == "__main__":
root = tk.Tk()
root.title("Modular GUI App")
Application(root).pack()
root.mainloop()
还有几个preg_replace
来电:
$post =
($_POST)
("/\b(\w+)\.(\d+)\.(\d+)\.(\d+)=/", "prod[$2][$1][$3][$4]=", $post)
然后根据parse_str()
将其转换回数组。
(这实际上只能正常工作,因为等值符号会被转义为("/\b(\w+)\.(\d+)\.(\d+)=/", "prod[$2][$1][$3]=", $post)
的值。)
你可以做一个手动循环当然“只是”重组键,但这是一个更多的代码。