I am using simplexlsx.class.php class to extract data from an Excel file which contains only two columns,
like
column-1 column-2
2017-06-06 25.98
2017-06-07 25.83
2017-06-08 10.265
but when I use the method $xlsx->rows() it is giving me result like this;
Array (
[0] => Array ( [0] => 2017-06-07 [1] => 9.75 )
[1] => Array ( [0] => 2017-06-07 [1] => 15 )
[2] => Array ( [0] => 2017-06-07 [1] => 18 )
[3] => Array ( [0] => 2017-06-07 [1] => 14.75 )
)
I want these two values(date and number) to be saved in two different variables, which further I will use to save them in database.
Kindly help me getting these two values in two separate variables.
Thanks!
You understand the array structure you are looking at?
Could break out with a loop like shown below. Honestly the structure makes sense probably for what you need to do though as it is.
$rows = $xlsx->rows();
$colA = []; $colB = [];
foreach($rows as $i => $row) {
$colA[$i] = isset($row[0) ? $row[0] : null;
$colB[$i] = isset($row[1) ? $row[1] : null;
//or in this loop access...
echo "ColA: {$row[0]}, ColB: {$row[1]}";
}
See the array_column answer for cleaner method.
I think use array_column in php doc explained how can solve your problem
For example:
$first_col = array_column($records, 0);
$second_col = array_column($records, 1);
Check out the run time result here
Related
PHP how to get array value:
<?php
print_r($_POST['cb']);
Array
(
[0] => Array
(
[id] => 1
[tipe] => read
)
[1] => Array
(
[id] => 1
[tipe] => update
)
[2] => Array
(
[id] => 2
[tipe] => update
)
)
?>
So what I want to get maybe in foreach function like this:
foreach(...)
{
$qInsert = oci_parse("INSERT INTO TDATA(ID, CHKSTATUS) VALUES('$getID', '$getTipe')");
oci_execute($qInsert);
}
$getID means the [id], and $getTipe means the [tipe] in array.
Is it possible to do like that?
If your Array has 100s of indices, your query will execute 100 times to insert data. Avoid such operations as much as possible, See the answer below, you can insert data in a single query, the code is not tested, you can adjust to meet your requirements .
$dataArray = $_POST['cb'];
$queryData = null;
foreach($dataArray as $data){
$queryData[]="(".$data['id'].", '".$data['tipe']."')";
}
$values = implode(',',$queryData);
$qInsert = oci_parse("INSERT INTO TDATA(ID, CHKSTATUS) VALUES ".$values."");
oci_execute($qInsert);
I hope this might save your database operations overhead
The output I have is this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
)
My current code to try and update is:
updateProduct($product->id, array('categories[108]'));
When I run this function, it overwrites the existing array, giving me an output like this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 108
)
What I am trying to achieve would look like this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
[1] => 108
)
But.... in the event that 108 already exists, I would hope that it would just ignore it. I don't want:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
[1] => 108
[2] => 108
)
Any idea how I do that?
You can push elements to the array using
$arr[] = $new_var;
and check if a value already exists by
if(in_array($new_var, $arr))
In example (trying to follow your input-output):
$categories = array();
$categories[] = 26;
$input_value = 108;
if(!in_array($input_value, $categories)) {
$categories[] = $input_value;
}
edit. in functions (my PHP is bit rusty, so there might be small tweaks that need to be done but you get the basic idea)
function updateProduct($arr, $input) {
if(!in_array($input, $arr)) {
$arr[] = $input;
}
}
and then call it as
updateProduct($categories, $input_value);
You can use array_push to add items to an array, and you can use in_array to check if it already exists, or you can use array_unique to remove duplicates after your done the rest of your processing too.
My input array :
Array
(
[0] => Array
(
[id] => 1
[status_name] => Released
)
[1] => Array
(
[id] => 2
[status_name] => Under Construction
)
)
I want the output result :
Array (
[1] => Released
[2] => Under Construction
)
USe sub array id as output array key value and status_name as value array.
This is built into php as array_column. You would have:
$status_names = array_column($data, 'status_name', 'id');
print_r($status_name);
Bonus points on question as I had no idea this existed until looking for an answer for you.
Try the following:
function reOrderArray($input_array)
{
$result = array();
foreach ($input_array as $sub_array)
{
$result[$sub_array['id']] = $sub_array['status_name'];
}
return $result;
}
There might be a built-in php function to do this, array functions in php are quite powerful. I am, however, woefully unaware of one.
I have this code:
$id = new matrix(array(0=>array(1,0.5,3), 1=>array(2,1,4), 2=>array(1/3,1/4,1)));
$soma = $id->times($id)->sumRows();
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 12.75 [1] => 22.3333333333 [2] => 4.83333333333 ) ) [numColumns] => 3 [numRows] => 1 )
and:
$total = $id->times($id)->sumRows()->sumTotal($id);
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 39.9166666667 ) ) [numColumns] => 3 [numRows] => 1 )
Now, i am trying to make:
foreach ($soma as $value){
$final = (int)$value/(int)$total;
print_r ((int)$final);
}
The output will be 000.
Must be:
12.75/39.9166666667 = 0,3269230769230769
22.3333333333 / 39.9166666667 = ...
and so on
Thanks!
Just some ideas, without really knowing much about the matrix class...
All those (int)s should probably be (float)s, as you seem to want a non-int answer.
$value is itself an object, so you'll probably need to use $value['numbers'][0][0 or 1 or 2]. Same goes for $total.
the issue is solved :
documentation:
get_data($..)
I have an array which looks like this:
Array
(
[0] => Array
(
[1] => Array
(
[name] => vrij
// ...
)
[2] => Array
(
[name] => zat
// ...
)
)
)
I build this array using a for loop; however, I need to push 4 more 'records' to the array, which I can't do in this for loop.
I want the array to look like this, after the pushes:
Array
(
[0] => Array
(
[1] => Array
(
[name] => vrij
// ...
)
[2] => Array
(
[name] => zat
// ...
)
// ...
)
[1] => Array
(
[1] => Array
(
[name] => zon
//...
)
[2] // etc
)
)
The four new records should be pushed to array[1], so I get something like
$array[1][0], $array[1][1], etc. 0 1 2 3 contains the new data.
I tried quite a lot of stuff, to be honest. I need to do four of these pushes, so I was trying a for loop:
for($i = 0; $i < 4; $i++)
{
$day_info = $this->get_day_info($i, $data['init']['next_month'], $data['init']['current_year']);
$push['name'] = $day_info['day_name'];
array_push($data['dates'], $push);
}
and all other kinds of things with [], [1][$i], etc. Sometimes it even adds five arrays! I'm confused as to why it won't just add the [1][1], [1][2],.. I'm probably missing out on something here. Thanks a lot.
If this isn't clear, please do tell and I'll add more code to explain the problem better.
$extradates = array(1 => 'zon', 2 => 'maa');
$data['dates'][] = $extradates;
Will add 2 extra dates to the array using a new index.
Although if I see what you trying to accomplish, I think there might be a better way.
This above works though :)