How foreach change original array values in php?

0 votes

I am very new in multi dimensional arrays, and this is bugging me big time.

My array is as following:

$fields = array(
    "names" => array(
         "type"         => "text",
         "class"        => "name",
         "name"         => "name",
         "text_before"  => "name",
         "value"        => "",
         "required"     => true,
    )
)

Then i got a function checking if these inputs are filled in, if they are required.

function checkForm($fields){
    foreach($fields as $field){
        if($field['required'] && strlen($_POST[$field['name']]) <= 0){
            $fields[$field]['value'] = "Some error";
        }
    }
    return $fields;
}

Now my problem is this line

$fields[$field]['value'] = "Some error";

I want to change the content of the original array, since i am returning this, but how do I get the name of the current array (names in this example) in my foreach loop?

Aug 14 in PHP by kartik
• 27,140 points
87 views

1 answer to this question.

0 votes

Hello @kartik,

Do the following:

foreach ($fields as $key => $field) {
    if ($field['required'] && strlen($_POST[$field['name']]) <= 0) {
        $fields[$key]['value'] = "Some error";
    }
}

So basically use $field when you need the values, and $fields[$key] when you need to change the data.

Hope it helps!!
Thank you!

answered Aug 14 by Niroj
• 58,500 points

Related Questions In PHP

0 votes
1 answer

How to change the session timeout in PHP?

Hello @kartik, Put $_SESSION['login_time'] = time(); into the previous authentication ...READ MORE

answered Sep 15 in PHP by Niroj
• 58,500 points
20 views
0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16 in PHP by Niroj
• 58,500 points
17 views
0 votes
1 answer

How can we create a session in PHP?

Hello @kartik, session_start() function is used in PHP to ...READ MORE

answered Mar 27 in PHP by Niroj
• 58,500 points
71 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1 in PHP by Niroj
• 58,500 points
87 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 58,500 points
2,169 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 58,500 points
236 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 58,500 points
189 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 58,500 points
2,683 views
0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15 in PHP by Niroj
• 58,500 points
21 views
0 votes
1 answer

How to store values from foreach loop into an array?

Hello @kartik, Declare the $items array outside the loop and ...READ MORE

answered Aug 31 in PHP by Niroj
• 58,500 points
42 views