1

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;

    }
}
2
  • 1
    Show us what you have tried (code wise). Commented Jun 5, 2014 at 21:38
  • @AzizSaleh i added my code , i tried a lot with different types , it's part of my code Commented Jun 5, 2014 at 21:43

2 Answers 2

2

try this if you dont want to use regular expressions:

$newArr = array();

foreach($post as $key => $val) {
   $newKey = explode("_", $key);

   if (count($newKey) > 2) {
      //this is the status
      $innerValue = array_pop($newKey);

      //this is the numeric ID _2_ for example
      $innerKey   = array_pop($newKey);

      $newArr[$innerKey][$innerValue] = $val;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

There is a simple way to do this do not use hard structures:

$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"
);

//result array;
$arr = array();
//counter
$n = 0;

foreach ($post as $key => $value) {

 if(strpos($key, '_name') != false){

    $arr[$n]['name'] = $value;

 }elseif(strpos($key, '_status') != false){

    $arr[$n]['status'] = $value;
    $n++;
 }

}

print_r($arr);

4 Comments

The OP needs to verify the format of the key before creating the array. The current strpos() checks are not really useful, because it would return true for all cases such as _name_SOMETHINGUNWANTED_____status It is correct to use preg_match() here, in my opinion.
You have array above and this array don't have names with some '_name_SOMETHINGUNWANTED_____status' so no need use preg_match because every time in loop we lose performance
"preg_match() lose performance in loop" — Do you have proof to support your argument? As I said in my other comment, the performance difference wouldn't be noticeable for a small array such as this one. The manual excerpt you quoted is not applicable here because the OP is not just searching for something in the key — they are extracting/capturing that information.
Perhaps you're right;-) For small arrays are not affected.

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.