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
If this has been answered before I apologize (I haven't seen to found it). I have a game pool website that wishes to sort by points (pts). In the below example I want 'Team B' to be first as their points are higher then Team A's at 22 compared to 12. While the solution on this page (How to sort an array of associative arrays by value of a given key in PHP?) seemed to be very similar to what I was looking for I wasn't able to get it to work.
Array
(
[0] => Team Object
(
[id] => 5
[name] => Team A
[games_played] => 13
[wins] => 6
[losses] => 7
[ot_losses] => 0
[pts] => 12
[goals_for] => 7.5
[goals_against] => 22
[streak] => 6-7
)
[1] => Team Object
(
[id] => 2
[name] => Team B
[games_played] => 13
[wins] => 11
[losses] => 2
[ot_losses] => 0
[pts] => 22
[goals_for] => 51
[goals_against] => 19
[streak] => 11-2
)
I suspect I'm on the right path with the following but missing something...
$new_array = array();
foreach ($array_objects as $key => $row)
{
$new_array[$key] = $row['pts'];
}
array_multisort($price, SORT_DESC, $array_objects);
There is probably a really simple way to do this but i can't work it out.
Array ( [0] => Array ( [] => US [1] => U.S. [2] => 21 [3] => 34 [4] => 33 [5] => 35 [6] => 39 [7] => 50 [8] => 61 ) [1] => Array ( [] => 79 [1] => 45 [2] => 84 [3] => 89 [4] => 59 [5] => 64 [6] => 34 [7] => 58 [8] => 55 ) [2] => Array ( [] => 63 [1] => 105 [2] => 68 [3] => 62 [4] => 64 [5] => 67 [6] => CL [7] => Chile [8] => 56 ) [3] => Array ( [] => 40 [1] => 40 [2] => 63 [3] => 37 [4] => 57 [5] => 64 [6] => 59 [7] => 53 [8] => 68 ) [4] => Array ( [] => 70 [1] => 66 [2] => 88 [3] => 48 [4] => 76 [5] => 83 [6] => 80 [7] => 53 [8] => 45 ) [5] => Array ( [] => 44 [1] => 51 [2] => 52 [3] => [4] => [5] => [6] => [7] => [8] => ) )
This is my array. There will all ways be the same number of object (9) in each however the number currently at 6 may increase.
I need to get the 1st item in each so for the 1st one i need (US) I'm stuck as if i put
echo $array[0][1];
Then I get U.S. however i need the first item (US) so i tried both
echo $array[0][0];
echo $array[0][];
Neither return a value. What am i doing wrong?
Using an “empty” string as key in an associative array is possible in PHP.
It is distinctively different from any other, non-empty string key values - so it fulfills the most basic requirement you have for such a key (and the PHP guys didn’t seem to see any need to explicitly prevent this.)
$arr = [
'foo' => 'bar',
'' => 'baz',
];
print_r would show this as
Array
(
[foo] => bar
[] => baz
)
and var_dump’ed it would look like this,
array(2) {
["foo"]=>
string(3) "bar"
[""]=>
string(3) "baz"
}
The latter makes it more obvious that the key is in fact an empty string, and therefor $arr[""] resp. $arr[''] can be used to access this element.
One might consider this one of PHP’s peculiarities - it does work, but it is hardly ever used in practice, because it just does not make the most sense for most use cases.
The other answers offer good explanation about the empty key that I won't reiterate, but a simple way to get the first item in each of the sub-arrays is to map the reset function which we mentioned in the comments over your main array.
$firsts = array_map('reset', $your_array);
This should work regardless of what the key is.
If I were you, I would begin by asking myself why my array has an empty key, my best guess is that you set your subarrays with keys instead of letting it being indexed incrementally by writing something like this :
array(
'' => 'US', // Maybe a '0' was intended to be there, and it's a type.
'1' => 'U.S.',
'2' => '21',
'3' => '34',
'4' => '33',
// etc...
);
In that case, you may benefit from fixing your code, or at least updating it so that your array is confortable to use, for example by removing the keys so that they are replaced with successive indexes.
Anyway, if you want to use that current array, do this :
echo $array[0][''];
Or iterate through it :
foreach ($array as $sub) {
echo $sub[''],'<br>';
}
If you don't have control over how the array is set, you can also reindex it using array_values(). That function takes an array as an argument and returns its values with successive indexes instead of its original keys :
foreach ($array as $key => $sub) {
$array[$key] = array_values($sub);
}
That code should give you the same array than before with the exception that the empty keys are replaced with 0.
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
i want to extract only duplicates out of all keys of an associative array..
The array structure is:
Array
(
[bank_users] => Array
(
[0] => 8
[1] => 8
[2] => 8
[3] => 28
)
[bank_link] => Array
(
[0] => 8
[1] => 8
[2] => 8
[3] => 28
[4] => 28
[5] => 28
[6] => 28
[7] => 28
[8] => 73
[9] => 73
[10] => 73
)
[banks] => Array
(
[0] => 8
[1] => 28
)
)
Now out of this array, i want a function that should check in each of the key and give me the duplicates.. Like as per the above example 8 and 28 should come out as a result as these values are available in all three keys.. bank_users, bank_link, banks.
Pls help...
You need array_intersect to get the common elements in each array and array_unique to reduce them to just one each.
Try
$result = array_unique(
array_intersect($arr['bank_users'], $arr['bank_link'], $arr['banks'])
);
array_unique — Removes duplicate values from an array