From MySQL join query I got result like:
Array ( [0] => c1 [1] => ot1 [2] => ot1 [3] => R )
Array ( [0] => 20 [1] => 10 [2] => 15 [3] => 5 )
But I want result like:
c1 = [20]
ot1 = [10,15]
R = [5]
Please help me.
Assuming
$array1 = array( 0 => 'c1', 1 => 'ot1', 2 => 'ot1' 3 => 'R' )
$array2 = array( 0 => 20, 1 => 10, 2 => 15, 3 => 5 )
Try this:
for($i = 0; $i < count($array1); $i++){
if(!isset(${$array1[$i]}))
${$array1[$i]} = [];
${$array1[$i]}[] = $array2[$i];
}
Note that this works only when keys are continous integers starting from 0 and both arrays have the same dimension
Assuming the first array is called $array1 and the other is called $array2 try this:
$merged = []; //new array for merged values
foreach ($array1 as $key => $value) { //iterate over the array which holds keys
if (!isset($merged[$value])) { //if a key does not exist in new array yet
$merged[$value] = []; //add it
}
$merged[$value][] = $array2[$key]; //add new value from the other array under this key
}
as long as the integrity of your arrays is preserved, there are no restrictions about continuous keys like in the other answer
Use this one
$array1 = array('c1', 'ot1', 'ot1', 'R');
$array2 = array(20, 10, 15, 5);
$array3 = array();
for ($i = 0; $i < count($array1); $i++) {
if (array_key_exists($array1[$i], $array3)) {
$array3[$array1[$i]] = array($array3[$array1[$i]], $array2[$i]);
} else {
$array3[$array1[$i]] = $array2[$i];
}
}
I have a multiple row dynamic table that I created using php and jQuery. Here's the link to view the table.
Everything is working fine except when I insert the data into the database, the serial numbers do not save sequentially. My insert queries are as below:
for($i = 0; $i < count($_POST['C_Objectives']); $i++)
{
$sql = "INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,Measures,Targets,subtotal,Corporate_Objective,Row_Number,ID) Values ('$formno','||<==','==','==','".$_POST['SubTotals'][$i]."','".$_POST['C_Objectives'][$i]."','".$_POST['SNo'][$i]."','$statement')";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false)
die(print_r(sqlsrv_errors(), true));
else
echo " ";
}
for($i = 0; $i < count($_POST['Measures']); $i++)
{
$sql = "INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,Measures,Targets,Weightage,Row_Number,target_date,ID) VALUES ('$formno','".$_POST['Objectives'][$i]."','".$_POST['Measures'][$i]."','".$_POST['Achievement'][$i]."','".$_POST['Weightage_Target'][$i]."','".$_POST['SNo'][$i]."','".$_POST['Date_Target'][$i]."','$statement')";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false)
die(print_r(sqlsrv_errors(), true));
else
echo " ";
}
The serial number is saved in the column Row_Number, using $_POST['SNo'][$i]. Is it possible to save both of the dynamic rows using 1 insert query so that the serial numbers are saved sequentially?
This is the $_POST array result:
[Row_Number] => Array
(
[0] => 1
[1] => 2
)
[C_Objectives] => Array
(
[0] => A
[1] => B
)
[Objectives] => Array
(
[0] => a1
[1] => a4
[2] => a7
[3] => b1
)
[Measures] => Array
(
[0] => a2
[1] => a5
[2] => a8
[3] => b2
)
[Achievement] => Array
(
[0] => a3
[1] => a6
[2] => a9
[3] => b3
)
[Date_Target] => Array
(
[0] => 2016-09-09
[1] => 2016-09-09
[2] => 2016-09-09
[3] => 2016-09-09
)
[Weightage_Target] => Array
(
[0] => 25
[1] => 25
[2] => 25
[3] => 25
)
[SNo] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
)
[SubTotals] => Array
(
[0] => 75
[1] => 25
)
[GrandTotal] => 100
)
I've also tried making the column auto-increment, but yet doesn't save the data in the same order as it is entered in the front end.
Your inserting has performance issue. Please change your way for inserting to the database. You can do all of them in one query. Even if you have 20 loop for first "for" and 20 loop for 2nd "for".
Answer to What you asked
If you want to insert by $_POST['SNo'] order, change this line
for($i = 0; $i < count($_POST['C_Objectives']); $i++)
to the
foreach($_POST['SNo'] as $i)
If you need multiple insert at once, just do this:
INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,...)
VALUES (Value1,Value2,...), (Value1,Value2,...)
This is What you MUST do
In your code, you did the same query in 6 queries. It can even be more than 6 with more $_POST['Measures'] or $_POST['C_Objectives'] array length.
You need to Put them in one query and when you don't need to set the value, just set it to the column default value. for example NULL
Something like this:
//first we create $values array. it contains all values that you need to insert to the db
$values = array();
$j=0;
for($i = 0; $i < count($_POST['C_Objectives']); $i++){
$values[$j]['Serial_Number'] = $formno;
$values[$j]['Objectives'] = '||<==';
//and fill others here
//fill all cols that you wrote inside your query with the correct order
$j++;
}
for($i = 0; $i < count($_POST['Measures']); $i++){
$values[$j]['Serial_Number'] = $formno;
$values[$j]['Objectives'] = $_POST['Objectives'][$i];
//and fill others here
//fill all cols that you wrote inside your query with the correct order
$j++;
}
//now create (value1,value2,...),(value1,value2,...),...
$query = NULL;
foreach($values as $value){
$tmp = NULL;
foreach($value as $v){
$tmp .= ($v=='')? 'NULL,' : "'$v',";
}
$tmp = rtrim($tmp,',');
$query .= "($tmp),";
}
$query = rtrim($query,',');
//Now Insert
$sql = "INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,...) VALUES $query";
In this example I just showed you how to do it. Remember, you must check $v and prepare it by your column type. check if $_POST[KEY] is set and it's array. Don't insert to the database if $query is empty.
Very Important about your codes
If this is not your original code there is no problem but if it is, please change the way you are using $_POST inside your query. It has very low security. at least you need to validate them before using it.
Yes you can able to insert in single insert query.
$arrMeasuresInsData = array();
for($i = 0; $i < count($_POST['C_Objectives']); $i++) {
$sql = "INSERT INTO Appraisal_Objectives (Serial_Number, Objectives, Measures, Targets, subtotal, Corporate_Objective, Row_Number, ID, Weightagen, target_date)
Values ('$formno',
'||<==',
'==',
'==',
'".$_POST['SubTotals'][$i]."',
'".$_POST['C_Objectives'][$i]."',
'".$_POST['SNo'][$i]."',
'$statement',
'',
'')";
if(!empty($_POST['Measures'][$i])) {
$arrMeasuresInsData[$i] = $_POST['Measures'][$i];
$sql .= ",('$formno',
'".$_POST['Objectives'][$i]."',
'".$_POST['Measures'][$i]."',
'".$_POST['Achievement'][$i]."',
'',
'',
'".$_POST['SNo'][$i]."',
'".$_POST['Date_Target'][$i]."',
'".$_POST['Weightage_Target'][$i]."',
'$statement',)";
}
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false)
{
die(print_r(sqlsrv_errors(), true));
}
else
{
echo " ";
}
}
for($i = 0; $i < count($_POST['Measures']); $i++) {
if(isset($arrMeasuresInsData[$i])) {
continue;
}
$sql="INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,Measures,Targets,Weightage,Row_Number,target_date,ID)
VALUES ('$formno',
'".$_POST['Objectives'][$i]."',
'".$_POST['Measures'][$i]."',
'".$_POST['Achievement'][$i]."',
'".$_POST['Weightage_Target'][$i]."',
'".$_POST['SNo'][$i]."',
'".$_POST['Date_Target'][$i]."',
'$statement')";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false)
{
die(print_r(sqlsrv_errors(), true));
}
else
{
echo " ";
}
}
I think you are getting your $_POST array wrongly. You have to change the input form and recieve the input some thing like below:
[C_Objectives] => Array
(
[Row_Number] => Array
(
[title] => 'xxx',
[0] => Array
(
[0] => Array
(
[SNo] => 2
[Objectives] => a1,
[Measures] => a2,
[Achievement] => a3,
[Date_Target] => 2016-09-09,
[Weightage_Target] => 25
),
(
[SNo] => 3
[Objectives] => a1,
[Measures] => a2,
[Achievement] => a3,
[Date_Target] => 2016-09-09,
[Weightage_Target] => 25
),
(
[SNo] => 4
[Objectives] => a1,
[Measures] => a2,
[Achievement] => a3,
[Date_Target] => 2016-09-09,
[Weightage_Target] => 25
),
[SubTotals] => 75
)
)
},
(
[Row_Number] => Array
(
[title] => 'xxx',
[0] => Array
(
[0] => Array
(
[SNo] => 6
[Objectives] => a1,
[Measures] => a2,
[Achievement] => a3,
[Date_Target] => 2016-09-09,
[Weightage_Target] => 25
),
[SubTotals] => 25
)
)
)
Above is the only example rest you have to understand how to do that.
As it would be difficlut to know which value belong to which row it might be possible that value defined as 2nd below to 3rd row.
With the current code, there will be at least two INSERT statements, and more when $_POST['Measures'] or $_POST['C_Objectives'] contain a larger number of elements.
You can insert multiple records with one statement, and instead of using a for statement, use a foreach so you don't have to do the bookkeeping on the iterator variable. Then store values in arrays and use implode() to combine the sets of values for each record.
Check which values are being inserted into which columns - it appears that in the first for loop of your example, you are inserting the value from $_POST['SNo'][$i] into the ID field...
$values = array();
foreach($_POST['C_Objectives'] as $index=>$value) {
$rowValues = array();
$rowValues[] = $_POST['SNo'][$index]; //Serial_Number
array_push($rowValues,$formno,'||<==','==','=='); //, Objectives, Measures, Targets, subtotal
$rowValues[] = $_POST['SubTotals'][$index]; //Corporate_Objective
$rowValues[] = $value; //Row_Number: $value == $_POST['C_Objectives'][$index];
$values[] = "('".implode("', '",$rowValues )."')";
}
$fields = array('Objectives','Measures','Achievement','Weightage_Target','SNo','Date_Target');
foreach($_POST['Measures'] as $index=>$value) {
$rowValues = array($formno);
foreach($fields as $field) {
$rowValues[] = $_POST[$field][$index];
}
$values[] = "('".implode("', '",$rowValues )."')";
}
$sql = "INSERT INTO Appraisal_Objectives (Serial_Number,Objectives,Measures,Targets,subtotal,Corporate_Objective,Row_Number,ID) VALUES ".implode(', ',$values);
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
else {
echo " ";
}
what are you going to do? Two loop execution is different insertion .....
Solution:
1. The second update operation.
2. organize the data into the database at once.
i have two arrays
$value_array = array('50','40','30','20','10');
$customer = array('300','200','100');
i want to distribute the value array to customers based on the value of customers that is taken as limit.adding values by checking it wont cross the limit that is 300 , 200 and 100.
but customer array not working one direction it should work first forward and then backward like that
i want to produce an array in form of
Array
(
[0] => Array
(
[0] => 50
)
[1] => Array
(
[0] => 40
[1] => 10
)
[2] => Array
(
[0] => 30
[1] => 20
)
)
After completing customer loop first time it should start from last to first. both array count will change , i mean count.
value array should check 50 -> 300 , 40->200, 30->100 then from last ie, 20 ->100, 10->200 etc.
I tried like
$i = 0;
while($i < count($customer)){
foreach($value_array as $k=>$value){
$v = 0;
if($value <= $customer[$i]){
$customer2[$i][] = $value;
unset($value_array[$k]);
$v = 1;
}
if($v ==1){
break;
}
}
//echo $i."<br/>";
if($i == (count($customer)-1) && (!empty($value_array))){
$i = 0;
$customer = array_reverse($customer, true);
}
$i++;
}
echo "<pre>";
print_r($customer2);
$valueArray = array('50','40','30','20','10','0','-11');
$customer = array('300','200','100');
function parse(array $valueArr, array $customerArr)
{
$customerCount = count($customerArr);
$chunkedValueArr = array_chunk($valueArr, $customerCount);
$temp = array_fill(0, $customerCount, array());
$i = 0;
foreach ($chunkedValueArr as $item) {
foreach ($item as $key => $value) {
$temp[$key][] = $value;
}
$temp = rotateArray($temp);
$i++;
}
// if $i is odd
if ($i & 1) {
$temp = rotateArray($temp);
}
return $temp;
}
function rotateArray(array $arr)
{
$rotatedArr = array();
//set the pointer to the last element and add it to the second array
array_push($rotatedArr, end($arr));
//while we have items, get the previous item and add it to the second array
for($i=0; $i<sizeof($arr)-1; $i++){
array_push($rotatedArr, prev($arr));
}
return $rotatedArr;
}
print_r(parse($valueArray, $customer));
returns:
Array
(
[0] => Array
(
[0] => 50
[1] => 0
[2] => -11
)
[1] => Array
(
[0] => 40
[1] => 10
)
[2] => Array
(
[0] => 30
[1] => 20
)
)
I've got an array with about 40 keys. I'd like to have a small function that returns a summary array.
Right now I've got the following that works:
foreach ($all_data as $value){
$new_array[ $value['location'] ][ $value['manufacturer'] ][ $value['model'] ] += 1;
}
This returns an array with everything I need. However, the location, manufacturer and model could be changed up for a bunch of other values.
what I am trying to do is have something simple as:
$new_array = summarize($all_data,array('location','manufacturer','model','count'),array('list','list','list','count') );}
where this summarize function would build the call. I think I just need a bit of help on how to get it to run the string as code for this array. Otherwise I get
$current_selection = "[ $row_item['location'] ][ $row_item['manufacturer'] ][ $row_item['model'] ]"
$return_array{$current_selection} += 1;
Where the end goal is to have a function like:
function summarize($data_array, $fields_array, $process_array){
//data_array = associative multi-dimensional data array
//fields = values to pull from the data_array
//process = array specifying whether to list, sum, count, average, max, min
$return_array = array();
$current_selection = "";
foreach($fields_array as $field){
$current_selection .= '[ $row_item[\'' . $field . '\'] ]';
}
foreach ($data_array as $row_item){
//dynamic = DOES NOT WORK
$return_array[$current_selection] += 1;//eval? create function? abstract?
//another attempt
${'return_array' . $current_selection} += 1;
//Manual = Does work
//$return_array[ $row_item['location'] ][ $row_item['manufacturer'] ][ $row_item['model'] ] += 1;
}
}
Thanks for any help on how to do an indirect reference.
JC
RESOLUTION
The final version that managed to resolve this looks like the following, thanks to user: check, for getting me on the correct path.
function summarize($data_array, $fields_array, $process_array){
$return_array = array();
$i = 0;
foreach ($data_array as $row){
$ii = 0;
$temp = array();
$temp2 = array();
foreach($fields_array as $key=>$field){
if($process_array[$ii] == 'list') $temp[$ii] = $row[$field];
if($process_array[$ii] == 'count') $temp2[$ii] = 1;
if($process_array[$ii] == 'sum') $temp2[$ii] = $row[$field];
$ii++;
}
$unique = true;
$ii = 0;
foreach($return_array as $row2){
if(array_intersect_key($row2,$temp) == $temp){//$row2 == $temp){
$unique = false;
break;
}
$ii++;
}
if($unique){
$return_array[$i] = $temp;
if(!empty($temp2)) $return_array[$i] = array_merge($temp,$temp2);
$i++;
}else{
if(!empty($temp2)){
foreach($temp2 as $key => $value){
if($process_array[$key] == 'sum') $temp2[$key] = $return_array[$ii][$key] + $value;
if($process_array[$key] == 'count') $temp2[$key] = $return_array[$ii][$key] + 1;
if($process_array[$key] == 'max') $temp2[$key] = ($return_array[$ii][$key] < $value) ? $value : $return_array[$ii][$key];
if($process_array[$key] == 'min') $temp2[$key] = ($return_array[$ii][$key] > $value) ? $value : $return_array[$ii][$key];
//TODO:(JC) 'average' - need to create a count field if not present (or always despite and assume overhead of extra computations).
// - then just calculate the 'sum' and divide by the counter as a last step before returning the array.
}
$return_array[$ii] = array_merge($temp,$temp2);
}
}
}
print_r($return_array);
return $return_array;
}
Which gives the following result:
/*
CALL: summarize($data,array('location','manufacturer','model','model','volume','colourvolume'),array('list','list','list','count','sum','sum') );
[0] = location
[1] = manufacturer
[2] = model
[3] = model count
[4] = mono volume sum
[5] = colour volume sum
*/
Array
(
[0] => Array
(
[0] =>
[1] => HP
[2] => LaserJet 4000
[3] => 3
[4] => 3000
[5] => 0
)
...
[17] => Array
(
[0] => Room 114
[1] => CANON
[2] => iR3235
[3] => 1
[4] => 4012
[5] => 0
)
[18] => Array
(
[0] => Room 115
[1] => LEXMARK
[2] => T652
[3] => 1
[4] => 20
[5] => 0
)
)
alternatively, if I assume that's $field_array contains sequentially key fields from root to sub key, you can loop your $field_array within $data_array loop
function summarize($data_array, $fields_array, $process_array){
$return_array = array();
foreach ($data_array as $row){
$temp = array();
foreach($fields_array as $key=>$field){
$temp = $key==0?$row[$field]:$temp[$field];
}
if(!empty($temp)) $return_array[] = $temp;
}
return $return_array;
}
and this is my array, will summarize with these function
$array = array(
array("multi"=>array("dimensional"=>array("array"=>"foo1"))),
array("multi"=>array("dimensional"=>array("array"=>"foo2"))),
array("multi"=>array("dimensional"=>array("array"=>"foo3"))),
array("multi"=>array("dimensional"=>array("array"=>"foo4"))),
array("multi"=>array("dimensional"=>array("array"=>"foo5"))),
array("multi"=>array("dimensional"=>array("array"=>"foo6"))),
array("multi"=>array("dimensional"=>array("array"=>"foo7"))),
array("multi"=>array("dimensional"=>array("array"=>"foo8"))),
array("multi"=>array("dimensional"=>array("array"=>"foo9")))
);
print_r(summarize($array,array("multi","dimensional","array"),NULL));
Ouput
Array ( [0] => foo1 [1] => foo2 [2] => foo3 [3] => foo4 [4] => foo5 [5] => foo6 [6] => foo7 [7] => foo8 [8] => foo9 )
Problem
I have an array which is returned from PHPExcel via the following
<?php
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excelFile = "excel/1240.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($excelFile);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$arrayData[$worksheet->getTitle()] = $worksheet->toArray();
}
print_r($arrayData);
?>
This returns:
Array
(
[Films] => Array
(
[0] => Array
(
[0] => Name
[1] => Rating
)
[1] => Array
(
[0] => Shawshank Redemption
[1] => 39
)
[2] => Array
(
[0] => A Clockwork Orange
[1] => 39
)
)
[Games] => Array
(
[0] => Array
(
[0] => Name
[1] => Rating
)
[1] => Array
(
[0] => F.E.A.R
[1] => 4
)
[2] => Array
(
[0] => World of Warcraft
[1] => 6
)
)
)
What I would like to have is
Array
(
[Films] => Array
(
[0] => Array
(
[Name] => Shawshank Redemption
[Rating] => 39
)
[1] => Array
(
[Name] => A Clockwork Orange
[Rating] => 39
)
)
[Games] => Array
(
[0] => Array
(
[Name] => F.E.A.R
[Rating] => 4
)
[1] => Array
(
[Name] => World of Warcraft
[Rating] => 6
)
)
)
The arrays names (Films, Games) are taken from the sheet name so the amount can be variable. The first sub-array will always contain the key names e.g. Films[0] and Games[0] and the amount of these can be varible. I (think I) know I will need to do something like below but I'm at a loss.
foreach ($arrayData as $value) {
foreach ($value as $rowKey => $rowValue) {
for ($i=0; $i <count($value) ; $i++) {
# code to add NAME[n] as keys
}
}
}
I have searched extensively here and else where if it is a duplicate I will remove it.
Thanks for any input
Try
$result= array();
foreach($arr as $key=>$value){
$keys = array_slice($value,0,1);
$values = array_slice($value,1);
foreach($values as $val){
$result[$key][] = array_combine($keys[0],$val);
}
}
See demo here
You may use nested array_map calls. Somehow like this:
$result = array_map(
function ($subarr) {
$names = array_shift($subarr);
return array_map(
function ($el) use ($names) {
return array_combine($names, $el);
},
$subarr
);
},
$array
);
Demo
Something like this should work:
$newArray = array();
foreach ($arrayData as $section => $list) {
$newArray[$section] = array();
$count = count($list);
for ($x = 1; $x < $count; $x++) {
$newArray[$section][] = array_combine($list[0], $list[$x]);
}
}
unset($arrayData, $section, $x);
Demo: http://ideone.com/ZmnFMM
Probably a little late answer, but it looks more like your tried solution
//Films,Games // Row Data
foreach ($arrayData as $type => $value)
{
$key1 = $value[0][0]; // Get the Name Key
$key2 = $value[0][1]; // Get the Rating Key
$count = count($value) - 1;
for ($i = 0; $i < $count; $i++)
{
/* Get the values from the i+1 row and put it in the ith row, with a set key */
$arrayData[$type][$i] = array(
$key1 => $value[$i + 1][0],
$key2 => $value[$i + 1][1],
);
}
unset($arrayData[$type][$count]); // Unset the last row since this will be repeated data
}
I think this will do:
foreach($arrayData as $key => $array){
for($i=0; $i<count($array[0]); $i++){
$indexes[$i]=$array[0][$i];
}
for($i=1; $i<count($array); $i++){
for($j=0; $j<count($array[$i]); $j++){
$temp_array[$indexes[$j]]=$array[$i][$j];
}
$new_array[$key][]=$temp_array;
}
}
print_r($new_array);
EDIT: tested and updated the code, works...