Programatically Produce a code in PHP

$values = shortcode_atts( array(
    'heading'	=> '',
    'title1'   	=> 'The Headline',
    'title2'   	=> 'The Headline',  
), $atts );

Currently, it is hardcoded.

Is there a possibility that I can generate lines programmatically?

'title2' => 'The Headline', → 10 times, uptil
.
.
.
.
.
'title10' => 'The Headline',

My approach - while loop should work here →

$x = 1;
while ($x <= 10) {
    'title2' => 'The Headline',
}

But how to change the numerical value in this in serial order →
'title2'

$data = [
   'heading' => '',
];

for ($x = 1; $x <= 10; $x++) {
    $data['title'.$x] = 'The Headline';
}

$values = shortcode_atts($data);

The fact that you need this though indicates to me that there is probably a problem in the code that uses this (probably a template?) and whatever your problem is should probably be solved there.

Solve the cause of the problem, don’t fight the symptoms.

2 Likes

I am trying to create a free plugin for “Frequently asked question”, but the user should have an option how many inputs he wants.

This should be →

$values = shortcode_atts($data, $atts); I think instead of →

$values = shortcode_atts($data);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.