I have a multidimensional array, in which I have a few set of values. What I want to do here is to merge the key values "marks" and "course" if the values of the key "name" match. So far, I've done something like below to remove the duplicates:
$multi = array(
array("name" => "Michael", "marks" => "25, 27, 34", "course" => "ABC"),
array("name" => "Kumar", "marks" => "59, 71, 38", "course" => "DEF"),
array("name" => "Peter", "marks" => "94, 43, 61", "course" => "JKL"),
array("name" => "Kumar", "marks" => "83, 57, 73", "course" => "GHI"),
);
$multiTemp = $multiNew = array();
foreach($multi as $key=>$val){
if(array_key_exists($val['name'], $multiTemp) ) {
continue;
}
$multiTemp[$val['name']] = 1;
$multiNew[] = $val;
}
echo "<pre>";
print_r($multiNew);
echo "</pre>";
It just removes the duplicate values. Is therey any way to merge the other two values based on the condition I mentioned above the code? Just like the second and fourth array in the array $multi carry same values for name, so I want marks and course to be mereged into one. Thanks in advance for your help.
Current Output:
Array
(
[0] => Array
(
[name] => Michael
[marks] => 25, 27, 34
[course] => ABC
)
[1] => Array
(
[name] => Kumar
[marks] => 59, 71, 38
[course] => DEF
)
[2] => Array
(
[name] => Peter
[marks] => 94, 43, 61
[course] => JKL
)
)
Expected Output:
Array
(
[0] => Array
(
[name] => Michael
[marks] => 25, 27, 34
[course] => ABC
)
[1] => Array
(
[name] => Kumar
[marks] => 59, 71, 38, 83, 57, 73
[course] => DEF, GHI
)
[2] => Array
(
[name] => Peter
[marks] => 94, 43, 61
[course] => JKL
)
)
array_reduce() + arrray_values() solution:
$multi = [
["name" => "Michael", "marks" => "25, 27, 34", "course" => "ABC"],
["name" => "Kumar", "marks" => "59, 71, 38", "course" => "DEF"],
["name" => "Peter", "marks" => "94, 43, 61", "course" => "JKL"],
["name" => "Kumar", "marks" => "83, 57, 73", "course" => "GHI"]
];
$result = array_values(array_reduce($multi, function($r, $a){
$name = $a['name'];
if (isset($r[$name])){
$r[$name]['marks'] .= ', ' . $a['marks'];
$r[$name]['course'] .= ', ' . $a['course'];
} else {
$r[$name] = $a;
}
return $r;
}, []));
print_r($result);
The output:
Array
(
[0] => Array
(
[name] => Michael
[marks] => 25, 27, 34
[course] => ABC
)
[1] => Array
(
[name] => Kumar
[marks] => 59, 71, 38, 83, 57, 73
[course] => DEF, GHI
)
[2] => Array
(
[name] => Peter
[marks] => 94, 43, 61
[course] => JKL
)
)
http://php.net/manual/en/function.array-reduce.php
Related
Is it possible to sort (with php) an array like this by last name:
$array = array(
array("name" => "Mary Johnson","age" => 43),
array("name" => "Amanda Miller","age" => 23),
array("name" => "James Brown","age" => 47),
array("name" => "Patricia Williams","age" => 31),
array("name" => "Michael Davis","age" => 15),
array("name" => "Sarah Miller","age" => 35),
array("name" => "Patrick Miller","age" => 44)
);
<?php
// A function to sort by last name.
function lastNameSort($a, $b) {
$aLast = end(explode(' ', $a));
$bLast = end(explode(' ', $b));
return strcasecmp($aLast, $bLast);
}
// The array of data.
$array = array(
array("name" => "Mary Johnson","age" => 43),
array("name" => "Amanda Miller","age" => 23),
array("name" => "James Brown","age" => 47),
array("name" => "Patricia Williams","age" => 31),
array("name" => "Michael Davis","age" => 15),
array("name" => "Sarah Miller","age" => 35),
array("name" => "Patrick Miller","age" => 44)
);
// Perform the sort:
uasort($array, 'lastNameSort');
// Print the result:
print_r($array);
Its just an addressing issue. Amend you function as below, remember the $a and $b are arrays and not scalar variables.
$array = array(
array("name" => "Mary Johnson","age" => 43),
array("name" => "Amanda Miller","age" => 23),
array("name" => "James Brown","age" => 47),
array("name" => "Patricia Williams","age" => 31),
array("name" => "Michael Davis","age" => 15),
array("name" => "Sarah Miller","age" => 35),
array("name" => "Patrick Miller","age" => 44)
);
function sortByName($a, $b) {
$aLast = explode(' ', $a['name'])[1];
$bLast = explode(' ', $b['name'])[1];
return strcasecmp($aLast, $bLast);
}
#print_r($array);
usort($array, 'sortByName');
print_r($array);
RESULT
Array
(
[0] => Array ( [name] => James Brown [age] => 47 )
[1] => Array ( [name] => Michael Davis [age] => 15 )
[2] => Array ( [name] => Mary Johnson [age] => 43 )
[3] => Array ( [name] => Amanda Miller [age] => 23 )
[4] => Array ( [name] => Sarah Miller [age] => 35 )
[5] => Array ( [name] => Patrick Miller [age] => 44 )
[6] => Array ( [name] => Patricia Williams [age] => 31 )
)
And to get the Millers in the correct order by firstname as well as lastname, you code amend the sort function a little as below
function sortByName($a, $b) {
$t = explode(' ', $a['name']);
$aLast = $t[1] . $t[0];
$t = explode(' ', $b['name'])[1];
$bLast = $t[1] . $t[0];
return strcasecmp($aLast, $bLast);
}
I currently have an array as follows:
Array (
[0] => Array ( [id] => 34 [another_id] => 2805 [third_id] => 1 )
[1] => Array ( [id] => 35 [another_id] => 2805 [third_id] => 1 )
[2] => Array ( [id] => 36 [another_id] => 2805 [third_id] => 1 )
[3] => Array ( [id] => 37 [another_id] => 2805 [third_id] => 1 )
[4] => Array ( [id] => 38 [another_id] => 2805 [third_id] => 1 )
[5] => Array ( [id] => 39 [another_id] => 2805 [third_id] => 1 )
[6] => Array ( [id] => 40 [another_id] => 2805 [third_id] => 2 )
[7] => Array ( [id] => 41 [another_id] => 2805 [third_id] => 2 )
[8] => Array ( [id] => 42 [another_id] => 2805 [third_id] => 2 )
[9] => Array ( [id] => 43 [another_id] => 2805 [third_id] => 2 )
)
What I need to do is ultimately print out 9 links ( as there are 9 array elements) but based on the keys in the array. For example:
www.samplelink/link/id/another_id/third_id
But I can't seem to get the loop right. What I have so far is:
foreach ($array as $arr) {
foreach ( $arr as $key => $value ) {
echo "<a>www.samplelink/link/".$key[$value]."</a>";
}
}
But thats not exactly what I need as its printing out the keys as well. Anyone know what I could do?
foreach ($array as $innerArray) {
echo "<a>www.samplelink/link/".$innerArray['id']."/".$innerArray['another_id']."/".$innerArray['third_id']."</a>";
}
It can give an undefined index error if key doesn't exist so you can do something like this as well:
foreach ($array as $innerArray) {
$finalLink = array_key_exists('id',$innerArray)?$innerArray['id']:"";
$finalLink.= "/".array_key_exists('another_id',$innerArray)?$innerArray['another_id']:"";
$finalLink.= "/".array_key_exists('third_id',$innerArray)?$innerArray['third_id']:"";
echo "<a>www.samplelink/link/$finalLink</a>";
}
If elements in subarrays always in same order, you can just implode them:
foreach ($array as $arr) {
echo "<a>www.samplelink/link/".implode('/', $arr)."</a>";
}
Otherwise you should point what index will be in which position explicitly, as in #Danyal Sandeelo's answer.
<?php
$arrays = array(
"0" => array('id' => 30, 'another_id' => 2800, 'third_id' => 1),
"1" => array('id' => 31, 'another_id' => 2801, 'third_id' => 1),
"2" => array('id' => 32, 'another_id' => 2802, 'third_id' => 1),
"3" => array('id' => 33, 'another_id' => 2803, 'third_id' => 1),
"4" => array('id' => 34, 'another_id' => 2804, 'third_id' => 1),
"5" => array('id' => 35, 'another_id' => 2805, 'third_id' => 2),
"6" => array('id' => 36, 'another_id' => 2806, 'third_id' => 3),
"7" => array('id' => 37, 'another_id' => 2807, 'third_id' => 3),
"8" => array('id' => 38, 'another_id' => 2808, 'third_id' => 2),
"9" => array('id' => 39, 'another_id' => 2809, 'third_id' => 2),
);
foreach($arrays as $key => $array) {
echo 'www.samplelink/link/'.$array['id'].'/'.$array['another_id'].'/'.$array['third_id']. "\n";
}
you can play with it here http://sandbox.onlinephpfunctions.com/code/1c7838e25045263de03e23c60b19c86d5640407d
You can do this :
foreach ($array as $arr) {
echo "<a>www.samplelink/link/" . $arr['id'] . '/' . $arr['another_id'] . '/' . $arr['third_id'] . "</a>";
}
or, if your sub-arrays always holds the ids in the proper order and nothing but the relevant ones :
foreach ($array as $arr) {
echo "<a>www.samplelink/link/" . implode('/', $arr) . "</a>";
}
$value will hold the inner array on each iteration.
foreach($array as $value) {
$link = '//www.samplelink/link/'.$value['id'].'/'.$value['another_id'].'/'.$value['third_id']
echo '<a>'.$link.'</a>';
}
I need to know how many arrays have valid keys, how many arrays with valid keys in multidimensional array. Let me explain:
Input:
Array
(
[65] => Array
(
[1] => Array
(
[0] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 525
[trackposition] => 1
[tracklocation] => SIDE A
[tracknumber] => 1
[trackname] => I love u
)
[1] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 526
[trackposition] => 1
[tracklocation] => SIDE A
[tracknumber] => 2
[trackname] => Sun is yellow
)
)
[2] => Array
(
[0] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 527
[trackposition] => 2
[tracklocation] => SIDE B
[tracknumber] => 1
[trackname] => Car red
)
[1] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 528
[trackposition] => 2
[tracklocation] => SIDE B
[tracknumber] => 2
[trackname] => Lady in red
)
)
)
[769] => Array
(
[] => Array
(
[0] => Array
(
[mediumid] => 769
[mediumname] => DVD
[trackid] =>
[trackposition] =>
[tracklocation] =>
[tracknumber] =>
[trackname] =>
)
)
)
)
The mediums[65] next array contains 2 valid keys (1 and 2). The mediums[769] next array contains no valid keys
Therefore only mediums[65] contains valid keys, so total of arrays with valid keys = 1.
I need to find that total. How ?
I've try using array_keys and array_filter, with no success (or either i'm doing it wrong)
PHP code demo
<?php
$array=Array
(
65 => Array
(
1 => Array
(
0 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 525,
"trackposition" => 1,
"tracklocation" => "SIDE A",
"tracknumber" => 1,
"trackname" => "I love u"
),
1 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 526,
"trackposition" => 1,
"tracklocation" => "SIDE A",
"tracknumber" => 2,
"trackname" =>"Sun is yellow"
)
),
2 => Array
(
0 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 527,
"trackposition" => 2,
"tracklocation" => "SIDE B",
"tracknumber" => 1,
"trackname" => "Car red"
),
1 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 528,
"trackposition" => 2,
"tracklocation" => "SIDE B",
"tracknumber" => 2,
"trackname" => "Lady in red"
)
)
),
769 => Array
(
"" => Array
(
0 => Array
(
"mediumid" => 769,
"mediumname" => "DVD",
"trackid" => "",
"trackposition" => "",
"tracklocation" => "",
"tracknumber" =>"",
"trackname" => ""
)
)
)
);
$counter=0;
$trackedNull=false;
foreach($array as $key => $value)
{
$keys=array_keys($array[$key]);
foreach($keys as $arraykey)
{
if($arraykey=="")
{
$trackedNull=true;
break;
}
}
if($trackedNull==true)
{
$trackedNull=false;
}
else
{
$counter++;
}
}
echo $counter;
I've array multidimentional ..
Array (
[123] => Array ( [0] => 120 [1] => 200 [2] => 180 [3] => 130 )
[124] => Array ( [0] => 150 [1] => 155 [2] => 160 [3] => 165 )
[125] => Array ( [0] => 121 [1] => 120 [2] => 121 [3] => 121 )
)
I want to convert like this
120,200,180,130
150,155,160,165
121,120,121,121
how to code this guys ?
my code from stackoverflow too ..
echo join("','", array_map(function ($data) { return $data[0]; }, $data))
but .. the output
120, 150, 121 .. i want to get from 123
This should work for you:
(Here I just go through each innerArray with array_map() and implode() it and print it)
<?php
$arr = [
"123" => [120, 200, 180, 130],
"124" => [150, 155, 160, 165],
"125" => [121, 120, 121, 121]
];
array_map(function($v){
echo implode(",", $v) . "<br />";
}, $arr);
?>
Output:
120,200,180,130
150,155,160,165
121,120,121,121
You can simply iterate over all items in the $arrs and use implode to format every single array:
$arrs = Array (
123 => Array ( 0 => 120, 1 => 200, 2 => 180, 3 => 130 ),
124 => Array ( 0 => 150, 1 => 155, 2 => 160, 3 => 165 ),
125 => Array ( 0 => 121, 1 => 120, 2 => 121, 3 => 121 ),
)
foreach($arrs as $arr) {
echo implode(",",$arr)."\n";
}
"\n" means you append a new line in raw text. In case you want to use HTML for formatting, you should evidently use <br/>:
foreach($arrs as $arr) {
echo implode(",",$arr).'<br/>';
}
$newArr = array();
foreach($yourArr as $key =>$val)
{
$newArr[] = implode(",",$val);
}
foreach($newArr as $arr)
{
echo $arr."<br>";
}
output
120,200,180,130
150,155,160,165
121,120,121,121
It feels I am going way over my head while discovering the ultimate usage of arrays.
I have two arrays, where the first has main keys, and the value is a count of files attached to that key.
The goal is to match the keys of this first array to the values in a second array, but still mainting (and show) the (value)count of Array-1 -- but for only the values in the second array.
Seems somewhat hazy perhaps, but here are the arrays. The second one has the values that should match the keys in the first.
(My problem is that I keep losing the values of array 1 with every attempt I make.)
Hope you can help me out with this one.
(working matches are keys like: 125, 2051 & 2214)
Array 1:
Array (
[6960] => 3
[2214] => 4
[2051] => 4
[6944] => 2
[6938] => 4
[1823] => 1
[766] => 6
[3993] => 4
[5896] => 6
[6927] => 2
[4220] => 3
[77] => 3
[83] => 1
[125] => 2
[6618] => 2
[196] => 1
[4072] => 12
[3718] => 1
[5918] => 1
[3388] => 10
[4500] => 13
[5968] => 2
[3000] => 2
[942] => 1
[4246] => 8
[5868] => 2
[6394] => 3
[1168] => 1
[2163] => 1
[1827] => 2
[2071] => 8
[4597] => 1
[1702] => 7
)
Array 2:
Array (
[0] => 1024
[1] => 1076
[2] => 111
[3] => 124
[4] => 125
[5] => 1301
[6] => 1409
[7] => 2051
[8] => 2214
[9] => 2636
[10] => 3246
[11] => 4838
[12] => 6946
[13] => 6955
[14] => 6961
[15] => 73
[16] => 74
[17] => 8
)
What about doing this:
<?php
$arr1 = array(1 => 1000, 500 => 1111, 1000 => 5000, 5000 => 5555);
$arr2 = array(1, 5000);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
(
[1] => 1000
[5000] => 5555
)
Or, using your data:
<?php
$arr1 = array(6960 => 3, 2214 => 4, 2051 => 4, 6944 => 2, 6938 => 4, 1823 => 1, 766 => 6, 3993 => 4, 5896 => 6, 6927 => 2, 4220 => 3, 77 => 3, 83 => 1, 125 => 2, 6618 => 2, 196 => 1, 4072 => 12, 3718 => 1, 5918 => 1, 3388 => 10, 4500 => 13, 5968 => 2, 3000 => 2, 942 => 1, 4246 => 8, 5868 => 2, 6394 => 3, 1168 => 1, 2163 => 1, 1827 => 2, 2071 => 8, 4597 => 1, 1702 => 7);
$arr2 = array(1024, 1076, 111, 124, 125, 1301, 1409, 2051, 2214, 2636, 3246, 4838, 6946, 6955, 6961, 73, 74, 8);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
Array
(
[2214] => 4
[2051] => 4
[125] => 2
)
array_interset_keys will find the intersection of arrays by keys, not values. Since your second array is an index based array (not an associative array) we need to first flip the keys and values using array_flip. Then the keys can be intersected.
Your question is somewhat unclear, but I think this is what you're looking for:
foreach( $array2 as $key)
{
$count = ( isset( $array1[ $key ]) ? $array1[ $key ] : 0);
echo $key . ' has ' . $count . ' files.';
}
Uhhmm.. i cant seem to understand what you want to imply.. but from the way i see it.. if you want to have the keys of array 1 as value to array 2.. just do this code..
foreach($array1 as $key=>$val) {
$array2[] = $key;
}
This should grab the KEYS of array1 and insert it to your array2[].
Hope this helps you.. Cheers :)
This should print out what you need:
foreach($array2 as $key=>$val) {
echo $val;
foreach($array1 as $key2 => $val2){
if($key == $val2){
echo $val2;
}
}
echo '\n'; // new line
}