I have this foreach loop that outputs the below array, and I'm having a senior moment, I need it to return one array with no duplicate values, and I just can't it right.
foreach ( $post_groups as $post_group => $id ) {
group = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE ID = %d", $id), ARRAY_A);
$groups[$group['group_name']] = $group['group_name'] = unserialize( $group['group_users'] );
}
output:
array(2) {
["Registered Users"]=>
array(1) {
[0]=>
string(1) "2"
}
["Admin Users"]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
}
Cheers
I believe the following is what you're after. Simply merge the arrays together and then ensure the result is unique.
$userIds = [
'Registered Users' => array(1,2,3),
'Admin Users' => array(3,4,5),
];
$allUserIds = array_unique(call_user_func_array('array_merge', $userIds));
var_dump($userIds);
/*
array(2) {
["Registered Users"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
["Admin Users"]=>
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
}
*/
var_dump($allUserIds);
/*
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[4]=>
int(4)
[5]=>
int(5)
}
*/
Use to php functions : array_unique(array_merge())
test code:
$userIds = array('RegisteredUsers' => array('A','a','b','B'),'AdminUsers' => array('C','c','B','d','a'));
$allUserIds = array_unique(array_merge($userIds['RegisteredUsers'], $userIds['AdminUsers']));
echo "<br><br>";
var_dump($userIds);
var_dump($allUserIds);
Related
Basically i want to compare arrays inside a multidimensional array,
So i want to compare everyone with etc category 1 against each other, category 2 against earch other etc etc.
So what i really want to do is:
Compare every category against each other with this formula
versus - won = x
And then find out who has the lowest number and then update that last survivor from each category in the database (The winners)
Any ideas on how i can solve this the best?
$arr;
$stmt = $dbCon->prepare(" SELECT versus, won, imgId, category FROM rating_versus ");
$stmt->execute();
$stmt->bind_result($versus, $won, $imgId, $category);
while ($stmt->fetch()) {
$arr[] = array('category' => $category, 'id' => $imgId, 'versus' => $versus, 'won' => $won);
// echo $imgId . "<br> Versus: " . $versus . "<br> Won: " . $won . "<br> <br>";
}
$stmt->close();
This will output something like this
array(9) {
[0]=> array(4) { ["category"]=> int(1) ["id"]=> int(1) ["versus"]=> int(42) ["won"]=> int(21) }
[1]=> array(4) { ["category"]=> int(1) ["id"]=> int(5) ["versus"]=> int(47) ["won"]=> int(24) }
[2]=> array(4) { ["category"]=> int(1) ["id"]=> int(13) ["versus"]=> int(47) ["won"]=> int(23) }
[3]=> array(4) { ["category"]=> int(2) ["id"]=> int(2) ["versus"]=> int(45) ["won"]=> int(19) }
[4]=> array(4) { ["category"]=> int(2) ["id"]=> int(4) ["versus"]=> int(49) ["won"]=> int(25) }
[5]=> array(4) { ["category"]=> int(2) ["id"]=> int(7) ["versus"]=> int(44) ["won"]=> int(25) }
[6]=> array(4) { ["category"]=> int(3) ["id"]=> int(3) ["versus"]=> int(47) ["won"]=> int(29) }
[7]=> array(4) { ["category"]=> int(3) ["id"]=> int(6) ["versus"]=> int(50) ["won"]=> int(18) }
[8]=> array(4) { ["category"]=> int(3) ["id"]=> int(9) ["versus"]=> int(45) ["won"]=> int(24) }
}
To find the winner, you don't need to eliminate one by one, who has the lowest score.
Winner is who has the highest score.
So your code should be like following
$arr; $stmt = $dbCon->prepare(" SELECT versus, won, imgId, category FROM rating_versus ");
$stmt->execute();
$stmt->bind_result($versus, $won, $imgId, $category);
$winners=array();
while ($stmt->fetch()) {
$arr[] = array('category' => $category, 'id' => $imgId, 'versus' => $versus, 'won' => $won);
if(isset($winners[$category])) {
$curmax = $winners[$category]['won'];
if($won>$curmax) {
$winners[$category] = array('id'=>$imgId, 'won' => $won);
}
} else {
$winners[$category] = array('id'=>$imgId, 'won' => $won);
}
// echo $imgId . "<br> Versus: " . $versus . "<br> Won: " . $won . "<br> <br>";
}
$stmt->close();
print_r($winners);
This should work for this case
So I sorted a multidimensional array with uasort in descending order. I did a var_dump($winrateArray) and it is sorted properly. The highest value is in the first returned array. However when I try a var_dump($winrateArray[0][3]) which is where I expect the highest value to be it isn't there. Instead it is in $winrateArray[1][3]. Am I using uasort properly?
Unsorted dump:
array(2) { [0]=> array(4) { [0]=> string(2) "18" [1]=> string(1) "1" [2]=> int(0) [3]=> int(1) } [1]=> array(4) { [0]=> string(2) "31" [1]=> string(1) "1" [2]=> int(100) [3]=> int(101) } }
Sorted dump:
array(2) { [1]=> array(4) { [0]=> string(2) "31" [1]=> string(1) "1" [2]=> int(100) [3]=> int(101) } [0]=> array(4) { [0]=> string(2) "18" [1]=> string(1) "1" [2]=> int(0) [3]=> int(1) } }
Specific dump:
int(1)
.
$winrateArray[0][0] = '18';
$winrateArray[0][1] = '1';
$winrateArray[0][2] = 0;
$winrateArray[0][3] = 1;
$winrateArray[1][0] = '31';
$winrateArray[1][1] = '1'
$winrateArray[1][2] = 100;
$winrateArray[1][3] = 101;
var_dump($winrateArray);
function cmp($a, $b){
if ($a[3] == $b[3]){
return 0;
}
return ($a[3] < $b[3]) ? 1 : -1;
}
uasort($winrateArray, 'cmp');
var_dump($winrateArray);
var_dump($winrateArray[0][3]);
You can do:
$newArray = $winrateArray;
uasort($winrateArray, 'cmp');
$i = 0;
foreach($winrateArray as $key => $item) {
$newArray[$i][3] = $item[3];
$i++;
}
and use the new array after that. You will preserve keys and you will reorder and replace 3rd elements only
I have this array (i can't change it) :
array(3) {
[0]=>
array(8) {
["kampania"]=> string(6) "dasdas"
[0]=> string(6) "dasdas"
["300x250"]=> int(1)
[1]=> int(1)
["160x600"]=> int(2)
[2]=> int(2)
["728x90"]=> int(3)
[3]=> int(3)
}
[1]=>
array(8) {
["kampania"]=> string(12) "aaaaaaaaaaaa"
[0]=> string(12) "aaaaaaaaaaaa"
["300x250"]=> int(4)
[1]=> int(4)
["160x600"]=> int(5)
[2]=> int(5)
["728x90"]=> int(6)
[3]=> int(6)
}
[2]=>
array(8) {
["kampania"]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
[0]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
["300x250"]=> int(7)
[1]=> int(7)
["160x600"]=> int(8)
[2]=> int(8)
["728x90"]=> int(9)
[3]=> int(9)
}
}
As you can see, I got repeated values, because the array as the defined key and then an integer key.
How can I create a function which will remove the integer key and value from array and return a new array already "clean"
the result expect should be like this:
array(3) {
[0]=>
array(8) {
["kampania"]=> string(6) "dasdas"
["300x250"]=> int(1)
["160x600"]=> int(2)
["728x90"]=> int(3)
}
[1]=>
array(8) {
["kampania"]=> string(12) "aaaaaaaaaaaa"
["300x250"]=> int(4)
["160x600"]=> int(5)
["728x90"]=> int(6)
}
[2]=>
array(8) {
["kampania"]=> string(20) "AAAAAAAAAAAAAAAAAAAA"
["300x250"]=> int(7)
["160x600"]=> int(8)
["728x90"]=> int(9)
}
}
Thanks Guys! I'm really sorry for ask it , but I loose already so much time trying to fix it by myself
function unset_num_keys($array)
{
$array_out = array();
foreach($array AS $k => $v)
{
if(is_array($v)) //value is an array, so clean it
{
$array_out[$k] = unset_num_keys($v); //clean "child" arrays
}
elseif(!is_numeric($k))
{
$array_out[$k] = $v; // key is "safe"
}
}
return $array_out;
}
Then
$clean_array = unset_num_keys($old_array);
Loop threw the array and if the key(index) is numeric then remove that item from the array.
foreach($level1_array as &$arr){
foreach ($arr as $key => $value) {
if (is_int($key)) {
unset($arr[$key]);
}
}
}
Example
<?php
$level1_array = array(
array(
"smith",
"name" => "smith",
"20",
"age"=>20
),
array(
"smith",
"name" => "smith",
"20",
"age"=>20
),
array(
"smith",
"name" => "smith",
"20",
"age"=>20
),
);
foreach($level1_array as &$arr){
foreach ($arr as $key => $value) {
if (is_int($key)) {
unset($arr[$key]);
}
}
}
var_dump($level1_array);
?>
You can try this code:
//$data = $yourData;
$newData = array();
foreach($data as $v){
array_push($newData,array_flip($v));
}
I have my main array:
array(6) {
[1]=> array(3) {
[0]=> string(15) "Extension"
[1]=> int(1)
[2]=> string(6) "3,00 "
}
[2]=> array(3) {
[0]=> string(32) "Physics "
[1]=> string(1) "1"
[2]=> string(6) "3,00 "
}
[3]=> array(3) {
[0]=> string(31) "Physics "
[1]=> int(1)
[2]=> string(6) "6,00 "
}
[4]=> array(3) {
[0]=> string(34) "Desk"
[1]=> int(4)
[2]=> string(8) "127,00 "
}
[5]=> array(3) {
[0]=> string(18) "assistance"
[1]=> int(1)
[2]=> string(7) "12,50 "
}
[6]=> array(3) {
[0]=> string(15) "Extension"
[1]=> int(1)
[2]=> string(6) "3,00 "
}
}
My expected output is:
Extension 2
Physics 2
Desk 1
Assistance 1
The result must be in an resultarray
How can I do? I tried with array_count_values function but don't work.
How can I stock answear:
I tried this code but It doesn't work
$tabrecap = array();
foreach($counts as $key=>$value){
//echo $key." qte".$value;
$tabrecap = array ($key,$value,$valueOption);
}
As you asked in comment,Please try this:-
<?php
$array = array( '1'=> array('0'=>"Extension", '1'=> 1, '2'=>"3,00 " ), '2'=> array('0'=>"Physics",'1'=>"1","3,00 " ),'3'=> array('0'=>"Physics",'1'=>1,"6,00 "),'4'=> array('0'=>"Desk",'1'=>4,"127,00 "),'5'=> array('0'=>"assistance",'1'=>1,"12,50 " ),'6'=> array('0'=>"Extension",'1'=>1,"3,00 "));
$count = array();
$i = 0;
foreach ($array as $key=>$arr) {
// Add to the current group count if it exists
if (isset($count[$i][$arr[0]])) {
$count[$i][$arr[0]]++;
}
else $count[$i][$arr[0]] = 1;
$i++;
}
print_r($count);
?>
Output:- https://eval.in/379176
Looping is the answer.
<?php
// untested
$counts = Array();
foreach( $array as $subArray ){
$value = $subArray[0];
$counts[ $value ] = ( isset($counts[ $value ]) )
? $counts[ $value ] + 1
: 1;
}
var_dump( $counts);
Just make a loop and use first item of each array as key :
$array = array(
array("Extension", 1, "3,00"),
array("Physics", "1", "3,00"),
array("Physics", 1, "6,00 ")
);
$count = array();
foreach($array as $a)
$count[$a[0]]++;
var_dump($count); // array(2) { ["Extension"]=> int(1) ["Physics"]=> int(2) }
Good day.
Code:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
I get array unique elements:
$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
if(!in_array($elms[0], $arr_uniq)) {
$arr_uniq[] = $elms[0];
}
}
Tell me pleasse how to get a count each unique element in the general array?
result should been next:
art_7880 - 3
art_7883 - 1
Assuming $all_array is subarray of your main array in your var_dump snipett, the general idea is
$result = array();
foreach ($all_array as $elms)
$result[$elms[0]]++;
array_count_values()
http://php.net/array_count_values
You should be able to easily apply this function.