0

I have following $_POST array

array(5) {
  ["addcatagory"]=>
  string(8) "CATEGORY"
  ["reg_admin_id"]=>
  string(2) "25"
  ["subcatagory"]=>
  array(2) {
    [0]=>
    string(9) "SUB CAT 1"
    [1]=>
    string(9) "sub cat 2"
  }
  ["subCat_Detais"]=>
  array(2) {
    [0]=>
    string(9) "AAAAAAAAA"
    [1]=>
    string(8) "BBBBBBBB"
  }
  ["submit"]=>
  string(15) "Submit Catagory"
}

and

array(1) {
  ["subCatFile1"]=>
  array(5) {
    ["name"]=>
    array(3) {
      [0]=>
      string(5) "2.jpg"
      [1]=>
      string(5) "3.jpg"
      [2]=>
      string(0) ""
    }
    ["type"]=>
    array(3) {
      [0]=>
      string(10) "image/jpeg"
      [1]=>
      string(10) "image/jpeg"
      [2]=>
      string(0) ""
    }
    ["tmp_name"]=>
    array(3) {
      [0]=>
      string(18) "/var/tmp/phpN5ENy2"
      [1]=>
      string(18) "/var/tmp/phpRyJdcc"
      [2]=>
      string(0) ""
    }
    ["error"]=>
    array(3) {
      [0]=>
      int(0)
      [1]=>
      int(0)
      [2]=>
      int(4)
    }
    ["size"]=>
    array(3) {
      [0]=>
      int(65101)
      [1]=>
      int(49550)
      [2]=>
      int(0)
    }
  }
}

now what i want to achieve is combine 0 index of subcatagory and subcat_details in one array and of 1 index of subcatagory and subcat_details in second array and so on... how can i achieve this?? is it even possible??

Expectations

array( 'name' => 'SUB CAT 1',
       'details' => 'AAAAAAAAA',
       'image_name'=>'2.jpg'
     );

array( 'name' => 'SUB CAT 2',
       'details' => 'BBBBBBB',
       'image_name'=>'2.jpg'
     );
3
  • Please show your expected output. Commented Apr 1, 2016 at 15:39
  • will count($_POST["subcatagory"]) always be the same as count($_POST["subCat_Detais"])? Commented Apr 1, 2016 at 15:41
  • @Sean yeah i will make their count always equal.. Commented Apr 1, 2016 at 15:44

1 Answer 1

1

This can be done with a simple foreach() loop -

$newArray = [];
foreach($_POST["subcatagory"] as $key => $value) {
    $newArray[] = array("name" => $_POST["subcatagory"][$key],
                        "details" => $_POST["subCat_Detais"][$key]);
}

As @CharlotteDunois mentioned, you could also use an for() loop, as long as you have sequential keys, with no keys missing -

$newArray = [];
for($i=0;$i<count($_POST["subcatagory"]);$i++) {
    $newArray[] = array("name" => $_POST["subcatagory"][$i],
                        "details" => $_POST["subCat_Detais"][$i]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Wouldn't a for loop be better here, as you just use the key?
sean if i would like to offer a beer for that. works like a charm ;)
@CharlotteDunois I think 'better' is subjective. I typically do foreach(), as it prevents issues if the arrays don't have sequential keys. If array values are added and/or deleted dynamically via javascript, there is a possibility you would have a missing key.
@Sean how would i do if i want to add $_FILES array also ,see edited question if you can help..
It looks like your $_FILES["subCatFile1"] has the same structure, so you would use $_FILES["subCatFile1"]["name"][$key] (using the $key in the foreach example) or $_FILES["subCatFile1"]["name"][$i] (using the $i in the for example).

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.