1

I want an array in the below format using Variables stored from the database and $_GET

array('FNAME' => 'NIDHI','EMAIL' => '[email protected]','SRC' => '')

What I am trying is like below

if ($variableResult->num_rows>0) {
            
            while($variable_row = $variableResult->fetch_assoc()) {

              $variableArray[] = $variable_row['variable_name'];
              
            }
            
            $totalVariables = count($variableArray);
            
            
         
            for($i = 0; $i < $totalVariables; $i++) {
              
              $dataArray[] = array($variableArray[$i] => htmlspecialchars($_GET[$variableArray[$i]]));
              
            }

            var_dump($dataArray);
            exit();
            
        }

It's giving me output like below

array(5) { [0]=> array(1) { ["EMAIL"]=> string(17) "[email protected]" } [1]=> array(1) { ["LNAME"]=> string(5) "NIDHI" } [2]=> array(1) { ["THIS_IS_CUSTOM"]=> string(0) "" } [3]=> array(1) { ["THIS_IS_CUSTOM_2"]=> string(0) "" } [4]=> array(1) { ["THIS_IS_CUSTOM_3"]=> string(0) "" } }

3 Answers 3

1

You do not need two loops here. 1) while and 2) for. While is enough.

Note: The below code may not perfect. You may need to change it a little bit :)

$variableArray = []; // declare blank array variable first
while($variable_row = $variableResult->fetch_assoc()) { 
   if(!empty($variable_row['variable_name'])){ // check if key presnt
       $varName = $variable_row['variable_name'];
       if(!empty($_GET[$varName])){ // check if valid get variable exist
            $variableArray[$varName] = htmlspecialchars($_GET[$varName]); 
       }
   }  
}
// print it for better visibility
echo '<pre>'; print_r($variableArray); echo '</pre>';
Sign up to request clarification or add additional context in comments.

Comments

0

Simplify your script by iterating the result set object with foreach() -- you don't need to check num_rows or make iterated fetch_assoc() calls. Using array destructuring syntax in the foreach()'s head will offer you a clean temporary variable to use in the loop body.

I'm not sure if your call of htmlspecialchars() is appropriate, but if you are going to make the call, make sure that the data type of the value is string (that's what it expects).

$dataArray = [];
foreach ($variableResult as ['variable_name' => $k]) {
    if (key_exists($k, $_GET)) {
        $dataArray[$k] = is_string($_GET[$k]) ? htmlspecialchars($_GET[$k]) : $_GET[$k];
    }
}
var_export($dataArray);

Comments

-1

I believe it's giving you the correct result but the formatting is a bit confusing.

Replace:

var_dump($dataArray);

with:

print("<pre>".print_r($dataArray,true)."</pre>");

2 Comments

Hi Manny! Thanks for your answer but its giving another array inside array Like this prnt.sc/1aeomgb Which I do not want, I want single array as I have asked in my question. Thanks!
Would you post the output

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.