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
Related
I've got a multidimensional array that I want to sort alphabetically by the values of specific key. Structure of the array is -
Array
(
[sr] => Array
(
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
)
[deptt] => Array
(
[2] => KKT-TICKETING
[3] => KKT-TICKETING
[4] => KKT-TICKETING
[5] => KKT-HOTELS
[6] => KKT-TICKETING
[7] => KKT-HOTELS
[8] => GTT-TICKETING
[9] => GTT-HOTELS
[10] => GTT-TICKETING
)
I wanted to sort the data on basis of key 'deptt' alphabetically by the values. My desired output should be like all 'GTT-HOTELS' data should shown first then 'KKT-HOTELS' -
[deptt] => Array
(
[2] => GTT-HOTELS
[3] => GTT-TICKETING
[4] => GTT-TICKETING
[5] => KKT-HOTELS
[6] => KKT-HOTELS
[7] => KKT-TICKETING
[8] => KKT-TICKETING
[9] => KKT-TICKETING
[10] => KKT-TICKETING
)
with corresponding values of key 'sr'.
Any ideas how to do this? Hope I could make you understand the scenario.
Given this data:
$data = [
'sr' =>
[
2 => 1,
3 => 2,
4 => 3,
5 => 4,
6 => 5,
7 => 6,
8 => 7,
9 => 8,
10 => 9,
],
'deptt' =>
[
2 => 'KKT-TICKETING',
3 => 'KKT-TICKETING',
4 => 'KKT-TICKETING',
5 => 'KKT-HOTELS',
6 => 'KKT-TICKETING',
7 => 'KKT-HOTELS',
8 => 'GTT-TICKETING',
9 => 'GTT-HOTELS',
10 => 'GTT-TICKETING',
]
];
The below code can be used. The magic is in the array_combine function which merges two arrays, using one for the keys and one for the values. However, in both cases, it takes the values of those arrays, so you need to call array_keys on the former to get those as values.
// Grab our arrays as variables
$sr = $data['sr'];
$deptt = $data['deptt'];
// Sort the latter by values alphabetically
sort($deptt);
// Merge them together, keys from the first and values from the second
$final = array_combine(array_keys($sr), $deptt);
This produces the following:
array (
2 => 'GTT-HOTELS',
3 => 'GTT-TICKETING',
4 => 'GTT-TICKETING',
5 => 'KKT-HOTELS',
6 => 'KKT-HOTELS',
7 => 'KKT-TICKETING',
8 => 'KKT-TICKETING',
9 => 'KKT-TICKETING',
10 => 'KKT-TICKETING',
)
Additional care should be taken to make sure that both arrays have the same number of items. array_combine will return false in that case.
I'm sorry because I'm probably not going to use the correct vocabulary here, but I'm trying to figure out "how-to" modify the following code so that it creates the array of arrays (Multidimensional Array). This code creates the structure illustrated in the image below, but I want it to create the array of arrays (Multidimensional Array) instead.
Basically, I want the 1001, 1002, 1004, etc... to be the main array. The nested arrays will be the strings that has the #1001, #1002, etc... in them. You'll notice the # in the string corresponds with number in the original array.
$combinedAssignmentData = [];
foreach($assignmentsYES as $key=>$assignedIDs){
$levels = array($assignedIDs);
foreach($levels as $key=>$level){
echo "<strong>$level</strong><br>";
foreach($studentIDsubmissions as $k=>$individualSubmission){
if (strpos($individualSubmission, $level) !== false) {
echo "--$individualSubmission<br>";
}
}
}
}
var_export($assignmentsYES);
array ( 0 => '1001', 1 => '1002', 2 => '1004', 3 => '1005', 4 => '1007', 5 => '1008', 6 => '1009', 7 => '1015', 8 => '1028', 9 => '1029', )
var_export($studentIDsubmissions);
array ( 0 => '346623#guhsd.net|TD-Share Test #1001|NO', 1 => '346623#guhsd.net|TD-Share Test #1001|NO', 2 => '346623#guhsd.net|TD-Share Test #1001|NO', 3 => '346623#guhsd.net|TD-Share Test #1001|NO', 4 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 5 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 6 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 7 => '346623#guhsd.net|TD-About Me #1004|YES', 8 => '346623#guhsd.net|TD-Calendar #1007|YES', 9 => '346623#guhsd.net|TD-Wage Tracker #1008|YES', 10 => '346623#guhsd.net|TD-Stock Portfolio #1009|YES', 11 => '346623#guhsd.net|TD-Collaboration #1005|YES', 12 => '346623#guhsd.net|TD-Stock Portfolio #1009|YES', 13 => '346623#guhsd.net|TD-Collaboration #1005|YES', 14 => '346623#guhsd.net|TD-Dream Vacation Presentation #1015|YES', )
Any help is greatly appreciated!
Todd
Here you go
$assignmentsYES = array ( 0 => '1001', 1 => '1002', 2 => '1004', 3 => '1005', 4 => '1007', 5 => '1008', 6 => '1009', 7 => '1015', 8 => '1028', 9 => '1029', );
$studentIDsubmissions = array ( 0 => '346623#guhsd.net|TD-Share Test #1001|NO', 1 => '346623#guhsd.net|TD-Share Test #1001|NO', 2 => '346623#guhsd.net|TD-Share Test #1001|NO', 3 => '346623#guhsd.net|TD-Share Test #1001|NO', 4 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 5 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 6 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 7 => '346623#guhsd.net|TD-About Me #1004|YES', 8 => '346623#guhsd.net|TD-Calendar #1007|YES', 9 => '346623#guhsd.net|TD-Wage Tracker #1008|YES', 10 => '346623#guhsd.net|TD-Stock Portfolio #1009|YES', 11 => '346623#guhsd.net|TD-Collaboration #1005|YES', 12 => '346623#guhsd.net|TD-Stock Portfolio #1009|YES', 13 => '346623#guhsd.net|TD-Collaboration #1005|YES', 14 => '346623#guhsd.net|TD-Dream Vacation Presentation #1015|YES', );
$combinedAssignmentData = [];
foreach($assignmentsYES as $key=>>$level){
$combinedAssignmentData[$level] =
array_filter(
$studentIDsubmissions,
function($item)use($level){
return strpos($item, '#'.$level) !== false;
}
);
}
print_r($combinedAssignmentData);
Output
Array
(
[1001] => Array
(
[0] => 346623#guhsd.net|TD-Share Test #1001|NO
[1] => 346623#guhsd.net|TD-Share Test #1001|NO
[2] => 346623#guhsd.net|TD-Share Test #1001|NO
[3] => 346623#guhsd.net|TD-Share Test #1001|NO
)
[1002] => Array
(
[4] => 346623#guhsd.net|TD-No Excuse Reflection #1002|YES
[5] => 346623#guhsd.net|TD-No Excuse Reflection #1002|YES
[6] => 346623#guhsd.net|TD-No Excuse Reflection #1002|YES
)
[1004] => Array
(
[7] => 346623#guhsd.net|TD-About Me #1004|YES
)
[1005] => Array
(
[11] => 346623#guhsd.net|TD-Collaboration #1005|YES
[13] => 346623#guhsd.net|TD-Collaboration #1005|YES
)
[1007] => Array
(
[8] => 346623#guhsd.net|TD-Calendar #1007|YES
)
[1008] => Array
(
[9] => 346623#guhsd.net|TD-Wage Tracker #1008|YES
)
[1009] => Array
(
[10] => 346623#guhsd.net|TD-Stock Portfolio #1009|YES
[12] => 346623#guhsd.net|TD-Stock Portfolio #1009|YES
)
[1015] => Array
(
[14] => 346623#guhsd.net|TD-Dream Vacation Presentation #1015|YES
)
[1028] => Array
(
)
[1029] => Array
(
)
)
Sandbox
*PS
I added a # in here strpos($item, '#'.$level), which will improve the accuracy a bit. It would be better to use a regular expression (in the array filter callback)
function($item)use($level){
return preg_match('/#'.$level.'\|/', $item); //match `#{id}|`
}
Consider for example matching 1001 to id 10012 ~ strpos will just match the 1001 part with no regard.
If the oddly numbered keys for the sub array bug you you can wrap the array_filter in array_values(array_filter(....)); to reset them. Array filter retains the keys from the original array. In most cases the keys don't really matter, so I wouldn't worry about it unless you really have to.
Update
After thinking about it and posting this
be better to use a regular expression
Why don't we go with this one:
$assignmentsYES = array ( 0 => '1001', 1 => '1002', 2 => '1004', 3 => '1005', 4 => '1007', 5 => '1008', 6 => '1009', 7 => '1015', 8 => '1028', 9 => '1029', );
$studentIDsubmissions = array ( 0 => '346623#guhsd.net|TD-Share Test #1001|NO', 1 => '346623#guhsd.net|TD-Share Test #1001|NO', 2 => '346623#guhsd.net|TD-Share Test #1001|NO', 3 => '346623#guhsd.net|TD-Share Test #1001|NO', 4 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 5 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 6 => '346623#guhsd.net|TD-No Excuse Reflection #1002|YES', 7 => '346623#guhsd.net|TD-About Me #1004|YES', 8 => '346623#guhsd.net|TD-Calendar #1007|YES', 9 => '346623#guhsd.net|TD-Wage Tracker #1008|YES', 10 => '346623#guhsd.net|TD-Stock Portfolio #1009|YES', 11 => '346623#guhsd.net|TD-Collaboration #1005|YES', 12 => '346623#guhsd.net|TD-Stock Portfolio #1009|YES', 13 => '346623#guhsd.net|TD-Collaboration #1005|YES', 14 => '346623#guhsd.net|TD-Dream Vacation Presentation #1015|YES', );
$combinedAssignmentData = [];
foreach($assignmentsYES as $key=>$level){
$combinedAssignmentData[$level] = preg_grep('/#'.$level.'\|/', $studentIDsubmissions);
}
print_r($combinedAssignmentData);
Using Preg Grep is a bit cleaner then array filter and a callback with a regular expression. I also realized you had a superficial loop in there $levels = array($assignedIDs); or basically $levels = array($level); or just $level.
Same output as before
Sandbox
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
I have a little Problem with a 2-dimensional and associative array which i need to merge in PHP.
So what i'm trying to achieve as output is something like this:
Array ( [0] => 6 [1] => 5 [2] => 9 [3] => 8 [4] => 3 [5] => 16 [6] => 55
[7] => 59 [8] => 56 [9] => 3 [10] => 4 .... [1293] => 2)
At the moment my output is as follows:
foreach ($arrayList as $key => $list) {
print_r($list);
}
is
Array ( [hgeneral1] => 6 [hgeneral2] => 5 [hgeneral3] => 9 [hgeneral4] => 8
[hgeneral5] => 3 [hgeneral6] => 16 [hmusic1] => 55 [hmusic2] => 59 [hmusic3] => 56 )
Array ( [hgeneral1] => 3 [hgeneral2] => 4 [hgeneral3] => 8 [hgeneral4] => 10 [hgeneral5]
=> 16 [hgeneral6] => 17 [hsport1] => 26 [hsport2] => 32 [hsport3] => 35 [hsport4] => 38
[hsport5] => 41 [hsport6] => 42 [hmusic1] => 55 [hmusic2] => 56 [hmusic3] => 58
[hmusic4] => 60 [hmusic5] => 61 ) Array ....
and like 50 more arrays.
Now since it's a associative array merge will just overwrite the values (if i understood that right), so my question is: Is there a way to get all these values into one big array?
I would really appreciate any help and sorry for my bad english (and the maybe kinda noobish question, but i'm really new to programming).
Cheers
Jutschge
"If the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended." array_merge doc
So take the values with array_values first and then merge like array_merge(array_values(arr1),array_values(arr2) .. )
This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 10 months ago.
I have 2 arrays
$arr1 = Array
(
[0] => 12
[1] => 4
[2] => 8
[3] => xx
[4] => 1
[5] => 1year
[6] => 7
)
$arr2 = Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
I want to create a new array with the values of a2 as keys in $arr1.
My resultant array should be like this
$arr3 = Array
(
[1] => 12
[2] => 4
[3] => 8
[4] => xx
[5] => 1
[6] => 1year
[7] => 7
)
$arr3 = array_combine($arr2, $arr1);
print_r($arr3);
Next time please consult the manual first.
You need array_combine.
whats about looking into php manual? and find array_combine($keys, $values)