I have a function like this:
foreach($data as $line) {
if(substr($line,0,1) == "A") {
if(!$first) {
$parts = explode(chr(9),$line);
//echo "<pre>"; print_r($parts); echo "</pre>";
When printed returns multiple arrays like this:
Array
(
[0] => A
[1] => 100_1
[2] => 0
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 100_2
[2] => 0
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 100_3
[2] => 0
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101_2
[2] => 0
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
Is it possible to 'only' select the keys ([1]) which have values _1,2,3, (if they exist)
and replace the key ([2]) with the same value, so the result would be:
Array
(
[0] => A
[1] => 100_1
[2] => 1
[3] => 1187
[4] => 0
)
Array
(
[0] => A
[1] => 100_2
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 100_3
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101_2
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
I tried to make lots of entries like this but they don't work (I cant get the key right):
foreach($parts as $key => $value) {
if ($value == '100_1') {$parts[$key] = '1';};
if ($value == '100_2') {$parts[$key] = '2';};
if ($value == '100_3') {$parts[$key] = '3';}}
But this would be pure madness as there are over 50 different values and arrays any help would be greatly received.
Something like:
foreach($data as $line) {
if(substr($line,0,1)=="A") {
if(!$first) {
$parts = explode(chr(9), $line);
list($num1, $num2) = explode('_', $parts[1]);
$parts[2] = isset($num2) ? $num2 : $parts[2];
}
}
}
Related
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
)
)
Well, I have this array $city :
[2] => Array
(
[0] => Array
(
[0] => fr
[1] => paris
[2] => a8
)
[1] => 0
[2] => 116729
)
[3] => Array
(
[0] => Array
(
[0] => fr
[1] => marseille
[2] => b8
)
[1] => 0
[2] => 12898
)
[4] => Array
(
[0] => Array
(
[0] => fr
[1] => lyon
[2] => b9
)
[1] => 0
[2] => 8608
)
And so on...
I'm trying to transform it to :
[2] => Array
(
[0] => fr
[1] => paris
[2] => 0
[3] => 116729
)
[3] => Array
(
[0] => fr
[1] => marseille
[2] => 0
[3] => 12898
)
[4] => Array
(
[0] => fr
[1] => lyon
[2] => 0
[3] => 8608
)
And here's how i do it :
foreach ($city as $key => $value){
$city[$key][0] = $city[$key][0][0];
array_splice( $city[$key], 1, 0, $city[$key][0][1] );
}
But the issue here is that the result array :
[2] => Array
(
[0] => fr
[1] => r
[2] => 0
[5] => 0
)
[3] => Array
(
[0] => fr
[1] => r
[2] => 0
[5] => 0
)
[4] => Array
(
[0] => fr
[1] => r
[2] => 0
[5] => 0
)
What is the error in my code and can I fix it?
You can use array_map instead of foreach:
$result = array_map(
function ($v) { return array_merge(array_slice($v[0], 0, 2), array_slice($v, 1)); },
$array
);
Demo
After you do:
$city[$key][0] = $city[$key][0][0];
you can't reference the original $city[$key][0] because you've replaced it. The solution is to save it in a variable first:
foreach ($city as $key => &$value){
$subarray = $value[0];
$value[0] = $subarray[0];
array_splice($value, 1, 0, $subarray[1] );
}
I've also used a reference to avoid having to repeat $city[$key] in the loop.
Try this
foreach($city as $key => $value) {
$city[$key] = [
$value[0][0], //fr
$value[0][1], //lyon
$value[1], //0
$value[2], //8608
];
}
Here you go (provided that your array is called $new in the example below):
foreach ($item as $derp) {
if (is_array($derp)) {
foreach ($derp as $derrrrrr) {
$d[$i][] = $derrrrrr;
}
} else {
$d[$i][] = $derp;
}
}
$i++;
}
?>
Returns
Array
(
[0] => Array
(
[0] => fr
[1] => paris
[2] => a8
[3] => 0
[4] => 11678
)
[1] => Array
(
[0] => fr
[1] => paris
[2] => a8
[3] => 0
[4] => 11678
)
[2] => Array
(
[0] => fr
[1] => paris
[2] => a8
[3] => 0
[4] => 11678
)
)
EDIT
Use the below to ignore the index[2] in the first multi-dimensional array.
$d = array();
$i = 0;
foreach ($new as $item) {
foreach ($item as $derp) {
if (is_array($derp)) {
foreach ($derp as $key => $derrrrrr) {
if ($key !== 2) {
$d[$i][] = $derrrrrr;
}
}
} else {
$d[$i][] = $derp;
}
}
$i++;
}
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.
I have this array and I want to sum up all Hits which figures at the $day[$key][2]
Array
(
[1] => Array
(
[0] => 01/07/13
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 02/07/13
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 03/07/13
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 04/07/13
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on .I've tried this and it doesn't seem to work!
foreach ($day as $key => $value){
$day[$key][2] += $day[$key][2];
}
Any errors in my code ? Thanks
Do it with:
$result = array_reduce($array, function(&$cur, $x)
{
return $cur+=$x[2];
}, 0);
You code also has correct logic, but you shouldn't try to modify existing array values. Just sum up into some result variable.
Something like this ?
$hits=0;
foreach($arr as $k=>$arr)
{
$hits+=$arr[2];
}
echo $hits; //"prints" 1362
Demo
I need to remove all months with 0 values, it must only remove it if all match 0.
Here is my array,
Array
(
[0] => Array
(
[0] => Month/Year
[1] => BRO002
[2] => SPI001
[3] => TYN001
)
[1] => Array
(
[0] => Jan
[1] => 0
[2] => 0
[3] => 1
)
[2] => Array
(
[0] => Feb
[1] => 0
[2] => 0
[3] => 0
)
[3] => Array
(
[0] => Mar
[1] => 0
[2] => 0
[3] => 0
)
[4] => Array
(
[0] => Apr
[1] => 0
[2] => 0
[3] => 0
)
[5] => Array
(
[0] => May
[1] => 0
[2] => 3
[3] => 0
)
[6] => Array
(
[0] => Jun
[1] => 0
[2] => 0
[3] => 0
)
[7] => Array
(
[0] => Jul
[1] => 0
[2] => 0
[3] => 0
)
[8] => Array
(
[0] => Aug
[1] => 0
[2] => 0
[3] => 0
)
[9] => Array
(
[0] => Sep
[1] => 11
[2] => 1
[3] => 2
)
[10] => Array
(
[0] => Oct
[1] => 0
[2] => 0
[3] => 0
)
[11] => Array
(
[0] => Nov
[1] => 0
[2] => 0
[3] => 0
)
[12] => Array
(
[0] => Dec
[1] => 0
[2] => 0
[3] => 0
)
)
The above array would turn into the following,
Array
(
[0] => Array
(
[0] => Month/Year
[1] => BRO002
[2] => SPI001
[3] => TYN001
)
[1] => Array
(
[0] => Jan
[1] => 0
[2] => 0
[3] => 1
)
[5] => Array
(
[0] => May
[1] => 0
[2] => 3
[3] => 0
)
[9] => Array
(
[0] => Sep
[1] => 11
[2] => 1
[3] => 2
)
)
Thank you.
Check each value:
$newArray = array();
foreach($array as $key => $val){
if(!(!$val[1] && !$val[2] && !$val[3])) {
$newArray[$key] = $array[$key];
}
}
print_r($newArray);
$array = array(
array('Jan', 0, 0, 1),
array('Feb', 0, 1, 0),
array('Mar', 0, 0, 0),
array('Apr', 1, 1, 1)
);
foreach($array as $i => $a) {
$save = false;
foreach($a as $k => $v) {
if($k == 0) continue;
if($v != 0) $save = true;
}
if(!$save) unset($array[$i]);
}
var_dump(array_values($array));
Without needing a second data structure:
foreach ( $data as $i => $month ) {
if ( $i == 0 ) { continue; }
if (
$month[1] == 0 &&
$month[2] == 0 &&
$month[3] == 0
) {
unset($data[$i]);
}
}
$Temp = array();
foreach ($Array as $Key=>$Value)
if ($Value[1]!=0 || $Value[2]!=0 || $Value[3]!=0)
$Temp[$Key] = $Value;
$Array = $Temp;