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;
Related
Its been four hours I have search everywhere on google and SOF but unable to find the answer. This is what i have tried so far. Here is my code so far
$tmp_array = array();
foreach ($cms as $key => $val) {
$cDate = date('Ym', strtotime($val['day_date']));
$tmp_ids[] = $val['id'];
if (array_key_exists($cDate, $tmp_array)) {
$tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
$tmp_array[$cDate]['ids'] = $tmp_ids;
} else {
$tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
$tmp_array[$cDate]['ids'] = $tmp_ids;
}
}
its output is coming like this,
Array
(
[202001] => Array
(
[new_visitors] => 797
[ids] => Array
(
[0] => 31
[1] => 32
)
)
[202002] => Array
(
[new_visitors] => 461
[ids] => Array
(
[0] => 31
[1] => 32
[2] => 33
[3] => 34
)
)
)
but i want the result array like this,
Array
(
[202001] => Array
(
[new_visitors] => 797
[ids] => Array
(
[0] => 31
[1] => 32
)
)
[202002] => Array
(
[new_visitors] => 461
[ids] => Array
(
[0] => 33
[1] => 34
)
)
)
Any suggestions, what am i doing in my code?
The ids are basicall primary key of the table and the "new_visitors" are count of the visitor those who visit on my site. but i havent find any solution so far.
Here is my $cms array.
Array
(
[0] => Array
(
[id] => 31
[day_date] => 2020-01-30 00:00:00
[new_visitors] => 459
)
[1] => Array
(
[id] => 32
[day_date] => 2020-01-31 00:00:00
[new_visitors] => 338
)
[2] => Array
(
[id] => 33
[day_date] => 2020-02-01 00:00:00
[new_visitors] => 242
)
[3] => Array
(
[id] => 34
[day_date] => 2020-02-02 00:00:00
[new_visitors] => 219
)
)
Thank you misorude, your comment worked
$tmp_array = array();
foreach ($cms as $key => $val) {
$cDate = date('Ym', strtotime($val['day_date']));
if (array_key_exists($cDate, $tmp_array)) {
$tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
$tmp_array[$cDate]['ids'][] = $val['id'];
} else {
$tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
$tmp_array[$cDate]['ids'][] = $val['id'];
}
}
$arr = [
['id' => 31 ,'day_date'=> "2020-01-30 00:00:00", 'new_visitors' => 459],
['id' => 32 ,'day_date'=> "2020-01-31 00:00:00", 'new_visitors' => 338],
['id' => 33 ,'day_date'=> "2020-02-01 00:00:00", 'new_visitors' => 242],
['id' => 34 ,'day_date'=> "2020-02-02 00:00:00", 'new_visitors' => 219]
];
echo "<pre>";
print_r($arr);
$data = [];
$add_visitor = 0;
$day_date = '';
foreach($arr as $arr_value){
if(date('Ym', strtotime($arr_value['day_date'])) != $day_date){
$add_visitor = 0;
}
$add_visitor += $arr_value['new_visitors'];
$data[date('Ym', strtotime($arr_value['day_date']))]['new_visitors'] = $add_visitor;
$data[date('Ym', strtotime($arr_value['day_date']))]['ids'][] = $arr_value['id'];
$day_date = date('Ym', strtotime($arr_value['day_date']));
}
print_r($data);
I tried to port a function that generates permutations from this answer to PHP and I came out with the following:
function recurse($s, $arrs, $k) {
if ($k === count($arrs)) {
echo $s.' ';
} else {
foreach ($arrs[$k] as $o) {
recurse($s.$o, $arrs, $k + 1);
}
}
}
which gives me the correct output 137 138 147 148 237 238 247 248
Now I want to have the output as an array and not as a string, but after the edit for some reason I get wrong results:
function generatePermutations($s, $arrs, $k) {
if ($k === count($arrs)) {
print_r($s);
} else {
foreach ($arrs[$k] as $o) {
$s[] = $o;
generatePermutations($s, $arrs, $k + 1);
}
}
}
Output:
Array
(
[0] => 1
[1] => 3
[2] => 7
)
Array
(
[0] => 1
[1] => 3
[2] => 7
[3] => 8
)
Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 7
)
Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 7
[4] => 8
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 7
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 7
[4] => 8
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 7
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 7
[5] => 8
)
This is the input for both functions
$in = array( array(1, 2), array(3, 4), array(7, 8) );
recurse("", $in, 0);
generatePermutations(array(), $in, 0);
What did I do wrong?
if you use $s[] = $o; in your recursive function, it will slice your array for every digit.
You can add a "result" parameter, which will hold your resultset as an array.
Notice the & sign. Without it, the parameter is not "writable", only "readable".
$in = array( array(1, 2), array(3, 4), array(7, 8) );
$result = array();
generatePermutations("", $in, 0, $result);
print_r($result);
function generatePermutations($s, $arrs, $k, &$result) {
if ($k === count($arrs)) {
$result[] = $s;
} else {
foreach ($arrs[$k] as $o) {
generatePermutations($s.$o, $arrs, $k + 1, $result);
}
}
}
Output :
Array
(
[0] => 137
[1] => 138
[2] => 147
[3] => 148
[4] => 237
[5] => 238
[6] => 247
[7] => 248
)
Edit : Modification to get Robotex's expected output :
$in = array( array(1, 2), array(3, 4), array(7, 8) );
$result = array();
$s = array();
generatePermutations($s, $in, 0, $result);
print_r($result);
function generatePermutations(&$s, $arrs, $k, &$result) {
if ($k === count($arrs)) {
array_push($result, $s);
} else {
foreach ($arrs[$k] as $o) {
array_push($s, $o);
generatePermutations($s, $arrs, $k + 1, $result);
array_pop($s);
}
}
}
Output :
Array
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 7
)
[1] => Array
(
[0] => 1
[1] => 3
[2] => 8
)
[2] => Array
(
[0] => 1
[1] => 4
[2] => 7
)
[3] => Array
(
[0] => 1
[1] => 4
[2] => 8
)
[4] => Array
(
[0] => 2
[1] => 3
[2] => 7
)
[5] => Array
(
[0] => 2
[1] => 3
[2] => 8
)
[6] => Array
(
[0] => 2
[1] => 4
[2] => 7
)
[7] => Array
(
[0] => 2
[1] => 4
[2] => 8
)
)
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;
}
}
}
I have this array:
Array
(
[0] => Array
(
[tag_id] => 1
)
[2] => Array
(
[tag_id] => 3
)
[22] => Array
(
[tag_id] => 44
)
[23] => Array
(
[tag_id] => 45
)
[25] => Array
(
[tag_id] => 47
)
[26] => Array
(
[tag_id] => 48
)
)
I'd like it to look something like this so its simpler for me to loop through and insert each value into a database:
Array
(
[0] => 1
[1] => 3
[2] => 44
[3] => 45
[4] => 47
[5] => 48
)
You can use array_map.
PHP 5.3 or higher:
$callback = function($value) {
return $value['tag_id'];
};
$result = array_map($callback, $array);
Below 5.3:
function collapseTagIds($value) {
return $value['tag_id'];
}
$result = array_map('collapseTagIds', $array);
Well, you could do this:
$new_array = array();
foreach($array as $key => $value)
{
$new_array[$key] = $value['tag_id'];
}
print_r($new_array);
In your case, you have just one index on $value. If you don't want to specify the index name just do it:
$new_array = array();
foreach($array as $key => $value) {
$new_array[$key] = reset($value);
}
print_r($new_array);
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