-1

I have array that I'd like to "optimize", instead of manually adding pg1-pg11, I'd like to use "for".

How to make it corectly?

$query = array(
    'e1' => $_POST['npole3-1'], 
    'e2' => $_POST['npole3-2'],
    'e3' => $_POST['npole3-3'],
    'e4' => $_POST['npole3-4'],
    'e5' => $_POST['npole3-5'],
    'e6' => $_POST['npole3-6'],
    'e7' => $_POST['npole3-7'],
    'e8' => $_POST['npole3-8'],
    'e9' => $_POST['npole3-9'],
    'e10' => $_POST['npole3-10'],
    'e11' => $_POST['npole3-11'],
    'e12' => $_POST['npole3-12'],
    'e13' => $_POST['npole3-13'],
    'e14' => $_POST['npole3-14'],
    'e15' => $_POST['npole3-15'],
    'e16' => $_POST['npole3-16'],
    'e17' => $_POST['npole3-17'],
    'e18' => $_POST['npole3-18'],   
    );

for ($i = 1; $i <= 11; $i++) {array_push($query, ('pg1' => $_POST['pgadd-' . i]))}; 
riten
  • 185
  • 1
  • 12

1 Answers1

0
$query = array();

for ($i = 1; $i <= 11; $i++) {

      // if exists in post array then
      if(isset($_POST['pgadd-' . $i])){

          // then add to query array
          // array key will be like pg1, pg2, pg3 ...
          // array value will be from your post data pgadd-1. pgadd-2

          $query['pg'.$i] = $_POST['pgadd-' . $i];
      }
}

// to see what's there inside array
print_r($query);
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • 2
    You were down-voted twice, if I had to guess why, it may be because it _looks_ like a code-only answer? But I don't know for sure. Not my downvote. I think your answer is useful though so I'll bring you back to 0 – GrumpyCrouton Oct 09 '17 at 18:08
  • 3
    That's my bet too @GrumpyCrouton as the close vote reviewers will often say *"While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value."* On top of it the voters vote and move on, not coming back to remove them even when the answer is improved. – Jay Blanchard Oct 09 '17 at 18:10
  • 1
    Closing posts is correctly done with links to code that solves the problem @AkshayHegde. We all know it isn't personal and should guide the new coder to a solution. Code only answers tend to lack even the basic guidance of what functions are used and why they are used the way they. In many ways this is more of a disservice to new coders than links to working code with explanations. As you said, *"OP couldn't able to understand and fix small small issues"* because the code given to them wasn't explained. – Jay Blanchard Oct 09 '17 at 18:26
  • 1
    @JayBlanchard But I feel like this answer was incorrectly marked, as each step of the code is commented to provide context. It just _looks_ like there is no context because it is 100% in a code block. A lot of my answers contain very commented code because sometimes there isn't more needed. – GrumpyCrouton Oct 09 '17 at 18:28
  • I ask you to consider the new programmer @GrumpyCrouton, one who has not become familiar with code comments and how to parse them as concepts. To them, they see a block of code that they're going to copy and paste into their project and it will either work (yay!) or not work because they haven't given us everything we need to know to properly code a solution for them. At that point they need to understand the concept behind the code provided because the concept might be right, but might have other unknown problems if they just cut 'n paste. – Jay Blanchard Oct 09 '17 at 18:34
  • @JayBlanchard I agree with you, so would it be preferred if heavily commented code is broken into smaller pieces with comments left outside of the code itself? – GrumpyCrouton Oct 09 '17 at 18:35
  • To my way of thinking @GrumpyCrouton...yes. – Jay Blanchard Oct 09 '17 at 18:36
  • 1
    @JayBlanchard I would like to say that my question was not sarcastic in the slightest, I was legitimately asking if that is a good solution to the problem. Thank you :) – GrumpyCrouton Oct 09 '17 at 18:36
  • 1
    I know you weren't being sarcastic @GrumpyCrouton ¯\\_(ツ)_/¯ – Jay Blanchard Oct 09 '17 at 18:37
  • 1
    @JayBlanchard Good, I just know it's hard to understand tone over just some text on your screen sometimes, I just wanted to plainly state the tone my question was in. :) – GrumpyCrouton Oct 09 '17 at 18:38