I have a series of variables that are collected in a while next loop.
I wanted to store these in an array so that I can access them later in some javascript, so I set
$remainingTime = array();
before while (!$listing->EOF) {
After the variables are populated I then added
$remainingTime[] = array( $remainingDay, $remainingHour, $remainingMinutes, $remainingSeconds );
If I do
echo "<pre>";
print_r($remainingTime);
echo "</pre>";
after the loop is finished I get the following output
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
[2] => Array
(
[0] => 8
[1] => 16
[2] => 4
[3] => 33
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
[2] => Array
(
[0] => 8
[1] => 16
[2] => 4
[3] => 33
)
[3] => Array
(
[0] => 2
[1] => 5
[2] => 38
[3] => 17
)
)
So it appears that it is building a new array each time it goes through the loop rather than just adding the next dataset to it.
Where have I gone wrong with building the array?
The assignment syntax you used ($remainingTime[]) pushes the value you assign it as a new element into the array. Try it without using the square brackets.
Related
I new to PHP, I need to convert single dimension array into multi dimension array in php
I have data like this, need to minimize as below.
Array
(
[0] => Array
(
[0] => David
[1] => School
[2] => 19
[3] => 29
)
[1] => Array
(
[0] => Paul
[1] => Home
[2] => 19
[3] => 29
)
[2] => Array
(
[0] => Paul
[1] => Cinema
[2] => 19
[3] => 29
)
[3] => Array
(
[0] => Paul
[1] => Park
[2] => 19
[3] => 29
)
[4] => Array
(
[0] => Rossie
[1] => Playground
[2] => 19
[3] => 29
)
[5] => Array
(
[0] => Rossie
[1] => Hotel
[2] => 19
[3] => 29
)
[6] => Array
(
[0] => Rossie
[1] => Hospital
[2] => 19
[3] => 29
)
)
And I want convert it to multidimensional
Array
(
[0] => Array
(
[0] => Array
(
[0] => David
(
[0] => School
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Paul
(
[0] => Home
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Cinema
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Park
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[2] => Array
(
[0] => Array
(
[0] => Rossie
(
[0] => Playground
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hotel
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hospital
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
)
I hope you get an idea. But my function doesn't do this correctly or maybe there are other ways to do this easier ?
I would be grateful for any help.
Thanks
Below is one way to do it:
<?php
$arr = [array('David','School',19,29),
array('Paul','Home',19,29),
array('Paul','Cinema',19,29),
array('Paul','Park',19,29),
array('Rossie','Playground',19,29),
array('Rossie','Hotel',19,29),
array('Rossie','Hospital',19,29)];
// Get all names.
$names = array_unique(array_map(function($value){return $value[0];}, $arr));
$places = [];
// Create the multidimensional array, grouping by name.
foreach($names as $key => $name){
$tempArr = [];
foreach($arr as $record){
if($record[0] === $name){
$tempArr[][$record[1]] = array_slice($record,2);
}
}
$places[][][$name] = $tempArr;
}
print("<pre>".print_r($places,true)."</pre>");
This will return the following result:
Array
(
[0] => Array
(
[0] => Array
(
[David] => Array
(
[0] => Array
(
[School] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[1] => Array
(
[0] => Array
(
[Paul] => Array
(
[0] => Array
(
[Home] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Cinema] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Park] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[2] => Array
(
[0] => Array
(
[Rossie] => Array
(
[0] => Array
(
[Playground] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Hotel] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Hospital] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
)
Below is the array in php and I need function to solve n numbers of array as displayed in Expected result by summing the (2) and (3) values if (0) and (1) are matching :
Array
(
[0] => Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 1
[3] => 2
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 3
[3] => 4
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 5
[3] => 6
)
)
[1] => Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 7
[3] => 8
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 9
[3] => 10
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 11
[3] => 12
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
)
Expected Result :
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
Need help to get the above expected result for the raised query. I tried below code :
<?php
$sum = [];
foreach($weekly as $aSet) {
foreach($aSet as $k => $kv) {
$i = 0;
foreach($kv as $v) {
$sum[$k][$i] += $v;
$i++;
}
}
}
?>
But it will add all types :
[0] => Array
(
[0] => 0
[1] => 4038
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => 0
[1] => 4038
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => 0
[1] => 4038
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => 0
[1] => 4038
[2] => 13
[3] => 14
)
I want get sum only for 2 and 3rd value and need to keep constant for 0 and 1st value for exact match
Because all your nested arrays are with predefined amount of elements, all of those with the same indexes, you can use a for loop instead of the third foreach:
$sum = [];
foreach($weekly as $aSet)
{
foreach($aSet as $k => $kv)
{
$sum[$kv[0]][0] = $kv[0];
$sum[$kv[0]][1] = $kv[1];
for($i = 2; $i <= 3; $i++)
{
$sum[$kv[0]][$i] += $kv[$i];
}
}
}
echo "<pre>";
print_r($sum);
echo "</pre>";
With a for loop you can cycle inside the array considering only the needed elements of that array (in your case the indexes 0 and 1 aren't useful in this loop).
Output:
Array
(
[Week - 1] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[Week - 2] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[Week - 3] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[Week - 4] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
If you prefer the array with numbered indexes do that:
echo "<pre>";
print_r(array_values($sum));
echo "</pre>";
Output:
Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
I have an multidimensional array which contain some numbers, and i want to multiplying value of array which contain key 0 with each other key inside one area array and erase key 0.
Array
Array
(
[0] => Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 6
[4] => 7
)
[1] => Array
(
[0] => 2
[1] => 7
[2] => 4
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 3
[3] => 2
[4] => 5
)
)
Here's the result i want
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
I was already combine it using foreach and for, but it still not working for me, any idea how to do this?
The solution using array_map and array_slice functions:
// $arr is your initial array
foreach ($arr as &$v) {
$multiplier = $v[0];
$v = array_map(function($val) use($multiplier){
return $multiplier * $val;
}, array_slice($v, 1));
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
Below are my arrays echoed inside a for-each loop.
Array
(
[0] => 1
[1] => 2
[2] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 1
)
Array
(
[0] => 2
[1] => 1
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 3
)
Array
(
[0] => 3
[1] => 3
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 1
)
I would like to group these arrays based on the value of the first item (index = 0) and get the following dimension array.
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
)
Array
(
[3] => Array
(
[0] => 2
[1] => 1
[2] => 1
)
)
Array
(
[4] => Array
(
[0] => 3
[1] => 3
[2] => 1
)
[5] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[6] => Array
(
[0] => 3
[1] => 3
[2] => 2
)
)
Array
(
[7] => Array
(
[0] => 4
[1] => 2
[2] => 2
)
[8] => Array
(
[0] => 4
[1] => 2
[2] => 1
)
)
I have spent hours to figure this out but due to less experience I still cannot get this done. Please help me with some algoritme.
Won't that do the trick?
$result = [];
foreach ($originalArrays as $array) {
$result[$array[0]][] = $array;
}
So long story short, I have the array $total which looks like this:
Array (
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 8
)
[2] => Array
(
[0] => 9
[1] => 10
)
)
However I need to pass it to a function that looks like this:
$count = cartesian(
Array("GH20"),
Array(1,3),
Array(6,7,8),
Array(9,10)
);
I have asked a similar question but I suspect the answer they gave was correct, I do not think asked the question in the right way. The cartesain function does not seem to accept:
$count = cartesian(
Array($total[0]),
Array($total[1]),
Array($total[2])
);
After some digging around I believe this is down to the fact that the arrays have keys which is not causing an error however instead of any values being returned, it just returns "array" for everything.
My question, in two parts then, is how do I remove the keys from the arrays so it is formatted as required. If possible, could the be done dynamically?
I have tried what everybody suggestes on google which is use the array_values() function, but this did not help
Any help is greatly appreciated, thanks, Nick
As requested, casertain function:
function cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
This is my desired output (Every permutation of ($total):
Array (
[0] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 9
)
[1] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 10
)
[2] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 9
)
[3] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 10
)
[4] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 9
)
[5] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 10
)
[6] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 9
)
[7] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 10
)
[8] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 9
)
[9] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 10
)
[10] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 9
)
[11] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 10
)
)