1

I want to make a form input with dynamic Name Label and Value, there are two arrays, how to make it loop in single foreach ?

This is an example:

<?php 
$value = explode(',',$row['value']); 
$name = explode(',',$row['name']); 
for($x = 0; $x <= 10; $x++) { 
echo $name; 
echo $value; 
}
?>

$row Variable is an Array, This method didn't work for me. Any suggestions?

1
  • what about absence of value represent? Commented Feb 20, 2017 at 5:38

2 Answers 2

2

Why cant you use array_combine before loop, try this..

<?php 
$value = explode(',',$row['value']); 
$name = explode(',',$row['name']);
$combainedArray = array_combine ( $name , $value );
foreach($combainedArray as $name => $value ) { 
          echo $name, '=>', $value; 
}
?>

OR

if you don't what to combine make it like this..

<?php 
$value = explode(',',$row['value']); 
$name = explode(',',$row['name']); 
$count = count($value);
for($x = 0; $x < $count; $x++) { 
   echo $name[$x]; 
   echo $value[$x]; 
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

if using foreach, it be possible to use parameters ? for example $combineArray['name'] and $combineArray['value']
No, here you will get $name as KEY and $value as VALUE of array and loop until getting the end of array.
1

As a solution to above mentioned problem,Please try executing below mentioned code snippet.

In consideration with same no of form fields for name and value

<?php 
$value = explode(',',$row['value']); 
$name = explode(',',$row['name']); 
for($x = 0; $x <= 10; $x++) { 
echo $value[$x];
echo $name[$x];
}
?>

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.