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
)
)
Related
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.
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
)
)
Hello sir below is my multidimensional array that contain some missing values on specific index
$array1 = Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] =>
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] =>
)
)
and below is my second array
I want to put the array2 value of index 3 and 4 in to the $array1 index 3 and 4 .but i dnt want to replace whole array value.
I just want to replace the those value that are null in the $array1
$array2 = Array
(
[3] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[5] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
)
Required output below where i show the replace value in single qoutes )
$array1 = Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] => '9'
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] => '7'
)
This is it:
$array1 = array("2"=> array("1"=>"2", "2"=>"4", "3"=>"9"),
"3"=> array("1"=>"4", "2"=>"6", "3"=>""),
"4"=> array("1"=>"4", "2"=>"6", "3"=>"7"),
"5"=> array("1"=>"2", "2"=>"4", "3"=>"")
);
$array2 = array("3"=> array("1"=>"2", "2"=>"4", "3"=>"9"),
"5"=> array("1"=>"4", "2"=>"6", "3"=>"7")
);
foreach ($array1 as $key => $value) {
foreach ($value as $key2 => $value2) {
if($value2 == ""){
$array1[$key][$key2] = $array2[$key][$key2];
}
}
}
echo '<pre>';
print_r($array1);
echo '</pre>';
Output:
Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] => 9
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] => 7
)
)
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;
}
What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.