Does anybody knows how can I get the max and min value of the 2nd and 3rd columns in PHP?
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
Output should be like:
max(12.9)
min(10)
Another option
<?php
function array_rotate( $array )
{
$rotated = array();
foreach ( $array as $rowIndex => $col )
{
foreach ( $col as $colIndex => $value )
{
$rotated[$colIndex][$rowIndex] = $value;
}
}
return $rotated;
}
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
$ar = array_rotate( $ar );
echo max( $ar[2] ), "\n", min( $ar[1] );
<?php
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
function col($tbl,$col){
$ret = array();
foreach ($tbl as $row){
$ret[count($ret)+1] = $row[$col];
}
return $ret;
}
print (max(col($ar,2))."\n");
print (min(col($ar,1))."\n");
?>
is this what you look for? I guess its not the most efficient way.
Related
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
The indexes of $names and $money correspond to each other.
I need to find the most efficient way to add common $money to each $names and print only the even values.
For example, john appears two times, with 2 and 3 money. So johnhas 5 money.
Desired output:
mark: 20
brian: 8
paul: 6
I am thinking of looping through each array, but this is O(n^2) Is there an easier way?
Make associative array (hash) for result:
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$result = [];
for ($i = 0; $i < count($names); $i++) {
if (!isset($result[$names[$i]])) {
$result[$names[$i]] = 0;
}
$result[$names[$i]] += $money[$i];
}
arsort($result); // reverse (big first) sort by money
print_r($result);
just loop through names and use it keys for money array:
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$res = [];
foreach ($names as $key => $value) {
$res[$value] = isset($res[$value]) ? $res[$value] + $money[$key] : $money[$key];
}
var_dump($res);
You just have to loop through $names once and set name as key in a result- variable.
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$data = [];
foreach ($names as $key => $name) {
if (!isset($data[$name])) {
$data[$name] = 0;
}
$data[$name] += $money[$key];
}
print_r($data);
Maybe you'll need a check, if both arrays have the same size.
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$result = [];
foreach ($names as $index => $name) {
if (empty($result[$name]) {
$result[$name] = $money[$index];
} else {
$result[$name] += $money[$index];
}
}
print_r($result);
I have custom page where there are 10 posts showing right now, what i need to show first 3 random then next 4-7 again random and 8-10 again random
Is their any way i can manage in the while loop
<?php
$count = 1;
while ( $loop->have_posts() ) : $loop->the_post();
echo the_title();
$count++;
endwhile;
?>
Thanks
If I understood you correctly, this should get you close to what you want.
Put your posts into an array first:
$posts = array();
while ( $loop->have_posts() ) {
$loop->the_post();
array_push($posts, $post);
}
Then sort that array. I'll demonstrate with 0-9:
$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$first = array_slice($array, 0, 3);
$second = array_slice($array, 3, 4);
$third = array_slice($array, 7, 3);
shuffle($first);
shuffle($second);
shuffle($third);
$newarray = array_merge($first, $second, $third);
print join(", ", $array) . "\n" . join(", ", $newarray) . "\n";
Which will lead to a random-ish sorting of the array while keeping "blocks" (top, middle, bottom) in the same order:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
1, 2, 0, 6, 5, 4, 3, 8, 7, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 2, 1, 3, 4, 5, 6, 9, 8, 7
Putting it all together:
$posts = array();
while ( $loop->have_posts() ) {
$loop->the_post();
array_push($posts, $post);
}
$first = array_slice($posts, 0, 3);
$second = array_slice($posts, 3, 4);
$third = array_slice($posts, 7, 3);
shuffle($first);
shuffle($second);
shuffle($third);
$newposts = array_merge($first, $second, $third);
foreach($newposts as $mypost) {
print $mypost->post_title . "<br />\n";
}
note that I erroneously had written push $posts, $post; instead of array_push($posts, $post);, I have written a lot of Perl lately, and it shows.
First time posting on stackoverflow.
After printing the main array, I have managed to highlight the values that are found in the second one, but I also want to print the number of times that the duplicate occurs in brackets at the same time. I have run out of the ideas on how to do that last part, I get stuck in multiple loops and other problems. I will paste here what's working for now.
The code:
$main = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20 );
$secondary = array( 1, 6, 10, 6, 17, 6, 17, 20 );
foreach ( $main as $number )
{
if ( in_array( $number, $secondary ) )
{
echo $item;
// this one is supposed to be highlighted, but the html code disappears on stackoverflow
/* this is where the number of duplicates should appear in bracket, example:
highlighted number( number of duplicates ) */
}
else
{
echo $item;
// this one is simple
}
}
EXPECTED RESULT:
1(1), 2, 3, 4, 5, 6(3), 7, 8, 9, 10(1), 11, 12, 13, 14, 15, 16, 17(2), 18, 19, 20(1)
Basically the brackets contain the number of times that the value is found in the second array, aside from being colored, but I can't paste the html code for some reason. Sorry for not making the expected result more clear !
PROBLEM SOLVED:
Thanks to everyone for your help, first time using this website, didn't expect such a quick response from you guys. Thank you very much !
You need to get the count values of your secondary array first using array_count_values. This is what you can acquire as
$main = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
$secondary = array(1, 6, 10, 6, 17, 6, 17, 20);
$count_values = array_count_values($secondary);
foreach ($main as $key => $value) {
if (in_array($value, $secondary)) {
echo $value . "<strong>(" . $count_values[$value] . ")</strong>";
echo ( ++$key == count($main)) ? '' : ',';
} else {
echo $value;
echo ( ++$key == count($main)) ? '' : ',';
}
}
Output:
1(1),2,3,4,5,6(3),7,8,9,10(1),11,12,13,14,15,16,17(2),18,19,20(1)
Assuming $secondary is the one with the dupes, you should go about it the other way:
$dupes = array();
foreach($secondary as $number) {
if (in_array($number, $main)) {
$dupes[$number]++;
}
}
var_dump($dupes);
<?php
$main = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12, 13, 14, 15, 16, 17,18,19,20);
$secondary = array( 1, 6, 10, 6, 17, 6, 17, 20 );
$result =array();
foreach($main as $key => $value){
$i=0;
foreach($secondary as $key1 => $value1){
if($value == $value1){
$i++;
}
$result[$value] = $i;
}
}
$resultString ='';
foreach($result as $key => $value){
$resultString .=$key.'('.$value.'),' ;
}
echo trim($resultString,',');
?>
Result:
1(1),2(0),3(0),4(0),5(0),6(3),7(0),8(0),9(0),10(1),11(0),12(0),13(0),14(0),15(0),16(0),17(2),18(0),19(0),20(1)
I am looking for a function in php,where the function must delete values in the array that shows up three times or more? For example, if you give the function array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0) the funciton will return array(2, 4, 6, 3, 4, 2, 0)
You can use array_count_values() to get frequencies. Then use a foreach to get values that has frequency less than 3...
$array = array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0);
$frq = array_count_values($array);
$result = array();
foreach ($frq as $key=>$value){
if ($value < 3){
$result[] = $key;
}
}
function FilterMyArray(array &$array){
$array_count=array_count_values($array);
foreach($array_count as $key => $value){
if($value > 2){
foreach (array_keys($array, $key, true) as $unsetKey) {
unset($array[$unsetKey]);
}
}
}
}
$array=array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
FilterMyArray($array);
print_r($array);
Output
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 2 [7] => 3 [8] => 1 [9] => 9 )
`
This will remove all duplicates, but you'd have to add to it to count the number of each of the values.
$a = array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0);
$b = array();
for ($a as $key=>$value) {
if (!in_array($value, $b)) {
$b[] = $value;
}
}
// array $b has all the values with no duplicates.
I am trying to put this code in a more flexible manner so it can work whatever the size of $sets array is. I suppose it can be done with recursion but cannot find the correct php syntax.
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
$combinations = array();
foreach($sets[0] as $s1)
foreach($sets[1] as $s2)
foreach($sets[2] as $s3)
foreach($sets[3] as $s4)
$combinations[] = array($s1, $s2, $s3, $s4);
print_r($combinations);
You can do it in recursion like this. The output is identical to your loops
<?php
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
function get_combinations($sets, &$combinations = array(), &$row = array()) {
if (count($sets) == 0) {
$combinations[] = $row;
return $combinations;
}
foreach ($sets[0] as $s) {
$row[] = $s;
get_combinations(array_slice($sets, 1), $combinations, $row);
array_pop($row);
}
return $combinations;
}
$combinations = get_combinations($sets);
print_r($combinations);