create array from multiple arrays & submit in mysql table - php

I am trying to fetch values from few - dynamically created HTML FORM & in action file, i am grabbing those values via $_POST. Please consider humbly, that i am using array in dynamic inputs like :
<input name="array1[]" />
<input name="array2[]" />
<input name="array3[]" />
So, that, after FORM SUBMIT, in action file, its giving :
$a = Array
(
[0] => 04/21/2017
[1] => 04/19/2017
[2] => 04/25/2017
[3] => 04/25/2017
[4] => 10/25/2017
)
$b= Array
(
[0] => 11
[1] => 34
[2] => 12
[3] => 12
[4] => 2
)
$c= Array
(
[0] => fghgthg
[1] => ggfg
[2] => fgfgfdgf
[3] => fgfdgdgfgdh
[4] => rgrgfgf
)
Now, m having troubles in arranging this way :
$FinalArray = array(
array($a[0], $b[0], $c[0]),
array($a[1], $b[1], $c[1]),
array($a[2], $b[2], $c[2]),
array($a[3], $b[3], $c[3]),
......last line without comma
);
And submitting in mysql table, so that i can easily retrieve it like :
step-1 : $FinalArray[0];
step-2 : $FinalArray[1];
.......goes on
Thanks, for help, in Advance.

This simple foreach help you in achieving your desired output.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$a = Array
(
0 => "04/21/2017",
1 => "04/19/2017",
2 => "04/25/2017",
3 => '04/25/2017',
4 => "10/25/2017"
);
$b = Array
(
0 => 11,
1 => 34,
2 => 12,
3 => 12,
4 => 2
);
$c = Array
(
0 => "fghgthg",
1 => "ggfg",
2 => "fgfgfdgf",
3 => "fgfdgdgfgdh",
4 => "rgrgfgf",
);
$result=array();
foreach($a as $key => $value)
{
$result[]=array($value,$b[$key],$c[$key]);
}
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 04/21/2017
[1] => 11
[2] => fghgthg
)
[1] => Array
(
[0] => 04/19/2017
[1] => 34
[2] => ggfg
)
[2] => Array
(
[0] => 04/25/2017
[1] => 12
[2] => fgfgfdgf
)
[3] => Array
(
[0] => 04/25/2017
[1] => 12
[2] => fgfdgdgfgdh
)
[4] => Array
(
[0] => 10/25/2017
[1] => 2
[2] => rgrgfgf
)
)

Related

How to group posted array names by index in PHP

I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);

Creating a 2D array moves data to key position

I declare the following array
$job_scope = array( "proposal_id",
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope",
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
I expect it to be created like
array([0] => 'proposal_id',
[1] => 'will_provide' => array([0] => "0",
[1] => "Supervision",
[2] => "Labor",
[3] => "Material",
[4] => "Equpment"),
[2] => 'general_scope',
[3] => 'per_bid' => array([0] => "Yes",
[1] => "No",
[2] => "Omit"),
[4] => 'job_type' => array([0] => "Painting",
[1] => "Sandblasting",
[2] => "Scappling")
But when I print the array it looks like
Array ( [0] => proposal_id [will_provide] => Array (
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment )
[1] => general_scope [per_bid] => Array (
[0] => Yes
[1] => No
[2] => Omit )
[job_type] => Array (
[0] => Painting
[1] => Sandblasting
[2] => Scappling )
I would like the array to be created in the same format as the second section of code.
All you need to do is assign an empty array to the proposal_id and general_scope. So the code will look like this
$job_scope = array( "proposal_id" => array(),
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope" => array(),
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
It will produce this array
Array (
[proposal_id] => Array ( )
[will_provide] => Array ( [0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
[general_scope] => Array ( )
[per_bid] => Array ( [0] => Yes
[1] => No
[2] => Omit
)
[job_type] => Array ( [0] => Painting
[1] => Sandblasting
[2] => Scappling
))
If you want to callback the value, (ex : call supervision value).
All you need to do is
print_r($job_scope['will_provide'][1])
and that will print the supervision value
use $new_job_scope = array_values($job_scope);
$job_scope = array(
"proposal_id",
"will_provide" => array(
"0",
"Supervision",
"Labor",
"Material",
"Equpment"
),
"general_scope",
"per_bid" => array(
"Yes",
"No",
"Omit"
),
"job_type" => array(
"Painting",
"Sandblasting",
"Scappling"
)
);
$new_job_scope = array_values($job_scope);
print_r($new_job_scope);
PhpFiddle
Create array first !!! Reassign at specified index with 2D array will be more clear to me
<?php
$arr = array('proposal_id','','general_scope','',''); //create array first
$arr[1] = array("will_provide" => array("0","Supervision","Labor","Material","Equpment"));
$arr[3] = array("per_bid" => array("Yes","No", "Omit"));
$arr[4] = array("job_type" => array("Painting","Sandblasting","Scappling"));
var_dump($arr);
?>
I think this process can serve you. I have just used a foreach loop to convert non-int key to int key:
$new_array = '';
foreach($job_scope as $k => $v){
if(is_int($k)){
$new_array[] = $v;
}else{
$new_array[] = [$k => $v];
}
}
print_r($new_array);
Output would be:
Array
(
[0] => proposal_id
[1] => Array
(
[will_provide] => Array
(
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
)
[2] => general_scope
[3] => Array
(
[per_bid] => Array
(
[0] => Yes
[1] => No
[2] => Omit
)
)
[4] => Array
(
[job_type] => Array
(
[0] => Painting
[1] => Sandblasting
[2] => Scappling
)
)
)

Rearrange array values according to another array values in php

I have got 2 arrays(One single and one multidimensional).
Single array "A" looks like
[questionid] => Array
(
[0] => 12
[1] => 13
[2] => 55
[3] => 15
[4] => 16
)
Multidimensional array "B" looks like
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 15
[answer] =>
)
[3] => Array
(
[quid] => 16
[answer] =>
)
[4] => Array
(
[quid] => 55
[answer] =>
)
)
Now I want the array B (quid) values to be rearranged depending upon the values from array A. So in array B the value of quid last element(55) is at the very end whereas in array A it is in 3rd position.
I want the array B look like this
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 55
[answer] =>
)
[3] => Array
(
[quid] => 15
[answer] =>
)
[4] => Array
(
[quid] => 16
[answer] =>
)
)
The code for multidimensional array is
$ansid = array
(
array
(
"quid" => 12,
"answer" => "AAA"
),
array
(
"quid" => 13,
"answer" => "neighbour"
),
array
(
"quid" => 15,
"answer" =>""
),
array
(
"quid" => 16,
"answer" =>""
),
array
(
"quid" => 55,
"answer" =>""
)
);
Not using array_walk() as to be mor demonstrative, you could just
$newB=array()
foreach ($arrayB as $b) $newB[$b['quid']]=$b;
$newA=array()
foreach ($arrayA as $k=>$v) $newA[$k]=$newB[$v]
//$newA has the required structure
With the user sort function:
$single_array = ...; // order by the index of this array
$mult_dim_array = ...; // to be ordered by the 'quid' value of the elements
function my_comp($a, $b) {
return array_search($a['quid'], $single_array ) - array_search($b['quid'], $single_array );
}
usort($mult_dim_array, "my_comp");
This will get the index on your first array to determine which element goes first or later. The function reads $single_array as a global variable (defined outside the function).
Documentation at http://php.net/manual/en/function.usort.php

help to optimize a function in php

i have created a function in php to convert an string like:
[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 20110924, [20110926, 20110927], 20110929]
to php array like:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
[8] => Array
(
[0] => 20111001
[1] => 20111002
)
[9] => 20111004
[10] => Array
(
[0] => 20111006
[1] => 20111007
)
)
The function is:
function dates2Array($d){
if($d!==''){
$d=substr($d, 1, strlen($d)-2);
$d=explode(', ', $d);
$dates=array();
if(!empty($d)){
$j=1;
foreach($d as $k=>$v){
if(substr($v, 0, 1)==='[') $dates[]=array(substr($v, 1, strlen($v)));
elseif(substr($v, strlen($v)-1, strlen($v))===']'){
$dates[$k-$j][1]=substr($v, 0, strlen($v)-1);
$j++;
}
else $dates[]=$v;
}
}
}
return $d!==''?$dates:'';
}
I am not fully happy with my function.
I think it can be more optimized and compressed for speed..
Can it be?
Use JSON (json_decode() and json_encode()) instead
http://sandbox.phpcode.eu/g/b3814/2
result:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
)
You can pass your string in {...} and pass it to json_decode(str, true) to get back an array.
>> json_decode("[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 2
0110924, [20110926, 20110927], 20110929]")
array (
0 => 20110911,
1 => 20110913,
2 =>
array (
0 => 20110915,
1 => 20110918,
),
3 => 20110920,
4 => 20110922,
5 => 20110924,
6 =>
array (
0 => 20110926,
1 => 20110927,
),
7 => 20110929,
)

how to merge key array on array

and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.

Categories