I am trying to recreate the below but within a PHP for loop. This is the original code that I need to replicate.
$values[ 'value' ] = array(
'form' => '1647',
'row_ids' => array( 0, 1, 2 ),
0 => array( 0 => '', $first_field => 'Row 1 value', $second_field => '1' ),
1 => array( 0 => '', $first_field => 'Row 2 value', $second_field => '2' ),
2 => array( 0 => '', $first_field => 'Row 3 value', $second_field => '3' ),
);
return $values;
}
I need to create a dynamic version of this, I thought the below code would work but it seems this is creating a parent array I don’t need and I am stuck with what to do next.
$i = 0;
$count = "0";
$row_values = array();
for ($x = 0; $x < $row_num; $x++) {
if($x != 0) {
$i++;
$count .= ", " . $x;
}
$row_values[$i][0] = '';
$row_values[$i][$first_field] = 'Row 1 value';
$row_values[$i][$second_field] = "$i";
}
$values[ 'value' ] = array(
'form' => '1647',
'row_ids' => array( $count ),
$row_values
);
So to clarify my code is creating:
Array ( [0] => Array ( [0] => [22] => Row 1 value [23] => 0 ) [1] => Array ( [0] => [22] => Row 1 value [23] => 1 ) );
And I need:
[0]=> array(3) { [0]=> string(0) "" [22]=> string(11) "Row 1 value" [23]=> string(1) "1" }
[1]=> array(3) { [0]=> string(0) "" [22]=> string(11) "Row 2 value" [23]=> string(1) "2" }
This needs to work for any amount of rows i.e. 2 as per the example or 5 or x.
2
Answers
I didn’t understand clearly what you are trying to achieve but there is a simplified version.
preview
Just define your array and then add to it based on the increment: