I want to sum each row of a multidimensional array :
$number = array
(
array(0.3,0.67, 0.3),
array(0.3,0.5,1),
array(0.67,0.67,0.3),
array(1,0.3,0.5)
);
The result what i want is like this :
row1 = 1.27
row2 = 1.8
row3 = 1.64
row4 = 1.8
I already tried this code :
for($i = 0; $i < 4; $i++) {
for($j = 0; $j < 5; $j++) {
$sumresult[] = array_sum($number[$i][$j]);
}
}
But it appear an error like this :
Warning: array_sum() expects parameter 1 to be array, double given in xxxx
array_sum needs array not values. Do like this:
for($i = 0; $i < 4; $i++) {
$sumresult[] = array_sum($number[$i]);
}
its because you are passing an value instead of the array containing it.
One correct solution would be:
$sumResult = array();
foreach($number as $values){
$sumResult []= array_sum($values);
}
print_r($sumResult);
Should do the trick ;)
It's easier to just map the array_sum() function to the array to sum the inner arrays:
$sumresult = array_map('array_sum', $number);
Related
I want to initializing a 2 dimensions Array in PHP and I don't know How to declare a 2d array of size (1,N) in php with all values as Zeros?
$Orders_C = array(1,N);
I am not sure if this is correct syntax or not.
PHP doesnot have 2d array ,but instead it has array of array. You can use the code below to initialize as you said.
$Orders_C=array_fill(0,1, array_fill(0, N,0));
Here the array_fill first return an array filled with 0 from index 0 to N.And again the same array is filled to the new array upto index 1.Hence you will get an array within array.
Generic solution for any count of columns and rows could be:
$maxRows = 5;
$maxCols = 5;
$orders = [];
for ($col = 0; $col < $maxCols; $col++) {
for ($row = 0; $row < $maxRows; $row++) {
$orders[$col] = $row;
}
}
And because you want 2d array like (1, N) then you can simplify it
$orders = [];
for ($i = 0; $i < $maxRows; $i++) {
$orders[0][] = 0;
}
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 6 months ago.
Good Day!
Any suggestions on how can I merged and print 3 arrays in PHP.
I retrieved data from database and put it in ARRAY.
1st Array: $date[] = $row['date'];
2nd Array: $requestor[] = $row['requestor'];
3rd Array: $die[] = $row['die'];
then I use foreach to print the retrieved data stored from each Array that meets the condition.
foreach($date as $item_date){
echo $item_date;
}
foreach($requestor as $item_requestor){
echo $item_date;
}
foreach($die as $item_die){
echo $item_requestor;
}
But the result of this code is like this:
date1
date2
date3
requestor1
requestor2
requestor3
die1
die2
die3
My goal is this one:
date1 - requestor1 - die1
date2 - requestor2 - die2
date3 - requestor3 - die3
Any Idea oon how can I achieved this output.
TIA
You have to do it manually count on loop like this
$count = count($date)-1;
then loop through this
for ( $i=0;$i <= $count; $i++ ) {
$arrayGenerate[$i] = array(
'row1' => $data[$i].'-'.$requestor[$i].'-'.$die[$i]
);
}
like this
Try with -
for($i = 0; $i <= count($date); $i ++) {
echo $date[$i]." - ".$requestor[$i]." - ".$die[$i];
}
Assuming the all 3 arrays count/size is same & you want it in this fashion
<?php
$a = array('1','2','3');
$b = array('4','5','6');
$c = array('7','8','9');
for($i=0;$i<count($a);$i++)
{
echo $merged_arr_str = $a[$i] . " - " . $b[$i] . " - ". $c[$i] . " <br/>";
}
?>
You can try this
Example One:-
$date = array('date1','date2','date3','date4');
$requestor = array('requestor1','requestor2','requestor3','requestor4');
$die = array('die1','die2','die3','die4');
$count = max(count($date), count($requestor), count($die));
$newarray = array();
for($i=0; $i < $count; $i++) {
//Demo1
if (isset($date[$i])) $newarray[] = $date[$i];
if (isset($requestor[$i])) $newarray[] = $requestor[$i];
if (isset($die[$i])) $newarray[] = $die[$i];
//Demo2
//echo $ouput = $date[$i].'-'.$requestor[$i].'-'.$die[$i];
}
//array merge output
var_dump($newarray);
Example Two
$date = array('date1','date2','date3','date4');
$requestor = array('requestor1','requestor2','requestor3','requestor4');
$die = array('die1','die2','die3','die4');
$arrays = array($date, $requestor, $die);
array_unshift($arrays, null);
$n = call_user_func_array('array_merge', call_user_func_array('array_map', $arrays));
print_r($n);
for($i = 0; $i < count($date); $i ++) {
echo $date[$i]." - ".$requestor[$i]." - ".$die[$i];
}
this loop will iterat untill last element of date array.if you want to put equal to sign then you have to initialize $i with 1.
exm. if $i = 0 then $i < count($date)
OR
if $i = 1 then $i <= count($date)
My goal is to make ant assoc array from the values of for loop.
//$from_time value is 6 and $to_time value is 23
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = $i;
}
echo json_encode($working_time_array);
The output I get on AJAX success, and when I console.log it, I get result as such :
["6",7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Preferred result is
["6","7","8","9","10"]... etc
The only difference between the two results is one result set contains integers and the other contains strings. If you want those values to be strings just cast them when assigning them to the array:
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string) $i;
}
This really shouldn't be necessary unless your client side is expecting strings only.
You would need to cast $i to a string before pushing it to the array.
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string)$i;
}
why would you convert int to string?
for your goal this should work
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = "$i";
}
echo json_encode($working_time_array);
I have some arrays extracted from a XML-file. These are in
$array0 = (.......)
$array1 = (.......)
...
$arrayN = (.......)
Now I need a single array with all arrays in it separated by "," as
$masterArray = array($array0,
$array1,
...
$arrayN)
I tried
for ( $i = 0; $i < N; $i++) {
$masterArray = $masterArray + $array[$i];
}
with no result. I tried array_merge but this will give one
$masterArray(......................all items of all $arrays.....)
How can I do it right?
for ( $i = 0; $i < N; $i++) {
$temp = "array".$i;
$masterArray[$i] = ${$temp};
}
Given your exact definition of what you got and what you want the routine should be:
for ( $i = 0; $i < N; $i++) $masterArray[] = ${'array'.$i};
Not much to explain here. You make a new entry in an array with $variable[] = <entry>; and you access your numbered array with a variable variable, see:
http://php.net/manual/en/language.variables.variable.php
I guess you wont to create a multi dimension array:
$masterArray = array();
$masterArray[] = $array0;
$masterArray[] = $array1;
...
$masterArray[] = $arrayN;
this will in all arrays as element of the MasterArray:
$masterArray[0=>(....),1=>(....), ...];
Then access it like this:
$arr1_key1 = $masterArray[1]['key1'] ; //$array1['key1'];
You can add specific keys if you wont:
$masterArray['arr1'] = $array1;
$arr1_key1 = $masterArray['arr1']['key1'] ; //$array1['key1']
In general read some more to get better understanding on how to work with arrays ;)
I'm trying to put the column names of a msSQL table into an array. Using
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
echo mssql_field_name($result) . "<br><br>";
}
the column names print to the screen just fine. Also get_type() shows that they are strings. However, when I try to put them into an array like this:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$current_column = mssql_field_name($result);
array_push($column_names, $current_column);
}
var_dump($column_names); gives me an array (albeit the expected length) of boolean values. All false. I would expect to see an array containing the names of all my columns. What am I doing wrong here? Thank you
Looks like you are missing the $i argument on the mssql_field_name call. Try this maybe:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$column_names[] = mssql_field_name($result, $i);
}
http://php.net/manual/en/function.mssql-field-name.php