Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

php - 在以下示例中,如何使用PHP foreach?(How can I use PHP foreach in the following example?)

I have a big issue right now in using php foreach function.

(我现在在使用php foreach函数时遇到了一个大问题。)

I need to return each name plus each value from the html source code below or a similar one.

(我需要从下面的html源代码或类似的源代码中返回每个名称加上每个值。)

Example: {_pn_Brand:Dell}.

(示例:{_ pn_Brand:Dell}。)

Everything good so far.

(到目前为止一切都很好。)

My problem is that the source code contains one or more parameters (in my example below: _pn_Hardware Connectivity, _pn_Features) which can be found several times in the source code and I only need to return the checked ones.

(我的问题是源代码包含一个或多个参数(在下面的示例中为_pn_Hardware Connectivity和_pn_Features),这些参数可以在源代码中多次找到,而我只需要返回选中的参数即可。)

To be more specific I need to return something like: {_pn_Hardware Connectivity:HDMI,USB 2.0}, {_pn_Features:Backlit Keyboard,Bluetooth,Built-in Microphone,Built-in Webcam}.

(具体来说,我需要返回以下内容:{_pn_Hardware Connectivity:HDMI,USB 2.0},{_ pn_Features:Backlight Keyboard,Bluetooth,内置麦克风,内置摄像头}。)

Please keep in mind that the attributes names like _pn_Hardware Connectivity or _pn_Features can change in other source codes so I need a foreach function to generally solve my problem.

(请记住,诸如_pn_Hardware Connectivity或_pn_Features之类的属性名称可以在其他源代码中更改,因此我需要一个foreach函数来普遍解决我的问题。)

I hope you guys understand exactly what I'm looking for.

(我希望你们能完全理解我在寻找什么。)

My apologise for the poor explanation but couldn't think of a better one for the moment.

(对于抱歉的解释,我深表歉意,但暂时无法想到更好的解释。)

Please check the html source code below:

(请检查以下html源代码:)

<input type="text" name="_pn_Brand" size="21" maxlength="50" gname="Brand" value="Dell">
<input type="text" name="_pn_MPN" size="21" maxlength="50" value="">
<input type="text" name="_pn_Type" size="21" maxlength="50" value="Notebook/Laptop">
<input type="text" name="_pn_Screen Size" size="21" maxlength="50" value="17 in">
<input type="text" name="_pn_RAM Size" size="21" maxlength="50" value="16 GB">
<input type="text" name="_pn_Processor" size="21" maxlength="50" value="Intel Core i7 4th Gen Quad core.">
<input type="text" name="_pn_Graphics Processing Type" size="21" maxlength="50" value="Dedicated Graphics">
<input type="text" name="_pn_Manufacturer Colour" size="21" maxlength="50" value="Black">
<input type="text" name="_pn_Manufacturer warranty" size="21" maxlength="50" value="None" closed="true">
<input type="text" name="_pn_Processor Speed" size="21" maxlength="50" value="2.50- 3.20 GHz">
<input type="text" name="_pn_pnorage Type" size="21" maxlength="50" value="HDD (Hard Disk Drive)">
<input type="text" name="_pn_Hard Drive Capacity" size="21" maxlength="50" value="500 GB">
<input type="text" name="_pn_Operating System" size="21" maxlength="50" value="Windows 10">
<input type="text" name="_pn_Operating System Edition" size="21" maxlength="50" value="Professional">
<input type="text" name="_pn_Release Year" size="21" maxlength="50" value="2014">
<input type="text" name="_pn_Colour" size="21" maxlength="50" value="Black">
<input type="checkbox" name="_pn_Hardware Connectivity" value="DisplayPort">
<input type="checkbox" name="_pn_Hardware Connectivity" value="Mini DisplayPort">
<input type="checkbox" name="_pn_Hardware Connectivity" value="HDMI" checked="checked">
<input type="checkbox" name="_pn_Hardware Connectivity" value="Micro-HDMI">
<input type="checkbox" name="_pn_Hardware Connectivity" value="Micro-USB">
<input type="checkbox" name="_pn_Hardware Connectivity" value="Mini-USB">
<input type="checkbox" name="_pn_Hardware Connectivity" value="USB 1.0/1.1">
<input type="checkbox" name="_pn_Hardware Connectivity" value="USB 2.0" checked="checked">
<input type="checkbox" name="_pn_Hardware Connectivity" value="USB 3.0">
<input type="checkbox" name="_pn_Hardware Connectivity" value="USB 3.1">
<input type="checkbox" name="_pn_Features" value="10/100 LAN Card">
<input type="checkbox" name="_pn_Features" value="3D Camera">
<input type="checkbox" name="_pn_Features" value="Backlit Keyboard" checked="checked">
<input type="checkbox" name="_pn_Features" value="Bluetooth" checked="checked">
<input type="checkbox" name="_pn_Features" value="Built-in Microphone" checked="checked">
<input type="checkbox" name="_pn_Features" value="Built-in Webcam" checked="checked">
<input type="checkbox" name="_pn_Features" value="HSDPA">
<input type="checkbox" name="_pn_Features" value="Tablet PC">
<input type="checkbox" name="_pn_Features" value="Touchscreen">
<input type="checkbox" name="_pn_Features" value="Virtual Reality Ready">

Thanks to everyone that can help in advance!

(感谢每个可以提前帮助的人!)

  ask by Iain translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I will advise you to first group the input in two groups: Input group and Checkbox group Then you can create variables and loop to display the inputs:

(我建议您首先将输入分为两组:输入组和复选框组然后可以创建变量并循环显示输入:)

 <php $inputGroup = [ ['_pn_Brand', 21, 50, 'Brand', 'Dell'], ['_pn_MPN', 21, 50, '', ''], ... ]; $checkboxGroup = [ ['_pn_Hardware Connextivity', [ 'DisplayPort', 'Mini DisplayPort', ... ]], ['_pn_Features', [ '10/100 LAN Card', '3D Camera', ... ]] ]; // Then you loop each varaible to display the inputs foreach($inputGroup as $input) { echo '<input type="text" name="'. $input[0] .'" size="'. $input[1] .'" maxlength="'. $input[2] .'" gname="'. $input[3] .'" value="'. $input[4] .'">'; } foreach($checkboxGroup as $checkbox) { foreach($checkbox[1] as $value) { echo '<input type="checkbox" name="'. $checkbox[0] .'" value="'. $value .'">' } } ?> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...