0

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" 

} 

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
  • By adjusting your HTML you can get the desired $_POST format you're looking for... see my below answer Commented Nov 19, 2010 at 15:07

4 Answers 4

4

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=""/>

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

update

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'];
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you don't click the checkbox, the tags will still be there. I don't think that is suppose to happen.
I updated it to show how you'd utilize this code on the PHP side
2
$finalArray = array();
foreach($array['product'] as $key => $name){
    if(!empty($array['chk'][$key])){
        $finalArray[] = array(
            "product" => $name,
            "tag" => $array['tags'][$key]);
    };
}

1 Comment

Like the array structure setup, +1.
1

Pretty simple:

$i=0;
foreach ($array['chk'] as $key => $val) {
     if ($val == "on") {
         $new_array[$i]['product'] = $array['product'][$key]; 
         $new_array[$i++]['tags'] = $array['tags'][$key];
     }
}

print_r($new_array);

Should do it, there are other ways, this is just one of them.

5 Comments

@Felix Why not? It only iterates on the second one, and since it is $i++ it will only iterate after it prints what $i was.
@Brad the $i++ is confusing when used like that. It would probably be better to just increment it after for clarity, or just use the [] with an entire array (see my answer).
@Chacha102, I can understand that, however, it is not incorrect. But thanks for elaborating, appreciate it.
There's no point in processing any of this in PHP, it can be done on the HTML level, see my below answer..... I like $i++ being part of the loop like that, though it does take away from readability cause it'd be easy to overlook
Sorry Brad, my mistake, you are right, $i is evaluated before it gets incremented... stupid me.
1
foreach ($records['chk'] as $id => $temp) {
  $tweet[$id]['product'] = $records['product'][$id];
  $tweet[$id]['tag'] = $records['tags'][$id];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.