0
  ["trnx_date"]=>
  array(2) {
    [0]=>
    string(10) "2017-01-10"
    [1]=>
    string(10) "2017-01-10"
  }
  ["curr_from"]=>
  array(2) {
    [0]=>
    string(3) "USD"
    [1]=>
    string(3) "PHP"
  }
  ["curr_from_amt"]=>
  array(2) {
    [0]=>
    string(8) "4,000.00"
    [1]=>
    string(8) "3,000.00"
  }
  ["curr_to"]=>
  array(2) {
    [0]=>
    string(3) "GBP"
    [1]=>
    string(3) "SAR"
  }
  ["curr_to_amt"]=>
  array(2) {
    [0]=>
    string(8) "3,000.00"
    [1]=>
    string(8) "2,000.00"
  }
  ["amount"]=>
  array(2) {
    [0]=>
    string(8) "7,000.00"
    [1]=>
    string(8) "5,000.00"
  }

I have the above array which was being submitted. This input was in sets of multiple field which was generated by dynamic table row. How can I group this into 1 (one) array so that I could save in the database? Like this:

[cust_row] => array(
  'tranx_date' => "2017-01-10",
  'curr_from' => "USD",
  'curr_from_amt' => "4,000.00",
  'curr_to' => "GBP",
  'curr_to_amt' => "3,000.00",
  'amount' => "7,000.00"
),
[cust_row] => array(
  'tranx_date' => "2017-01-10",
  'curr_from' => "PHP",
  'curr_from_amt' => "3,000.00",
  'curr_to' => "SAR",
  'curr_to_amt' => "2,000.00",
  'amount' => "5,000.00"
),

All of the above we being populated like this:

$trnx_date = $this->input->post('trnx_date');
$curr_from = $this->input->post('curr_from');
$curr_from_amt = $this->input->post('curr_from_amt');
$curr_to = $this->input->post('curr_to');
$curr_to_amt = $this->input->post('curr_to_amt');
$amount = $this->input->post('amount');
6
  • 1
    Have you tried something by yourself? Commented Jan 9, 2017 at 17:35
  • I don't understand the desired row. Why isn't there a key for the second array? Commented Jan 9, 2017 at 17:42
  • Hi Perumal, sorry my bad. it supposed to have the same key for the second array.. Commented Jan 9, 2017 at 17:46
  • <pre>[cust_row] => array( 'tranx_date' => "2017-01-10", 'curr_from' => "USD", 'curr_from_amt' => "4,000.00", 'curr_to' => "GBP", 'curr_to_amt' => "3,000.00", 'amount' => "7,000.00" ), [cust_row] => array( 'tranx_date' => "2017-01-10", 'curr_from' => "PHP", 'curr_from_amt' => "3,000.00", 'curr_to' => "SAR", 'curr_to_amt' => "2,000.00", 'amount' => "5,000.00" ), </pre> Commented Jan 9, 2017 at 17:47
  • You can't have two elements with the same key. Commented Jan 9, 2017 at 17:48

3 Answers 3

1

Assuming all the sub-arrays have the same length, you can use a simple for loop that iterates over the index of one of them, and use that to access each of the sub-arrays at that index. Then combine all of them into the associative array for each customer.

$result = array();
$keys = array_keys($array);
$len = count($array[$keys[0]]); // Get the length of one of the sub-arrays
for ($i = 0; $i < $len; $i++) {
    $new = array();
    foreach ($keys as $k) {
        $new[$k] = $array[$k][$i];
    }
    $result[] = $new;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Barmar, The fields are like this: trnx_date = $this->input->post('trnx_date'); $curr_from = $this->input->post('curr_from'); $curr_from_amt = $this->input->post('curr_from_amt'); $curr_to = $this->input->post('curr_to'); $curr_to_amt = $this->input->post('curr_to_amt'); $amount = $this->input->post('amount'); all above are being populated from a form submission
The question looks like all the inputs are in an associative array, not separate variables.
Looks like you are using CodeIgniter . The nature of the arrays you are returning will depend on how the form is constructed, so one can't say from this detail you've just provided whether Barmar's assumption is a reasonable one. If you have control over the form, you might consider changing the input names to define a more convenient array structure. E.g, <input name="[0][trnx_date]">
0
$arr = array(
    'trnx_date' => array('2017-01-10', '2017-01-10'),
    'curr_from' => array('USD', 'PHP'),
    'curr_from_amt' => array('4,000.00', '3,000.00'),
    'curr_to' => array('GBP', 'SAR'),
    'curr_to_amt' => array('3,000.00', '2,000.00'),
    'amount' => array('7,000.00', '5,000.00')
);

$arr_out = array();
$arr_keys = array_keys($arr);

for($i = 0; $i < count($arr[$arr_keys[0]]); $i++) {
    $new_arr = array();
    foreach($arr_keys as $key => $value) {
        $new_arr[$value] = $arr[$value][$i];
    }
    $arr_out[] = $new_arr;
}

var_dump($arr_out);

Hope it helps!

1 Comment

You're my savior, this what I'm looking for. Thanks a lot.
0

How about this?

// assume $arr is your original data array
$keys = array_keys($arr);
$result = array();
foreach($keys as $key) {
    $vals = $arr[$key];
    foreach($vals as $i =>$val) {
        if (!is_array($result[$i]) {
            $result[$i] = array();
        }
        $result[$i][$key] = $val;
    }
}
var_dump($result);

EDIT: added array check for $result[$i]

Comments

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.