Good day, i am trying to generate a csv with array below
array[0] => array(
["a"] => 1,
["b"] => 1,
["c"] => 1, --> the value is only 1 and 2
["d"] => user2,
),
array[1] => array(
["a"] => 1,
["b"] => 1,
["c"] => 2,
["d"] => user3,
),
array[2] => array(
["a"] => 2,
["b"] => 2,
["c"] => 1,
["d"] => user4,
),
array[3] => array(
["a"] => 2,
["b"] => 2,
["c"] => 2,
["d"] => user5,
)
edit: i want to get the 2 array with same number on "a" and "b" value, but different in "c" value
i tried my code below
$results = array();
for($i = 1 ; $i <= 10 ; $i++){
$j = $i+1;
$response = array(
array(
$i, $i, 1, "user".$i
),
array(
$i, $i, 2, "user".$j
)
);
array_push($results, $response);
}
but the array result is not as i wanted like above result. is there any solutions?, sorry for the confusion of this question, i am trying my best to ask. thank you for the help!.
1.You need to use % (modules operator) in your code to set 1 or 2 at third index of each of your child-array.
2.Don't create too many array/variables in your code.Do direct assignments
$results = array();
$j = 1;
for($i=1; $i<=10; $i++){
$j++;
$results[] = array($i, $i, 1, "user".$j);
$j++;
$results[] = array($i, $i, 2, "user".$j);
}
print_r($results);
Output: https://3v4l.org/GE7MD
I think you will have to work with a for loop and a manually increments value for your userx value like this.
$results = [];
$user = 1;
for($i = 1 ; $i <= 10 ; $i++){
$x = 1;
$user++;
$results[] = [$i, $i, $x, "user$user"];
$x = 2;
$user++;
$results[] = [$i, $i, $x, "user$user"];
}
print_r($results);
RESULT:
Array
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => user2
)
[1] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => user3
)
[2] => Array
(
[0] => 2
[1] => 2
[2] => 1
[3] => user4
)
[3] => Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => user5
)
[4] => Array
(
[0] => 3
[1] => 3
[2] => 1
[3] => user6
)
[5] => Array
(
[0] => 3
[1] => 3
[2] => 2
[3] => user7
)
[6] => Array
(
[0] => 4
[1] => 4
[2] => 1
[3] => user8
)
[7] => Array
(
[0] => 4
[1] => 4
[2] => 2
[3] => user9
)
[8] => Array
(
[0] => 5
[1] => 5
[2] => 1
[3] => user10
)
[9] => Array
(
[0] => 5
[1] => 5
[2] => 2
[3] => user11
)
[10] => Array
(
[0] => 6
[1] => 6
[2] => 1
[3] => user12
)
[11] => Array
(
[0] => 6
[1] => 6
[2] => 2
[3] => user13
)
[12] => Array
(
[0] => 7
[1] => 7
[2] => 1
[3] => user14
)
[13] => Array
(
[0] => 7
[1] => 7
[2] => 2
[3] => user15
)
[14] => Array
(
[0] => 8
[1] => 8
[2] => 1
[3] => user16
)
[15] => Array
(
[0] => 8
[1] => 8
[2] => 2
[3] => user17
)
[16] => Array
(
[0] => 9
[1] => 9
[2] => 1
[3] => user18
)
[17] => Array
(
[0] => 9
[1] => 9
[2] => 2
[3] => user19
)
[18] => Array
(
[0] => 10
[1] => 10
[2] => 1
[3] => user20
)
[19] => Array
(
[0] => 10
[1] => 10
[2] => 2
[3] => user21
)
)
I removed the array_push() as it is quicker (no function call) to do a simple $arr[] = $something;
Related
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'm so confused with arrays. Can any one help me to solve this??
I have 4 arrays, these all are related.
My array structure is like:
Array 1:
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
Array 2:
Array ( [0] => 500 [1] => 500 [2] => 1 [3] => 2 [4] => 3 [5] => 3 )
Array 3:
Array ( [0] => 2 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
Array 4:
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
I have to map all 1 value from Array 1 to another arrays.
If you want to map the values of 4 arrays to each position, you can:
$arr1 = array(1, 2, 1, 1, 2, 1 );
$arr2 = array(500, 500, 1, 2, 3, 3 );
$arr3 = array(2, 2, 1, 1, 2, 1 );
$arr4 = array(1, 2, 1, 1, 2, 1 );
$results = array_map(function($v1, $v2, $v3, $v4) {
return array($v1, $v2, $v3, $v4);
}, $arr1, $arr2, $arr3, $arr4);
echo "<pre>";
print_r( $results );
echo "</pre>";
This will result to:
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 2
[3] => 1
)
[1] => Array
(
[0] => 2
[1] => 500
[2] => 2
[3] => 2
)
[2] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 1
)
[4] => Array
(
[0] => 2
[1] => 3
[2] => 2
[3] => 2
)
[5] => Array
(
[0] => 1
[1] => 3
[2] => 1
[3] => 1
)
)
Doc: http://php.net/manual/en/function.array-map.php
I'am bulding an app with laravel, and I have a problem, I have two array in php :
Array1
(
[0] => 15
[1] => 15
[2] => 16
[3] => 16
[4] => 17
[5] => 17
[6] => 17
[7] => 17
)
Array2
(
[0] => 0
[1] => 1
[2] => 1
[3] => 2
[4] => 0
[5] => 1
[6] => 2
[7] => 3
)
in this case i can not to use array_chunk because the value of array2 is dinamic and key of array1 must not be same if i combine it, so, how i can combine it to be like this :
Array
(
[15] => Array
(
[0] => 0
[1] => 1
)
[16] => Array
(
[0] => 1
[1] => 2
)
[17] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
Simple foreach loop:
$arr1 = [15,15,16,16,17,17,17,17];
$arr2 = [0,1,1,2,0,1,2,3];
$result = [];
foreach($arr1 as $k => $v){
$result[$v][] = $arr2[$k];
}
print_r($result);
The output:
Array
(
[15] => Array
(
[0] => 0
[1] => 1
)
[16] => Array
(
[0] => 1
[1] => 2
)
[17] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
$array1 =
[
0 => 15,
1 => 15,
2 => 16,
3 => 16,
4 => 17,
5 => 17,
6 => 17,
7 => 17,
];
$array2 =
[
0 => 0,
1 => 1,
2 => 1,
3 => 2,
4 => 0,
5 => 1,
6 => 2,
7 => 3,
];
$arr = array_unique($array1);
print_r($arr);
$newarray = [];
foreach($arr as $ar){
foreach($array1 as $key => $value){
if($ar == $value){
$newarray[$value][] =$array2[$key];
}
}
}
print_r($newarray);
I am working with multi dimensional array is like below with php,
$return_array= Array
(
[0] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 19
[3] => 7
[4] => 13
[5] => 3
[6] => 0
[7] => 42
)
[1] => Array
(
[0] => Yet to closed
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 0
[7] => 1
)
[2] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 7
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 7
)
[3] => Array
(
[0] => 4_Apr_2017
[1] => 0
[2] => 8
[3] => 4
[4] => 0
[5] => 0
[6] => 0
[7] => 12
)
)
On 0th and 2nd indexs -> from sub array of that indexes -> 0th index are common "3_Mar_2017" ,I want to sum that two indexes and want result as shown below,
$final_return = Array
(
[0] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 26
[3] => 7
[4] => 13
[5] => 3
[6] => 0
[7] => 49
)
[1] => Array
(
[0] => Yet to closed
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 0
[7] => 1
)
[2] => Array
(
[0] => 4_Apr_2017
[1] => 0
[2] => 8
[3] => 4
[4] => 0
[5] => 0
[6] => 0
[7] => 12
)
)
My tried code with loop as below,
$tem_array = array();
$final_return = array();
foreach ($return_array as $unique) {
if (!in_array($unique[0], $tem_array)) {
array_push($tem_array, $unique[0]);
$final_return[] = $unique;
} else {
$index = array_search($unique[0], $tem_array);
for ($i = 1; $i < count($unique); $i++) {
$final_return[$index][$i] = $final_return[$index][$i] + $unique[$i];
}
}
}
but if array size will large then ,may be it will take time is there any simple solution.
can any person help me to get this required result with minimum code ?
I will appreciate best answer.
hope this is what you are looking for
$temp1 = array(); $result = array();
foreach ($myArray as $temp) {
if (!in_array($temp[0], $temp1)) {
array_push($temp1, $temp[0]); $result[] = $temp;
} else {
$id = array_search($temp[0], $temp1); for ($i = 1; $i <= count($temp); $i++) {
$result[$id][$i] = $result[$id][$i] + $temp[$i];
}
}
}
your first array would look like
Array
(
[0] => Array
(
[0] => 13
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => 14
[1] => 5
[2] => 6
[3] => 7
)
[2] => Array
(
[0] => 13
[1] => 1
[2] => 2
[3] => 3
)
)
and the result be like
Array
(
[0] => Array
(
[0] => 13
[1] => 2
[2] => 4
[3] => 6
)
[1] => Array
(
[0] => 14
[1] => 5
[2] => 6
[3] => 7
)
)
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
)
)