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;
}
Related
I have an array in asp. Example SampleArray(100,100). This in PHP would be SampleArray[100][100] if I am correct. I am trying to populate this array and this is what I have so far:
$sampleArray = array(array());
$counter1 = 0;
$counter2 = 0;
for($counter1; $counter1 < 100; $counter1++)
{
for($counter2; $counter2 < 100; $counter2++)
{
$sampleArray[$counter1][$counter2] = $counter1 . " , " . $counter2;
}
$counter2 = 0;
}
echo("Sample Array size: " . count($sampleArray));
You are not using your for loops in an efficient way. The first element of the for loop is for initializing the counter.
$sampleArray = array();
for($counter1=0; $counter1<100; ++$counter1) {
$sampleArray[$counter1] = array();
for($counter2=0; $counter2<100; ++$counter2)
$sampleArray[$counter1][$counter2] = $counter1.','.$counter2;
}
Now, you will have a 100x100 array.
The MAIN difference is that I declared each element of $sampleArray to be an array instead of using array(array()) which makes one element in the top level that contains an array instead of 100 elements that each contain an array.
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);
I know how to merge arrays manually, but I need to merge arrays in a loop, while not knowing the names of the arrays or how many times they will loop.
I can do this manually:
$masterarray = array_merge_recursive($searchcustomers1, $searchcustomers2);
but how do I do it in a loop. This is what I have:
$pages is how many times it needs to loop
for ( $i = 1; $i <= $pages; $i += 1) {
$searchcustomers[$i] = $sc->call.....//an API call
}
How would I merge or append all the $searchcustomers[$i] to each other into a master array.
Maybe this can help you
$allCustomers = [];
for ($i = 1; $i <= $pages; $i += 1) {
$allCustomers = array_merge($allCustomers, $searchcustomers[$i]);
}
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 load a xml-file which contains several arrays.
$array0 = (.......)
$array1 = (.......)
...
$arrayN = (.......)
In my HTML-part I have a DIV to display each array. To do it write all arrays in one masterarray:
for ( $i = 0; $i < 1; $i++) $masterArray[] = ${'array'.$i};
and can access each array
$Titel = $masterArray[0] [$i] [$kat] [0] ["TitelD"];
This works if I have more than one array in my xml-File. If I have only one the masterarray rests empty. Why that? If I copy a second in the xml-File it works... (with displaying the second!)
What can I do to get only one array in the masterarray?
This XML works:
and when changing the url to this xml-file (in the same html/php-file) the DIV is empty:
change this
for ( $i = 0; $i < 1; $i++)
to
for ( $i = 0; $i <= 1; $i++)