本文介绍了PHP:帮助数组结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最后一个问题帮助我将数据从表单获取到数组中.该数组的结构与我以前使用的任何东西都大不相同.我有多个记录,每个记录都有一个文本框和一个复选框.这是表单数据:

My last question helped me get data from a form into an array. The array is fairly different in structure to anything I've used before. I have multiple records with a textbox and checkbox each. Here is the form data:

array(4) {
["product"]=> array(4) {
    [0]=> string(5) "Dummy"
    [1]=> string(7) "Dummy 2"
    [2]=> string(7) "Dummy 3"
    [3]=> string(7) "Dummy 4"
}
["tags"]=> array(4) {
    [0]=> string(25) "#enter, #product, #hastag"
    [1]=> string(0) ""
    [2]=> string(25) "#enter, #product, #hastag"
    [3]=> string(25) "#enter, #product, #hastag"
}
["chk"]=> array(2) {
    [0]=> string(2) "on"
    [2]=> string(2) "on"
}
["submit"]=> string(5) "tweet"

}

我想从该数组中获取数据,例如(仅当chk ="on"时):

I want to get the data from this array into a form such as (only if chk = "on"):

tweet[0]["product"] = "dummy"
tweet[0]["tag"] = "hash tag list here"

tweet[1]["product"] = "dummy3"
tweet[1]["tag"] = "hash tag list here"

任何帮助,我们将不胜感激!:)

Any help most appreciated! :)

推荐答案

首先,我建议使用 1 代替 on .您应该尽可能使用数值,因为它们需要较少的处理.我认为您需要重新调整HTML,因此您根本不需要在PHP方面进行任何处理...

First I'd recommend using 1 instead of on. You should try to use numerical values whenever possible as they require less processing. I think you need to readjust your HTML, so you don't do any processing on the PHP side at all...

<input type="checkbox" name="tweet[(YOUR_TWEET_ID)][product]" value="PRODUCT_NAME"/>
<input type="text" name="tweet[(YOUR_TWEET_ID)][tags]" value=""/>

这将使表单按照您想要的方式准确地$ _POST,而无需任何其他代码.

This will cause the form to $_POST exactly how you want it without any additional code.

更新

foreach ($_POST['tweet'] as $tweetId => $value) {
   //again, it'd be a good idea to use a numerical value here
   if (strlen($value['product']) > 0) {
      //this tweet was checked, lets do with it what we need

      //here's how you'd get the tags
      echo $_POST['tweet'][$tweetId]['tags'];
   }
}

这篇关于PHP:帮助数组结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:12