He stackoverflow,
Today I am busy with an function witch get’s all the elements with specific name. Now I have one problem creating this function. The specific name’s are dynamic, so there can be: “conf_1=data&conf_2=data” but also: “conf_1=data&conf_2=data&conf_3=data”
Some code to enlighten you,
foreach($_GET as $key => $value) {
$a++;
if (strpos($key, "conf_$a") === 0) {
$conf[$key] = $value;
}
}
So lets say we have this URL,
naam=name&dom=domain&id=41&conf_1=data&conf_2=data&conf_3=data&this_1=data&this_2=opt1
Now I am trying to get all the conf elements with the foreach loop but I need the $a parameter to be the 1,2 en 3 numbers. And when I try to take all the this elements $a shut give 1 en 2.
How can I declare that or how can I do this with an different loop. The next step is of-course to put the elements into an array like this:
$conf = Array
(
[1] => data
[2] => data
[3] => data
)
$this = Array
(
[1] => data
[2] => data
)
It is important that the numbering is not done automatically. The number in the array shut be the number in the name of the element. Basically conf_1=data has to become [1] => data
I understand that there are multiple ways to do this but I don’ t know witch are the best and the fastest ways. The way I am doing it now is complete wrong:
for($a = 0; $a < 99; $a++){
// Get all the data
$conf = array();
foreach($_GET as $key => $value) {
if (strpos($key, "conf_$a") === 0) {
$conf[$key] = $value;
}
}
$finalconf = array();
//order all data
$finalconf[$a] = $conf['conf_' . $a];
print_r ($conf);
print_r ($finalconf);
}
You can try
Output