I have the next probleme, i want to filter a multidimensional and multi level array with for the uniqe one.
An example:
Array
(
[Home] => Array
(
[Kids] => Array
(
[For sleeping] => Array
(
[0] => Sleeping Bags
[1] => mattress
[2] => mattress
[3] => mattress
[4] => Beds
[5] => Beds
[6] => Beds
[..]
The befored array i want to make it with the uniqe values.
I don't know if this is the fastest / shortest answer, but the code below might work for you:
#Function to make a multidimensional array unique
function makeUnique(&$array)
{
foreach($array as $key => &$value)
{
if(is_array($value))
{
makeUnique($value);
$value = array_unique($value);
}
}
return $array;
}
#Example of your array
$exampleArray = Array(
'Home' => Array(
'Kids' => Array(
'For sleeping' => Array(
0 => 'Sleeping Bags',
1 =>'mattress',
2 =>'mattress')
)
)
);
#Make the array unique and print the results
makeUnique($exampleArray);
print_r($exampleArray);
I have resolved this problem. The solution for this problem is:
$test = Array
(
[Home] => Array
(
[Kids] => Array
(
[For sleeping] => Array
(
[0] => Sleeping Bags
[1] => mattress
[2] => mattress
[3] => mattress
[4] => Beds
[5] => Beds
[6] => Beds
[..]
foreach ($test as $key=>$value){
foreach ($value as $key2 => $value2) {
foreach ($value2 as $key3=>$value3) {
$cat[$key][$key2][$key3]= array_unique($value3);
}
}
}
Related
I have an two arrays in php, what i would like to do is if drug_ids match, I want to add the second array as a sub array.
What I have
Array
(
[0] => Array
(
[drug_id] => 1
[drug] => Abacavir 300mg - Tabs
)
[1] => Array
(
[drug_id] => 4
[drug] => Abacavir/Lamivudine 120/60mg - FDC Tabs
)
[2] => Array
(
[drug_id] => 3
[drug] => Abacavir/Lamivudine 600/300mg - FDC Tabs
)
);
The second array with more data
Array
(
[0] => Array
(
[id] => 2
[decision_date] => 2018-08-10
[discussion] => The product is at 2.6 MOS
[recommendation] => recommendation
[drug_id] => 1
[created] => 2018-08-16 09:23:09
[user] => System Admin
)
[1] => Array
(
[id] => 3
[decision_date] => 2018-08-10
[discussion] => recommendation.
[recommendation] =>recommendation.
[drug_id] => 4
[created] => 2018-08-16 09:23:09
[user] => System Admin
)
[2] => Array
(
[id] => 4
[decision_date] => 2018-08-10
[discussion] => The product is at 6MOS.
[recommendation] =>ggfgfg.
[drug_id] => 4
[created] => 2018-08-16 09:23:09
[user] => System Admin
)
);
I would like to push the second array into the first one as decisions if drug ids match to create a final array
[0] => Array
(
[drug_id] => 4
[drug] => Abacavir/Lamivudine 120/60mg - FDC Tabs
[decisions] => Array(
[0] => Array
(
[id] => 3
[decision_date] => 2018-08-10
[discussion] => recommendation.
[recommendation] =>recommendation.
[drug_id] => 4
[created] => 2018-08-16 09:23:09
[user] => System Admin
)
[1] => Array
(
[id] => 4
[decision_date] => 2018-08-10
[discussion] => The product is at 6MOS.
[recommendation] =>ggfgfg.
[drug_id] => 4
[created] => 2018-08-16 09:23:09
[user] => System Admin
)
)
)
Any suggestions as to how this can be achieved?
What i have tried
$table_data //first array
$table_data //second array
foreach ($table_data as $mt) {
foreach ($items as $it) {
if ($it['drug_id'] == $mt['drug_id']) {
$decision['decisions'] = $it;
array_push($table_data, $decision);
}
}
}
//This does not work
There's a bug. Try the following
foreach ($table_data as &$mt) {
foreach ($items as $it) {
if ($it['drug_id'] == $mt['drug_id']) {
if (!isset($mt['decisions'])) {
$mt['decisions'] = [];
}
$mt['decisions'][] = $it;
}
}
}
Another way:
foreach ($arr1 as &$a) {
$a["decisions"] = array_filter($arr2, function ($b) use ($a) {
return $b["drug_id"] === $a["drug_id"];
});
}
I do not recommended nested loops they will do too many unnecessary iterations. Instead, because the drug_ids in the first array are unique, copy them to be the first level's keys -- this will let you swiftly associate data between the two arrays. When the loop is done, you can remove the first level keys with array_values().
Code: (Demo)
$drugs = array_column($array1, null, 'drug_id');
foreach ($array2 as $row) {
if (isset($drugs[$row['drug_id']])) {
$drugs[$row['drug_id']]['decisions'][] = $row;
}
}
var_export(array_values($drugs));
I want to perform an intersection of two arrays that have different structures, but both have one key common (fid). I want a new (filtered second) array after intersection with first array. below is my code and two arrays :
first array:
Array
(
[0] => Array
(
[fid] => 1
)
[1] => Array
(
[fid] => 3
)
)
Second array:
Array
(
[0] => Array
(
[fid] => 9
[functionality] => testing
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[2] => Array
(
[fid] => 2
[functionality] => view functionality category
[funcat_id] => 1
[name] => functionality
)
[3] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
[4] => Array
(
[fid] => 4
[functionality] => edit functionality
[funcat_id] => 1
[name] => functionality
)
)
I want this Output :
Array
(
[0] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
)
I tried this code but I'm not getting the right answer:
$result=array_intersect($array1,$array2);
//Or this also
$result=recursive_array_intersect_key($array1,$array2);
Please let me know, if any one can do this ?
I do not know if a function does exists to do this outright, but alternatively, you can just loop them instead:
$result = array();
foreach($array2 as $val2) {
foreach ($array1 as $val1) {
if($val2['fid'] == $val1['fid']) {
$result[] = $val2;
}
}
}
echo '<pre>';
print_r($result);
Sample Output
Or if you're using PHP 5.5 or greater:
$val1 = array_column($array1, 'fid');
$result = array_filter($array2, function($val2) use($val1) {
return in_array($val2['fid'], $val1);
});
foreach($array2 as $val)
{
$i=0;
foreach($array1 as $val1)
{
if($val['fid']==$val1['fid'])
{
$i++;
}
}
if($i!=0)
{
$a[]=$val;
}
}
print_r($a);
I have a multidimentional array like this $membergroups :
Array ( [0] =>
Array ( [id] => 1645819602
[name] => Oryza NurFa
[first_name] => Oryza
[last_name] => NurFa
[work] => MAN 2 Yogyakarta )
[1] =>
Array ( [id] => 100000251643877
[name] => Lathif Pambudi
[first_name] => Muhammad Lathif
[last_name] => Pambudi
[work] => Omah TI )
[2] =>
Array ( [id] => 1152078197
[name] => Novantio Bangun
[first_name] => Novantio
[last_name] => Bangun
[work] => Pertamina))
How to delete one of child array with specific value. For the example, I want to delete an array with [id] => 100000251643877 inside? So the output will be :
Array ( [0] =>
Array ( [id] => 1645819602
[name] => Oryza NurFa
[first_name] => Oryza
[last_name] => NurFa
[work] => MAN 2 Yogyakarta )
[1] =>
Array ( [id] => 1152078197
[name] => Novantio Bangun
[first_name] => Novantio
[last_name] => Bangun
[work] => Pertamina))
Here is my php code, but it doesn't work :
if (($key = array_search($user_fbid, $membergroups)) !== false) {
unset($membergroups[$key]);
}
Any help would be greatly appreciated. Thank you
You can make use of array_column but only for php >= 5.5
if (($key = array_search($user_fbid, array_column( $membergroups, 'id') ) !== false) {
unset($membergroups[$key]);
}
array_column( $membergroups, 'id') search in membergroup multidimensional array for id column, and return tou you an array containing all rows values entries with id key.
array_column -> MANUAL
Using a foreach you can do the job like this
$id = 100000251643877;//Example
foreach($membergroups as $key => $value){
if($value['id'] == $id){
unset($membergroups[$key]);
}
}
Loop through the whole array:
foreach ($membergroups as $idx => $group) {
if ($group['id'] === $user_fbid) {
unset($membergrouops[$idx]);
break;
}
}
foreach($membergroups as $key => $value){
if($value['id'] == $user_fbid){
unset($membergroups[$key]);
}
}
Don't forget to merge the array after removing key to keep the index sequantially
$membergroups = array_merge($membergroups);
how can i count an element if it appears more than once in the same array?
I already tried with array_count_values, but it did not work, is it beacuse i got more than one key and value in my array?
This is my output from my array (restlist)
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla)
[1] => Array ( [restaurant_id] => 32144 [title] => test5)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
[3] => Array ( [restaurant_id] => 32144 [title] => test5)
[4] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
)
I want it to count how many times the same element appears in my array and then add the counted value to my newly created 'key' called hits in the same array.
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla [hits] => 1)
[1] => Array ( [restaurant_id] => 32144 [title] => test5 [hits] => 2)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 [hits] => 2)
)
This is how i tried to do what i wanted.
foreach ($cooltransactions as $key)
{
$tempArrayOverRestaurants[]= $key['restaurant_id'];
}
$wordsRestaruants = array_count_values($tempArrayOverRestaurants);
arsort($wordsRestaruants);
foreach ($wordsRestaruants as $key1 => $value1)
{
$temprestaurantswithhits[] = array(
'restaurant_id' => $key1,
'hits' => $value1);
}
foreach ($restlistas $key)
{
foreach ($temprestaurantswithhits as $key1)
{
if($key['restaurant_id'] === $key1['restaurant_id'])
{
$nyspisestedsliste[] = array(
'restaurant_id' => $key['restaurant_id'],
'title' => $key['title'],
'hits' => $key1['hits']);
}
}
}
I know this is probably a noob way to do what i want but i am still new at php..I hope you can help
Just try with associative array:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $item ) {
$id = $item['restaurant_id'];
if ( !isset($output[$id]) ) {
$output[$id] = $item;
$output[$id]['hits'] = 1;
} else {
$output[$id]['hits']++;
}
}
And if you want to reset keys, do:
$outputWithoutKeys = array_values($output);
I have two-dimensional array and I want replace second of two-dimensional array on random number of second array
Array
(
[1] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 500
[4] => 600
[5] => 700
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 6
)
)
I want get
Array (
[1] => 5 (<- random from first array)
[2] => 6 (<- random from second array)
)
I tried to do:
foreach($variables as $key => $val) {
$variables = str_replace($val, $val[array_rand($val)], $variables);
}
Why it doesnt work?
foreach($variables as $key => $val) {
$variables[$key] = $val[array_rand($val)];
}
foreach ($variables as &$var) {
$var = array_rand($variables[$var]);
}