I have an array $session that I extract from an awstats file:
# Session range - Number of visits
BEGIN_SESSION 7
1h+ 10
5mn-15mn 9
0s-30s 107
2mn-5mn 7
30s-2mn 21
15mn-30mn 4
30mn-1h 11
END_SESSION
First I wanted to rearrange this by adding the two values of 0s-30s & 30s-2mn and creating another one, here's how I tried it:
$newline="\n";
$lines = explode($newline,$session);
$results = array();
foreach($lines as $line) {
$parts = explode(" ",trim($line),2);
if( count($parts) < 2) continue;
else {
$results[$parts[0]] = intval($parts[1]);
}
}
$temp['0s-30s'] = (isset($results['0s-30s'])?$results['0s-30s']:NULL);
$temp['30s-2mn'] = (isset($results['30s-2mn'])?$results['30s-2mn']:NULL);
$results['0s-2mn'] = $temp['0s-30s'] + $temp['30s-2mn'];
unset($results['0s-30s'],$results['30s-2mn']);
$session = $results['BEGIN_SESSION'].$newline;
foreach($results as $k=>$v) $session .= $k." ".$v.$newline;
$session .= "END_SESSION";
$session = explode("\n", $session) ;
unset($session[(count($session)-1)]) ;
unset($session[0]) ;
unset($session[1]) ;
$sessions = array();
foreach ($session as $key => $value) {
$session[$key] = explode(" ", $value) ;
$sessions[] = array($session[$key][0],trim($session[$key][1])) ;
}
and it displays me this array :
Array
(
[0] => Array
(
[0] => 1h+
[1] => 10
)
[1] => Array
(
[0] => 5mn-15mn
[1] => 9
)
[2] => Array
(
[0] => 2mn-5mn
[1] => 7
)
[3] => Array
(
[0] => 15mn-30mn
[1] => 4
)
[4] => Array
(
[0] => 30mn-1h
[1] => 11
)
[5] => Array
(
[0] => 0s-2mn
[1] => 128
)
)
Is there a way to rearrange my array like this:
Array
(
[0] => Array
(
[0] => 1h+
[1] => 10
)
[1] => Array
(
[0] => 30mn-1h
[1] => 11
)
[2] => Array
(
[0] => 15mn-30mn
[1] => 4
)
[3] => Array
(
[0] => 5mn-15mn
[1] => 9
)
[4] => Array
(
[0] => 2mn-5mn
[1] => 7
)
[5] => Array
(
[0] => 0s-2mn
[1] => 128
)
)
Knowing that $session sometimes come with missing sessions. Any help with this? Thanks.
<?php
$session = 'BEGIN_SESSION 7
1h+ 10
5mn-15mn 9
0s-30s 107
2mn-5mn 7
30s-2mn 21
15mn-30mn 4
30mn-1h 11
END_SESSION';
$newline="\n";
$lines = explode($newline,$session);
$results = array();
foreach($lines as $line) {
$parts = explode(" ", trim($line), 2);
if (in_array($parts[0], array('BEGIN_SESSION', 'END_SESSION'))) continue;
else $results[$parts[0]] = intval($parts[1]);
}
$temp['0s-30s'] = isset($results['0s-30s']) ? $results['0s-30s'] : 0;
$temp['30s-2mn'] = isset($results['30s-2mn']) ? $results['30s-2mn'] : 0;
$results['0s-2mn'] = $temp['0s-30s'] + $temp['30s-2mn'];
unset($results['0s-30s'], $results['30s-2mn']);
$order = array('0s-2mn', '2mn-5mn', '5mn-15mn', '15mn-30mn', '30mn-1h', '1h+');
uksort($results, function($a, $b) use ($order) {
return array_search($a, $order) < array_search($b, $order);
});
var_dump($results);
Related
$country = explode(",",$post_array['allcountry']);
foreach ($country as $count) {
$array_set = explode("=",$count);
pr($array_set);
}
die;
O:P-
Array
(
[0] => 5
[1] => 100
)
Array
(
[0] => 11
[1] => 150
)
Array
(
[0] => 13
[1] => 200
)
But I need this array to Array
(
[5] => 100,[11] => 150, [13] => 200
)`
How is it possible ?
You need to make first value of exploded array as key and second as its value.
Following is the modified code:
$country = explode(",",$post_array['allcountry']);
$formattedArr = array();
foreach ($country as $count) {
$array_set = explode("=",$count);
$formattedArr[$array_set[0]] = $array_set[1];
}
print_r($formattedArr);
you can change your code to be:
$country = explode(",",$post_array['allcountry']);
$new_arr = array();
foreach ($country as $count) {
$array_set = explode("=",$count);
$new_arr[$count[0]] = $count[1] ;
}
print_r($new_arr);
die;
Here's a simple way to do it:
$input = '5=100,11=150,13=200';
$output = parse_ini_string( str_replace( ',', "\n", $input ) );
Output:
Array
(
[5] => 100
[11] => 150
[13] => 200
)
If you have an array like this:
O:P- Array ( [0] => 5 [1] => 100 ) Array ( [0] => 11 [1] => 150 ) Array ( [0] => 13 [1] => 200 ) ---->>$yourArray
and you need it to be like this:
But I need this array to Array ( [5] => 100,[11] => 150, [13] => 200 ) How is it possible ?
$finalArray = array();
foreach($yourArray as $inside){
$finalArray[$inside[0]]= $inside[1];
}
I have an array a bit like:
Array (
[1] => Array
(
[1] => 21
[2] => 3
[0] => Analyst
)
[2] => Array
(
[1] => 47
[2] => 8
[0] => Catalysis
)
[3] => Array
(
[1] => 1
[2] => 0
[0] => Biomaterials
)
[4] => Array
(
[3] => 12
[4] => 2
[0] => Analyst
)
[5] => Array
(
[5] => 12
[6] => 2
[0] => Analyst
)
...
However I would like to renumber those entries with the same [0] value so that I end up with
[1] => Array
(
[1] => 21
[2] => 3
[3] => 12
[4] => 2
[5] => 12
[6] => 2
[0] => Analyst
)
So far I've tried getting the [0] values out of the $results array by putting them in their own array and saying if you're already there then add [3] and [4] to where [1] and [2] are in a new array but it's not working.
$final = array();
$jArray = array();
foreach($results as $key => $result) {
if(!in_array($result[0],$jArray) && !empty($result[0])) {
$jArray[$i] = $result[0];
$i++;
}
}
for($x = 0; $x < count($results); $x++) {
$k = array_search($results[$x][0],$jArray);
if(!isset($results[$x][1]))
$final[$k][1] = $results[$x][1];
if(!isset($results[$x][2]))
$final[$k][2] = $results[$x][2];
if(!isset($results[$x][3]))
$final[$k][3] = $results[$x][3];
if(!isset($results[$x][4]))
$final[$k][4] = $results[$x][4];
if(!isset($results[$x][5]))
$final[$k][5] = $results[$x][5];
if(!isset($results[$x][6]))
$final[$k][6] = $results[$x][6];
}
Any simpler ways of doing this?
You can do this way...
$new_arr=array();
$arkeys = array_unique(array_map(function ($v){ return $v[0];},$arr));
foreach($arr as $k=>$arr1)
{
$new_arr[$arr1[0]][]=array_slice($arr1,0,count($arr1)-1);
}
foreach($arkeys as $v)
{
$new_arr[$v] = call_user_func_array('array_merge', $new_arr[$v]);
}
print_r($new_arr);
OUTPUT :
Array
(
[Analyst] => Array
(
[0] => 21
[1] => 3
[2] => 12
[3] => 2
[4] => 12
[5] => 2
)
[Catalysis] => Array
(
[0] => 47
[1] => 8
)
[Biomaterials] => Array
(
[0] => 1
[1] => 0
)
)
Working Demo
If you just want to group by the first element of the sub array, a single loop is enough:
$result = array();
foreach ($array as $sub_arr) {
$key = $sub_arr[0];
unset($sub_arr[0]);
if (!isset($result[$key])) {
$result[$key] = array();
}
$result[$key] += $sub_arr;
}
Here
$final = array();
foreach($results as $key => $result) {
if (!in_array($result[0], array_keys( $final ) ) && !empty($result[0])) {
$final[$result[0]] = array( $result[0] );
}
foreach($result as $k => $v) {
if ($k != 0 && isset($v)) {
$final[$result[0]][$k] = $v;
}
}
}
This question already has answers here:
php - how to merge 2d array that have different no of element for each array
(4 answers)
Closed 9 years ago.
I have 3 set of 2d array that have different element number?One is the array that need to be as key(date and time) to compare with other two array.The other two array must in sequence.For row that empty, i want set as -9999.
First array :
Array (
[0] => Array
(
[0] => 01/7/2013
[1] => 12.00 a.m
)
[1] => Array
(
[0] => 01/7/2013
[1] => 12.01 a.m
)
[2] => Array
(
[0] => 01/7/2013
[1] => 12.02 a.m
)
)
For second array:
Array(
[0] => Array
(
[0] => 01/7/2013
[1] => 12.01 a.m
[2] => 9.02
)
)
For Third array:
Array(
[0] => Array
(
[0] => 01/7/2013
[1] => 12.00 a.m
[2] => 1.23
[3] => 6.1
)
[1] => Array
(
[0] => 01/7/2013
[1] => 12.02 a.m
[2] => 1.75
[3] => 1.75
)
)
and i need the output as:
Array(
[0] => Array
(
[0] => 01/7/2013
[1] => 12.00 a.m
[2] => -9999
[3] => 1.23
[4] => 6.1
)
[1] => Array
(
[0] => 01/7/2013
[1] => 12.01 a.m
[2] => 9.02
[3] => -9999
[4] => -9999
)
[2] => Array
(
[0] => 01/7/2013
[1] => 12.02 a.m
[2] => -9999
[3] => 1.75
[4] => 1.75
)
)
$arr = array_merge($arr1, $arr2, $arr3);
$out = array();
$cnt = 0;
foreach ($arr as $key => $value){
$date = explode('/', $value[0]);//Change date format
$dex = strtotime($date[1].'/'.$date[0].'/'.$date[2].' '.$value[1]);
$head = (array_key_exists($dex, $out)) ? $out[$dex] : array_slice($value, 0, 2);
$out[$dex] = array_merge($head, array_slice($value, 2));
$cnt = ($cnt > count($out[$dex])) ? $cnt : count($out[$dex]);
}
$out = array_map(function ($e) use ($cnt){
return array_pad($e, $cnt, '-9999');
}, array_values($out));
print_r($out);
That works for PHP 5.3 and later. For older versions of PHP, you can replace the array_map function with the following:
foreach ($out as $key => $value){
$res[] = array_pad($value, $cnt, -9999);
}
print_r($res);
NEW VERSION (for PHP 5.3 or later):
$template = array();
array_walk($arr1, function (&$e, $k) use (&$template){
$date = explode('/', $e[0]);
$dex = strtotime($date[1].'/'.$date[0].'/'.$date[2].' '.$e[1]);
$template[$dex][0] = '-9999';
});
$arr1 = array_combine(array_keys($template), $arr1);
$out = array();
$data = array($arr2, $arr3);
foreach ($data as $key => $value){
foreach ($value as $key2 => $value2){
$date = explode('/', $value2[0]);//Change date format
$dex = strtotime($date[1].'/'.$date[0].'/'.$date[2].' '.$value2[1]);
$out[$key][$dex] = array_slice($value2, 2);
}
$out[$key] += array_diff_key($template, $out[$key]);
foreach ($out[$key] as $key2 => $value2){
$arr1[$key2] = array_merge($arr1[$key2], $value2);
}
}
print_r(array_values($arr1));
I have an array $matrix_part, containing arrays, and I want to rekey the inner keys to start at 1.
I am trying to code below but it doesn't work - it just stores the new array identically.
$temp_matrix = array();
foreach ($matrix_part as $k => $v){
$temp_matrix[$k++] = $v;
}
$matrix_part = $temp_matrix;
Source array:
Array
(
[0] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[1] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[2] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
)
Desired output:
Array
(
[0] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[1] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[2] => Array
(
[1] => 163
[1] => 23
[3] => 97
)
)
Try use:
instead of this:
$temp_matrix[$k++] = $v;
do this:
$temp_matrix[++$k] = $v;
This maybe?
$input = array(
array(163, 23, 97),
array(163, 23, 97),
array(163, 23, 97),
);
$output = array_map(function ($innerArray) {
return array_combine(range(1, sizeof($innerArray)), $innerArray);
}, $input);
print_r($output);
foreach ($a as $outer_k => $outer_v) {
for ($i = count($outer_v) - 1; $i >= 0; $i--) {
$outer_v[$i+1] = $outer_v[$i];
}
unset($outer_v[0]);
$a[$outer_k] = $outer_v;
}
where $a is your input array
Could do something like ...
foreach ($matrix as $k=>$v) {
foreach ($v as $k2=>$v2) {
$tmp_arr[$k][$k2+1] = $v2;
}
}
$matrix = $tmp_arr;
I'm having two arrays in PHP as follows:
Array1 ( [1] => Array ( [0] => 16 )
[2] => Array ( [0] => 17 [1] => 29 )
[3] => Array ( [0] => 30 [1] => 31 )
) Total Element Count: 5
Array2 ( [1] => Array ( [0] => 21 )
[2] => Array ( [0] => 22 )
[3] => Array ( [0] => 23 )
[4] => Array ( [0] => 24 [1] => 25 )
[5] => Array ( [0] => 43 [1] => 44 )
) Total Element Count: 7
I want to pair above two arrays depending on count of first array, that means, first five elements of Array2 should get mixed with Array1 with outer 1D keys remaining intact.
Output should be as follows:
Output Array( [1] => Array ( [0] => 16 [1] => 21)
[2] => Array ( [0] => 17 [1] => 29 [2] => 22)
[3] => Array ( [0] => 30 [1] => 31 [2] => 23 )
[4] => Array ( [0] => 24 [1] => 25 )
)
If you want to avoid E_STRICT warnings:
function combine(array $arr1, array $arr2) {
$ret = array();
foreach ($arr1 as $k => $v) {
if (!array_key_exists($k, $ret)) {
$ret[$k] = array();
}
$ret[$k][] = $v;
}
foreach ($arr2 as $k => $v) {
if (!array_key_exists($k, $ret)) {
$ret[$k] = array();
}
$ret[$k][] = $v;
}
return $ret;
}
If you prefer a shorter version:
function combine(array $arr1, array $arr2) {
$ret = array();
foreach ($arr1 as $k => $v) {
$ret[$k][] = $v;
}
foreach ($arr2 as $k => $v) {
$ret[$k][] = $v;
}
return $ret;
}
What about http://www.php.net/manual/en/function.array-merge-recursive.php :P