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_combined, array_merged and can't get the results that I am looking for. Thanks for the help on this.
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);
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>";
?>
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);
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);
Related
I am looping though one initial array, and specifying values i want. Then i am storing the values i need in an array. Then i am combining those values into a new array, with keys and values. The new array is only storing the last entry of all the data that has been passed to it.
$exampleArray = array();
for ($i = 0; $i < 100; $i++){
$exampleArray[] = array(
$A1 = $Anotherarray[$i][25],
$A2 = $Anotherarray[$i][26],
$A3 = $Anotherarray[$i][24],
$A4 = $Anotherarray[$i][27],
$A5 = $Anotherarray[$i][28]
);
$secondExample = array();
foreach( $A1 as $i => $val )
{
$secondExample[] = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$i]),
"Field3" => ucfirst($A3[$i]),
"Field4" => ucfirst($A4[$i]),
"Field5" => ucfirst($A5[$i])
);
}
You are declaring $secondExample as a new array on every iteration. Do it like this:
$exampleArray = array();
$secondExample = array();
for ($i = 0; $i < 100; $i++){
$exampleArray[] = array(
$A1 = $Anotherarray[$i][25],
$A2 = $Anotherarray[$i][26],
$A3 = $Anotherarray[$i][24],
$A4 = $Anotherarray[$i][27],
$A5 = $Anotherarray[$i][28]
);
$secondExample[$i] = array();
foreach( $A1 as $j => $val) {
$secondExample[$i][] = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$j]),
"Field3" => ucfirst($A3[$j]),
"Field4" => ucfirst($A4[$j]),
"Field5" => ucfirst($A5[$j])
);
}
As because you are overwriting every time the loop goes.Use array_push
$secondExample = array();
foreach( $A1 as $i => $val )
{
$varArray = array(
"Field1" => $val,
"Field2" => ucfirst($A2[$i]),
"Field3" => ucfirst($A3[$i]),
"Field4" => ucfirst($A4[$i]),
"Field5" => ucfirst($A5[$i])
);
array_push($secondExample, $varArray);
}
I am trying to merge two arrays, but getting NULL. Below is my code
$a = 1;
foreach($codes as $values) {
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$this->data['output' . $a++] = $this->my_modal->simple_post($post_data);
}
$this->data['output'] = array_merge($this->data['output1'], $this->data['output2']);
var_dump($this->data['output']);
Any suggestions will be appreciated. Thanks..
You have to delete the first parameter (NULL) of array_merge();
And what is $this->input->$id? Don't you mean $id?
And in this environment, it's better to use array_push();:
$a = 1;
$this->data['output'] = array();
foreach($codes as $values)
{
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$new_data = $this->my_modal->simple_post($post_data);
array_push($this->data['output'], $new_data);
}
var_dump($this->data['output']);
$a = 1;
$this->data['output'] = array();
foreach($codes as $values){
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$data['output2']= $this->my_modal->simple_post($post_data);
if(count($this->data['output1']) > 1) {
$this->data['all'] = array_merge($this->data['output1'],$data['output2']);
}else {
$this->data['all'] = $data['output1'];
}
}
print_r($this->data['all']);
Your code is perfectly right, the only problem is that you start the counter with $a = 1, and do $a++ which will result in 2. So the output1 does not exist. However if you write (notice the subtle change):
$a = 1;
foreach($codes as $values) {
$id = $values['id'];
$post_data = array (
"id" => $id,
"name" => $this->input->post('Name'),
"from_date" => $this->input->post('FromDate'),
"to_date" => $this->input->post('ToDate')
);
$this->data['output' . $a] = $this->my_modal->simple_post($post_data); // $a = 1 now
$a++; // $a becomes 2 here
}
$this->data['output'] = array_merge($this->data['output1'], $this->data['output2']);
var_dump($this->data['output']);
I am trying get user value from multidimensional array as
$array = array();
$array["id"] = "1";
$array["name"] = "name1";
$array["country"] = "country1";
$array["id"] = "2";
$array["name"] = "name2";
$array["country"] = "country2";
$array["id"] = "3";
$array["name"] = "name3";
$array["country"] = "country3";
$array["id"] = "4";
$array["name"] = "name4";
$array["country"] = "country4";
foreach($array as $e){
print_r($e);
}
It return me 4name4country4 only
I need to fetch rows like
foreach($array as $e){
$id=$e['id'];
$name=$e['name'];
$country=$e['country'];
echo $id.'/'.$name.'/'.$country.'<br>';
}
but this gives me error as Illegal string offset 'id'
from what I understood about array this should return all values, Please see why this simple array is not working and suggest any way to do it
Currently you are overwriting the keys. Need to add the keys properly. You have to build the array like -
$array[0]["id"] = "1";
$array[0]["name"] = "name1";
$array[0]["country"] = "country1";
$array[1]["id"] = "2";
$array[1]["name"] = "name2";
$array[1]["country"] = "country2";
OR
$array = array(
0 => array('id' => 1, 'name' => 'name1', 'country' => 'country1'),
1 => array('id' => 2, 'name' => 'name2', 'country' => 'country2'),
);
Instead, do it like so you won't have to give array keys manually
$array = array();
$array[] = array("id" => 123, "name" => "Your name", "country" => "UK");
$array[] = array("id" => 1342, "name" => "Your name 2 ", "country" => "UK");
then in foreach do this
foreach($array as $key => $val){
echo $key. ": ".$val['id']. " " . $val['name'];
}
You have to create the multidimensional array like this, right now you're overwriting the array multiple times.
$arrays = [
[0]=>
["id"] => "1",
["name"] => "name1",
["country"] => "country1"
],
[1]=>[
...
]
];
foreach($arrays as $array){
$id=$array['id'];
$name=$array['name'];
$country=$array['country'];
echo $id.'/'.$name.'/'.'$country'.'<br>';
}
My code just populates the last array in the array $workshops
my loop function populates array $workshops, like so
while (ECSE_purchase::have_products()) {
ECSE_purchase::the_product();
$prodid = ECSE_purchase_product::get_the_ID();
$workshop_data = ETICKETDATA::get_workshop_details_helper($prodid);
$workshops = array();
if($prodid == 565) {
............
} else {
$workshopsTmp = array();
$workshopsTmp['workshop_barcode'] = $data['workshop_barcode'];
$workshopsTmp['workshop_id'] = ECSE_purchase_product::get_the_ID();
$workshopsTmp['workshop_title'] = ECSE_purchase_product::get_the_name();
$workshopsTmp['workshop_time'] = $workshop_data['time'];
$workshopsTmp['workshop_room'] = $workshop_data['room'];
$workshopsTmp['workshop_num_of_tickets'] = ECSE_purchase_product::get_the_QTY();
$workshops[] = $workshopsTmp;
}
But here i just get the last array and not like this:
$workshops = array(array('workshop_barcode' => '0101010101',
'workshop_id' => '589',
'workshop_title' => 'Swimming',
'workshop_time' => '12:00',
'workshop_room' => 'Room 1',
'workshop_num_of_tickets' => '2'),
array('workshop_barcode' => '03030303003',
'workshop_id' => '568',
'workshop_title' => 'Running',
'workshop_time' => '15:00',
'workshop_room' => 'Room 3',
'workshop_num_of_tickets' => '3'),
array('workshop_barcode' => '0505050505',
'workshop_id' => '570',
'workshop_title' => 'Biking',
'workshop_time' => '16:00',
'workshop_room' => 'Room 2',
'workshop_num_of_tickets' => '2'));
Any pointers appreciated.
regards,
just edit the loop
You keep redefining your initial array. Try putting $workshops array before while loop
$workshops = array();
while (ECSE_purchase::have_products()) {
ECSE_purchase::the_product();
$prodid = ECSE_purchase_product::get_the_ID();
$workshop_data = ETICKETDATA::get_workshop_details_helper($prodid);
if($prodid == 565) {
............
} else {
$workshopsTmp = array();
$workshopsTmp['workshop_barcode'] = $data['workshop_barcode'];
$workshopsTmp['workshop_id'] = ECSE_purchase_product::get_the_ID();
$workshopsTmp['workshop_title'] = ECSE_purchase_product::get_the_name();
$workshopsTmp['workshop_time'] = $workshop_data['time'];
$workshopsTmp['workshop_room'] = $workshop_data['room'];
$workshopsTmp['workshop_num_of_tickets'] = ECSE_purchase_product::get_the_QTY();
$workshops[] = $workshopsTmp;
}
i've got the following array:
$comments = array();
$comments[] = array('member_id' => '17',
'time' => '2011-05-10 11:10:00',
'name' => 'John Smith',
'comment' => 'Test Comment 1');
$comments[] = array('member_id' => '25',
'time' => '2011-05-10 11:26:00',
'name' => 'David Jones',
'comment' => 'Test Comment 2');
$comments[] = array('member_id' => '17',
'time' => '2011-05-10 13:15:00',
'name' => 'John Smith',
'comment' => 'Test Comment 3');
How would i go about grouping it by member_id? So I'll be able to display the comments on the page with the following formatting:
John Smith(2 comments)
2011-05-10 11:10:00 | Test Comment 1
2011-05-10 13:15:00 | Test Comment 3
David Jones(1 comment)
2011-05-10 11:26:00 | Test Comment 2
One solution is to sort them by the name field (check out usort for that), but even easier might be to just populate a new array in this way:
$grouped = array();
foreach($comments as $c) {
if(!isset($grouped[$c['name']]) {
$grouped[$c['name']] = array();
}
$grouped[$c['name']][] = $c;
}
//Now it's just a matter of a double foreach to print them out:
foreach($grouped as $name => $group) {
//print header here
echo $name, "<br>\n";
foreach($group as $c) {
//print each comment here
}
}
I would suggest using a second grouping array
$groups[] = array();
foreach( $comment as $k=>$v ) {
$groups[$v['member_id']][] = $k
}
And then to print it
foreach( $group as $m_id=>$arr ) {
echo "Group $m_id<br/>\n";
foreach( $arr as $k ) {
echo $comment[$k]."<br/>\n";
}
}
You should try with taking multi-dimensional array.
$comment_groups[] = array();
$m_id = '';
foreach( $comment_groups as $key=>$val ) {
if($key == 'member_id'){
$m_id = $val;
}
$comment_groups[$m_id]][] = $val;
}
Then you can print as you want to display.