i have an array like this :
$post = array(
"name" => "John",
"user" => "1" ,
"title" => "hello" ,
"uploader_0_name" => "pic.jpg",
"uploader_0_status" => "done",
"uploader_1_name" => "aaaa.jpg",
"uploader_1_status" => "done",
"uploader_2_name" => "Tulips.jpg",
"uploader_2_status" => "failed",
"uploader_count" => "3"
);
i want to have uploader_[/d]_name and uploader_[/d]_name in another array like example :
[0] => Array
(
[name] => pic.jpg
[status] => done
)
[1] => Array
(
[name] => aaaa.jpg
[status] => done
)
[2] => Array
(
[name] => Tulips.jpg
[status] => failed
)
in this case array with index 0 should have uploader_0_name,uploader_0_status
i tried a lot to do this with preg_match in foreach loop , but i could not be successful
foreach ( $post as $key => $value ) {
$pattern = "/^uploader_[\d]_(name|status)$/";
preg_match( $pattern , $key ,$matches[]);
}
P.S : Unfortunately today i seen the best answer and the best way was deleted ,so i added it , if any one have problem like this , can use :
foreach ($post as $key => $value) {
if (preg_match('/^uploader_(\d)_(name|status)$/', $key, $matches)) {
$result[$matches[1]][$matches[2]] = $value;
}
}