-1

I have a form that is posting 4 arrays that I need to combine and eventually compose to email. The four arrays:

$quantityArray    = $this->input->post('qty');
$dimensionArray   = $this->input->post('dimension');
$thicknessArray   = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');

will all have the same array length and each index will be related. How do I combine the 4 arrays such as

[0]
     'qty' => '1',
     'dimenesion => '2x2',
     'thickness' => '2in',
     'description' => 'this is the description'
[1]
     'qty' => '1',
     'dimenesion => '2x2',
     'thickness' => '2in',
     'description' => 'this is the description'

I have tried array_combine(), array_merge() and can't get the results that I am looking for.

3
  • 1
    A simple loop can do this. Commented Oct 25, 2013 at 18:12
  • BTW, how do you expect the resulting array ? Both arrays look same. Commented Oct 25, 2013 at 18:13
  • sorry, just copied and pasted the example result. Commented Oct 25, 2013 at 18:17

5 Answers 5

2

If you have same length for those arrays here is example code:

$resultArray = array();
foreach($quantityArray as $index => $qty) {
    $resultArray[$index]['qty'] = $qty;
    $resultArray[$index]['dimenesion'] = $dimensionArray[$index];
    $resultArray[$index]['thickness'] = $thicknessArray[$index];
    $resultArray[$index]['description'] = $descriptionArray [$index];
}

print_r($resultArray);
Sign up to request clarification or add additional context in comments.

Comments

1

This also may work:

<?php
//...
$quantityArray    = $this->input->post('qty');
$dimensionArray   = $this->input->post('dimension');
$thicknessArray   = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');

//
// combine them:
//
$combined = array();
$n = count($quantityArray);
for($i = 0; $i < $n; $i++)
{
  $combined[] = array(
    'qty' => $quantityArray[$i],
    'dimenesion' => $dimensionArray[$i],
    'thickness' => $thicknessArray[$i],
    'description' => $descriptionArray[$i]
  );
}
//
echo "<pre>";
print_r($combined);
echo "</pre>";
?>

Comments

1

If we assume that we have same length for those arrays here is my codes:

$quantityArray = array(1, 1, 5, 3);
$dimensionArray = array("2x2", "3x3", "4x4", "2x2");
$thicknessArray = array("2in", "3in", "4in", "2in");
$descriptionArray = array("this is the description 1", "this is the description 2 ", "this is the description3 ", "this is the description4" );

$myCombinArray = array();
foreach ( $quantityArray as $idx => $val ) {
    $subArray = array (
            'qty' => $quantityArray [$idx],
            'dimenesion' => $dimensionArray [$idx],
            'thickness' => $thicknessArray [$idx],
            'description' => $descriptionArray [$idx] 
    );
    array_push ( $myCombinArray, $subArray );
}
print_r($myCombinArray);

Comments

0

How about an easy way if there are always those 4 arrays:

$quantityArray    = $this->input->post('qty');
$dimensionArray   = $this->input->post('dimension');
$thicknessArray   = $this->input->post('thickness');
$descriptionArray = $this->input->post('description');

$combinedArray = [$quantityArray, $dimensionArray, $thicknessArray, $descriptionArray];

# old syntax:
# $combinedArray = array($quantityArray, $dimensionArray, $thicknessArray, $descriptionArray);

Comments

0

Assuming that all arrays will have the same length, your data transposition and pre-defined associative rows can be concisely implemented using array_map() and get_defined_vars(). Demo

var_export(
    array_map(
        fn($qty, $dimension, $thickness, $description) => get_defined_vars(),
        $this->input->post('qty'),
        $this->input->post('dimension'),
        $this->input->post('thickness'),
        $this->input->post('description')
    )
);

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.