How to store 3 key by index per row PHP [duplicate] - php

This question already has an answer here:
Multi dimensional loops from 4 existing arrays [duplicate]
(1 answer)
Closed 10 months ago.
i have problem with array, i have 3 array and i wont to merge into one by row index
$tahun = array(2010,2011,2012,2013,2014);
$status = array("akademi","instansi","umum","pending","pass");
$total = array(2,1,3,4,5);
i want this array into want like this
array(
0 => array(2010,"akademi",2),
1 => array(2011,"instansi",1),
2 => array(2012,"umum",3),
3 => array(2013,"pemding",4),
4 => array(2014,"pass",5),
);
but when i use array_merge_recursive() the output like this
Array
(
[0] => 2015
[1] => 2016
[2] => 2017
[3] => 2018
[4] => 2019
[5] => 2019
[6] => 2019
[7] => 1
[8] => 1
[9] => 1
[10] => 1
[11] => 3
[12] => 2
[13] => 1
[14] => akademisi
[15] => instansi pemerintah
[16] => umum
[17] => umum
[18] => akademisi
[19] => instansi pemerintah
[20] => umum
)

You can use array_map() with null as the callback...
$combined = array_map(null, $tahun, $status, $total);
from the manual...
NULL can be passed as a value to callback to perform a zip operation
on multiple arrays. If only array1 is provided, array_map() will
return the input array.

Use simple foreach loop:
$res = [];
foreach($tahun as $ind=>$val){
$res[$ind] = [$val, $status[$ind], $total[$ind]];
}
Demo

Related

Compare Array elements and add based on key and value

I have two arrays like this:
$array_1 = Array ( [0] => 4 [1] => 6 [2] => 2 [3] => 6 [4] => 4 [5] => 10 [6] => 4 [7] => 6 [8] => 2 [9] => 2 [10] => 4 [11] => 4 [12] => 2 [13] => 2 );
$array_2 = Array ( [0] => DK [1] => GA [2] => DK [3] => GA [4] => DK [5] => GA [6] => WE [7] => VE [8] => WE [9] => VE [10] => PLA [11] => PRA [12] => PLA [13] => PRA ) ;
Now I want result like this:
$dk=4+2+4=10;
$ga=6+6+10=22;
$we=4+2=6;
$ve=6+2=8;
$pla=4+2=6;
$pra=4+2;
Explanation:
In $array_2, 'DK' exists 3 times and key values are = 0,2 and 4.
So, i have to add the values of $array_1 having key 0,2,4 and assign them to $dk. Here, $dk will be 4+2+4=10. This process will be same for all other variables.
How can i do this??
Instead separate variable name I suggest you to make array like this
<?php
$array_1 = [4,6,2,6];
$array_2 = [ 0=> "DK", 1=>"GA", 2=>"DK", 3=>"GA"];
$newArray = [];
foreach($array_2 as $key=>$value){
if(isset($newArray[$value])){
$newArray[$value] +=$array_1[$key];
}else{
$newArray[$value] =$array_1[$key];
}
}
print_r($newArray);
?>
Live Demo
Output :
Array
(
[DK] => 6
[GA] => 12
)
Another suggestion : Instead complex programming try to make good relation or binding to not get any inconsistency in records
This will loop array2 and build an array with the sum.
Then output it (just to see the result), then I use extract to pull out the variables as you want them.
But I would rather keep them in the array
Foreach($array_2 as $key => $val){
If(!isset($new[$val])) $new[$val] =0;
$new[$val] += $array_1[$key];
}
Var_dump($new);
Extract($new);
https://3v4l.org/jOR7Z

2 arrays count how many times id = date

Ive sat with this for a while now, and its getting late.
Im trying to get a top 3 of most sales from last month, and i need to count how many times a id from array 1 is equal to array 2 last month(6 = last atm.) like id 4 = 2, id 7 = 3
It might not be the perfect solution, but im just trying to break it down by my self, so later on 'maybe' problems, will i take care of when i hit the wall,
so please, if anyone can help me here, ill be greatfull.
UPDATE
- I will add the result im looking for here: (sorry i didnt before, it makes it alot easier :-)
- The result below, is because i want the count from 2014-06-01 and up to the last day of that month monly, on array[0][1] under this array, only 6-7-8 is not from 2014-06
Hope it makes a bit more sense now ^^
Array
(
[0] => Array
(
[0] => Array
(
[4] => 2
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
)
)
Array
(
[0] => Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 7
[3] => 1
[4] => 7
[5] => 7
[6] => 3
[7] => 3
[8] => 4
[9] => 9
[10] => 12
[11] => 2
[12] => 13
[13] => 1
)
[1] => Array
(
[0] => 2014-06-18
[1] => 2014-06-10
[2] => 2014-06-05
[3] => 2014-06-05
[4] => 2014-06-12
[5] => 2014-06-11
[6] => 2013-12-12
[7] => 2014-07-23
[8] => 2014-05-13
[9] => 2014-06-01
[10] => 2014-06-12
[11] => 2014-06-04
[12] => 2014-06-04
[13] => 2014-06-11
)
)
)
I hope that what I understood is what you're really asking for. let's say your array definition is :
$arr = Array
(
0 => Array
(
0 => Array
(
0 => 4,
1 => 4,
2 => 7,
3 => 1,
4 => 7,
5 => 7,
6 => 3,
7 => 3,
8 => 4,
9 => 9,
10 => 12,
11 => 2,
12 => 13,
13 => 1
),
1 => Array
(
0 => "2014-06-18",
1 => "2014-06-10",
2 => "2014-06-05",
3 => "2014-06-05",
4 => "2014-06-12",
5 => "2014-06-11",
6 => "2013-12-12",
7 => "2014-07-23",
8 => "2014-05-13",
9 => "2014-06-01",
10 => "2014-06-12",
11 => "2014-06-04",
12 => "2014-06-04",
13 => "2014-06-11"
)
)
);
If you need to test if the date is lower than 6 month and then put their id, sales and date you need to use this code
$result = [];
$index=0;
foreach ($arr[0][0] as $key => $value)
{
$date1 = new DateTime($arr[0][1][$key]);
$date2 = new DateTime();
$diff = $date1->diff($date2);
$diff = ($diff->format('%y') * 12) + $diff->format('%m');
if($diff<=6)
{
$result[$index]['id'] = $key;
$result[$index]['sales'] = $value;
$result[$index]['date'] = $arr[0][1][$key];
$index++;
}
}
var_dump($result);
array_count_values() will give you the number of times each value appears in array 1.
$count = array_count_values($array[0][0]);
Result:
Array
(
[4] => 3
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
Then you can use a loop to combine with array 2:
$result = array();
foreach($count as $key=>$val) {
$result[$array[0][1][$key]] = $val;
}
Result:
Array
(
[2014-06-12] => 3
[2014-07-23] => 3
[2014-06-10] => 2
[2014-06-05] => 1
[2014-06-01] => 1
[2014-06-04] => 1
[2014-06-11] => 1
)
See demo

php move keys from array into another array

So I have this array:
[0] => 3
[1] => 9
[2] => 4
[3] => 6
[4] => 69
[5] => 8
[6] => 9
[7] => 12
[8] => 9
[9] => 7
And this one
[Far] => 1
[far] => 3
[away] => 1
[behind] => 1
[the] => 23
[word] => 2
[mountains] => 1
[from] => 3
[countries] => 1
[Vokalia] => 1
I would like that the values of the first array will overwrite the values of the second array without changing the keys of the second array.
I have already tried fiddling with the foreach function, but no prevail.
So in the end I would like it to look like this:
[Far] => 3
[far] => 9
[away] => 4
[behind] => 6
[the] => 69
[word] => 8
[mountains] => 9
[from] => 12
[countries] => 9
[Vokalia] => 7
does anyone know how to do that? And if yes, can that person give a bit more information how it works in the foreach function?
Assuming your arrays are $array1 and $array2:
$keys = array_keys($array2);
$result = array_combine($keys, $array1);
Documentation:
array_keys()
array_combine()
Online demo

Re-order PHP array by middle key as start (Circular Sorting)

very basic question however I have had some trouble finding the answers on PHP.NET.
I have the following array:
Array (
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
)
I want the array to be re-ordered so that the key number 3 in the first series of the array becomes the first, then the rest to be re-ordered from there to eventually get the result of:
Array (
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
)
I am looking for a way to do this so I can define the array, then the first level key I need to sort by, and then it will return the array in this way.
The standard PHP keys didn't seem to offer something like this, so it would be good to be able to have a separate function such as $newArray = reorder_array($array, $key);
I don't require any sorting of the second level, only the initial 4 main / first level array sections.
You help is greatly appreciated as I have been sitting on this one for awhile without a clear and simple solution.
You re-ordering can be simply implemented with one foreach loop, like:
function reorderArray($array, $key)
{
$found = false;
foreach($array as $k=>$v)
{
$found = $found || $k===$key;
if(!$found)
{
unset($array[$k]);
$array[$k] = $v;
}
//else break can be added for performance issues
}
return $array;
}
with usage
$array=[1=>'foo', 4=>'bar', 9=>'baz', 'test'=>51];
var_dump(reorderArray($array, 9));
var_dump(reorderArray($array, 'test'));
var_dump(reorderArray($array, 'no_such_key'));//original array in result
-check this demo. If keys are consecutive numerics, however, this can be easily implemented with array_slice() calls.

How to get "maximum range" from an array

Index is 00 to 23 (24-hour time)
What I am trying to accomplish is to get largest maximum range.
Each array will have a maximum of 24 elements. Of these, I want to get all the ones which are high (a range).
So,
for first array - 10,16,19,19
second - 18,19,20
third - 9,11.
Array
(
[00] => 10
[01] => 19
[02] => 10
[03] => 4
[04] => 1
[13] => 16
[14] => 2
[15] => 5
[16] => 2
[17] => 3
[18] => 1
[19] => 1
[20] => 1
[21] => 5
[22] => 1
[23] => 2
)
Array
(
[09] => 6
[10] => 20
[11] => 18
[12] => 19
[13] => 3
[15] => 11
[16] => 9
[18] => 10
)
Array
(
[00] => 4
[01] => 3
[12] => 4
[16] => 4
[21] => 9
[22] => 11
[23] => 6
)
The problem is, these values could get changed entirely, like this one -
Array
(
[13] => 117
[14] => 221
[15] => 211
[16] => 145
[17] => 23
[18] => 15
[19] => 1
)
Any solution for this?
Thank you,people.
It's a bit unclear how you want to choose which values are 'high', but:
In PHP 5.3+
$filetered = array_filter($yourArray, function($v) {return $v > 10;});
Will return array with values higher than 10.
In PHP <5.3 you will need to createa callback function instead of passing a closure to array_filter.
Use array_keys() and than max() to get the maximum key value:
$keys = array_keys($myArray);
$maxKey = max($keys);
I think what you want (although it's not very clear what is a "high" value) can be accomplished with asort(). If you sort your array numerically:
asort($yourarray, SORT_NUMERIC);
Then you can just retrieve the first elements from the sorted array. You don't even specify how many elements should be retrieved, but I'm guessing it's 3, although you don't seem to want anything below 10, so just use array_slice() to extract the last three elements and if any of them is lower than 10 discard it.
Putting it all together would give something like:
asort($yourarray, SORT_NUMERIC);
$top3 = array_slice($yourarray, -3, null, true)
$filtered = array_filter($top3, function($v) {return $v > 10;});
Note: I used Mchl example on the final part of my answer.
You can use array_filter() to filter out those values that are not within the range:
function is_between_10_and_20($v)
{
return ($v >= 10 && $v <= 20);
}
$result = array_filter($arr, "is_between_10_and_20")

Categories