Hello i just finish a code where i get like 50 variables... all of them with int values..
I have the variables as separete values, just for this example I will set the variables with the result, BUT the result came from other evaluations and stuff that its fine, cause im already echoing a verified result.
$one = 13
$two = 35
$three = 46
The "item1" appears <?PHP echo $one; ?> times<br />
The "item2" appears <?PHP echo $two; ?> times<br />
The "item3" appears <?PHP echo $three; ?> times<br />
This is fine but,, How can i order the results, in ASC way or DSC , to build a order by...
Thanks so much
This far this is working great
$naturales = array(
$uno => "n1",
$dos => "n2",
$tres => "n3",
$cuatro => "n4",
$cinco => "n5",
$seis => "n6",
$siete => "n7",
$ocho => "n8",
$nueve => "n9",
$diez => "n10",
$once => "n11",
$doce => "n12",
$trece => "n13",
$catorce => "n14",
$quince => "n15",
$dieciseis => "n16",
$diecisiete => "n17",
$dieciocho => "n18",
$diecinueve => "n19",
$veinte => "n20",
$veintiuno => "n21",
$veintidos => "n22",
$veintitres => "n23",
$veinticuatro => "n24",
$veinticinco => "n25",
$veintiseis => "n26",
$veintisiete => "n27",
$veintiocho => "n28",
$veintinueve => "n29",
$treinta => "n30",
$treintayuno => "n31",
$treintaydos => "n32",
$treintaytres => "n33",
$treintaycuatro => "n34",
$treintaycinco => "n35",
$treintayseis => "n36",
$treintaysiete => "n37",
$treintayocho => "n38",
$treintaynueve => "n39",
$cuarenta => "n40",
$cuarentayuno => "n41",
$cuarentaydos => "n42",
$cuarentaytres => "n43",
$cuarentaycuatro => "n44",
$cuarentaycinco => "n45",
$cuarentayseis => "n46",
$cuarentaysiete => "n47",
$cuarentayocho => "n48",
$cuarentaynueve => "n49",
$cincuenta => "n50",
$cincuentayuno => "n51",
$cincuentaydos => "n52",
$cincuentaytres => "n53",
$cincuentaycuatro => "n54",
$cincuentaycinco => "n55",
$cincuentayseis => "n56",
);
krsort($naturales);
foreach ($naturales as $count => $name) {
echo "The \"$name\" appears $count times<br />";
}
Why my results are like this (Its hidding all the results with 12 (Similar count results)
for example for "n3" appears 12 times. and its not listed.
The "n20" appears 12 times
The "n30" appears 11 times
The "n37" appears 10 times
The "n41" appears 9 times
The "n42" appears 8 times
The "n45" appears 7 times
The "n47" appears 6 times
The "n35" appears 5 times
The "n44" appears 4 times
The "n46" appears 2 times
The "n56" appears 0 times
Build an array
$myresults = array("Item1"=>13,"item2"=>35,"item3"=>46);
then use asort() or arsort() on the array $myresults
then do a for/foreach loop to output the results
basic guidelines but off this you should be able to google how to implement in detail fairly easily (even on here will work)
$one = 13;
$two = 35;
$three = 46;
$arr = array("Item 1"=>$one,"Item 2"=>$two,"Item 3"=>$three);
echo "<strong>Original</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
asort($arr);
echo "<strong>Ascending Sort</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
arsort($arr);
echo "<strong>Descending Sort</strong><br />";
foreach($arr as $k => $v){
echo $k . " = " . $v . "<br />";
}
As previously mentioned, you can use asort and arsort to sort your array as needed... I'm adding some examples here as well as some working CODE
As mentioned, you could insert your values into an associative array, i.e.:
$items = array(
$one => "item1",
$two => "item2",
$three => "item3"
);
and then you can use a function like ksort() to sort all of your values:
http://php.net/manual/en/function.ksort.php
so you can end up with something like this:
ksort($items);
foreach ($items as $count => $name) {
echo "The \"$name\" appears $count times<br />";
}
Related
I need to random a single element from the array. I have code ;
if (isset($_POST['losuj'])) {
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/2.jpg',
'mienso3' => 'skiny/2.jpg',
'mienso4' => 'skiny/2.jpg',
'mienso5' => 'skiny/2.jpg',
'Hasasdasd' => 'skiny/2.jpg',
];
foreach($arr as $key => $value) {
$keys = array_rand( $arr, 1);
echo $keys;
}
}
And its didnt working. Any tips ?
You can use array_keys to get the keys in a indexed array.
The just use array_rand just like you did to pick one and echo the $arr associative key.
$keys = array_keys($arr);
$random = $keys[array_rand($keys,1)];
Echo $random . " => " . $arr[$random];
https://3v4l.org/miacb
Just use array_rand($arr,1) without foreach loop
With PHP we can use the function array_rand()
More information can be found at:
http://php.net/manual/en/function.array-rand.php
https://www.w3schools.com/php/func_array_rand.asp
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/2.jpg',
'mienso3' => 'skiny/2.jpg',
'mienso4' => 'skiny/2.jpg',
'mienso5' => 'skiny/2.jpg',
'Hasasdasd' => 'skiny/2.jpg',
];
$randomEntry = array_rand($arr, 1);
use this without the loop
$key = array_rand($arr);
echo $arr[$key];
full example
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/3.jpg',
'mienso3' => 'skiny/4.jpg',
'mienso4' => 'skiny/5.jpg',
'mienso5' => 'skiny/6.jpg',
'Hasasdasd' => 'skiny/7.jpg',
];
$key = array_rand($arr);
echo $key;
echo $arr[$key];
You should print array result like below,
$rand_keys = array_rand($arr, 1);
echo $arr[$rand_keys[0]] . "\n";
Are the '.jpg' files meant to all be the same except for one? Because randomly choosing between 7 files when 6 are the same is going to return the same file more than often.
$rand_keys = array_rand($arr);
echo $arr[$rand_keys];
<?php
$input = array(
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/3.jpg',
'mienso3' => 'skiny/4.jpg',
'mienso4' => 'skiny/5.jpg',
'mienso5' => 'skiny/6.jpg',
'Hasasdasd' => 'skiny/7.jpg',
);
foreach($input as $key => $value) {
$keys = array_rand( $input, 1);
echo $input[$keys];
}
?>
I'm trying to get sum and average of visitors from the following multi-dimensional array :
Array([visitors] => Array(
[2015-06-12] => Array([0] => Array([value] => 29))
[2015-06-11] => Array([0] => Array([value] => 55))
...
))
I cannot manage to find a way to get the results i need as i get lost with "foreach".
Can anybody help please ?
Use this
<?php
$mainarray = array('visitors' => Array(
'2015-06-12' => Array(Array('value' => 29)),
'2015-06-11' => Array(Array('value' => 55))));
$sum = 0;
$count = 0;
$visitor = $mainarray['visitors'];
foreach ($visitor as $key => $val) {
$sum += $val[0]['value'];
$count++;
}
echo "Sum is " . $sum."<br>";
$average = ($sum / $count);
echo "Average is " .$average."<br>";;
?>
I have an array that looks like this:
$rowarray(
[0] => [PID] => 97162 [TID] => 340 [StatsID] => 49678
[1] => [PID] => 97165 [TID] => 340 [StatsID] => 49673
[2] => [PID] => 97167 [TID] => 340 [StatsID] => 49675
[3] => [PID] => 97162 [TID] => 340 [StatsID] => 49679
)
Then my code looks like this:
$cntr=0;
foreach($rowarray as $row)
{
echo "<tr><td>$row[PID] $row[TID] $row[StatsID] </td></tr>";
$cntr++;
}
Two things I want to do I want to be able not print the duplicates in the array but print the additional column that has a different value. So my desired output would look like this.
97162 340 49678 49679
97165 340 49673
97167 340 49675
I started out with the array_unique() but that only returned:
97162 340 49678
Assuming only the StatsID changes (not clear from the question)
$map = array();
foreach($rowarray as $row){
$k = $row["PID"] . '-' . $row["TID"];
if( !isset( $map[$k] ) ){
$map[$k] = array();
}
array_push( $map[$k], $row["StatsId"] );
}
foreach($map as $k=>$v){
$row = explode( '-', $k );
echo "<tr><td>$row[0] $row[1] " . implode( " ", $v ) . " </td></tr>";
}
Here's what I'd do:
Start by sorting the array (using usort to sort by PID, then by TID)
Initialize "last" variables ($last_PID and $last_TID). They will store the respective values in the loop
In the loop, first compare the "current" variables to the "last" ones, if they're the same then just echo the StatsID value.
If they're not the same, output the <tr> (but not the final </tr>, so the first part of the loop can add more StatsID values if necessary)
Still inside the loop, after outputting everything, update the "last" variables.
After the loop, output the final </tr>
This may not be optimal, but I'm pretty sure it'll work.
Transfer the $rowarray structure into a map of maps of arrays, like this:
$rowarray = array(
array('PID' => 97162, 'TID' => 340, 'StatsID' => 49678),
array('PID' => 97165, 'TID' => 340, 'StatsID' => 49673),
array('PID' => 97167, 'TID' => 340, 'StatsID' => 49675),
array('PID' => 97162, 'TID' => 340, 'StatsID' => 49679)
);
$keys = array();
foreach ($rowarray as $row) {
if (!is_array(#$keys[$row['TID']])) {
$keys[$rowarray['TID']] = array();
}
if (!is_array(#$keys[$row['TID']][$row['PID']])) {
$keys[$row['TID']][$row['PID']] = array();
}
$keys[$row['TID']][$row['PID']][] = $row['StatsID'];
}
foreach ($keys as $pid => $pid_arr) {
foreach ($pid_arr as $tid => $tid_arr) {
echo "<tr><td>$tid $pid " . implode(' ', $tid_arr) . "</td></tr>";
}
}
See this code in action
As far as I can tell, the only way to do this would be to loop through the array creating a new unique array as you go.
$unique = array();
foreach ($row as $value)
{
$key = $value['PID'];
if (isset($unique[$key]))
{
$unique[$key]['StatsID'] .= ' ' . $value['StatsID'];
}
else
{
$unique[$key] = $value;
}
}
Now, $unique would give you the results you're looking for and you can loop through the unique array and output your results (I also added your counter if needed):
$count = count($unique);
foreach ($unique as $row)
{
echo "<tr><td>{$row['PID']} {$row['TID']} {$row['StatsID']} </td></tr>";
}
Hi my question is a little tricky, I got a 3-dimensional array and try to verify the 3rd level value and echo both 1st and 3rd level values.
The following is the code example, and my failed approaches.
$myArray=array(
"mySub0" => arrary(
0 => array("mySubSub0" => "1","mySubSub1" => "a"),
1 => array("mySubSub0" => "2","mySubSub1" => "b"),
2 => array("mySubSub0" => "3","mySubSub1" => "b"),
),
"mySub1" => arrary(
0 => array("mySubSub0" => "4","mySubSub1" => "a"),
1 => array("mySubSub0" => "5","mySubSub1" => "a"),
2 => array("mySubSub0" => "6","mySubSub1" => "a"),
),
"mySub2" => arrary(
0 => array("mySubSub0" => "7","mySubSub1" => "a"),
1 => array("mySubSub0" => "8","mySubSub1" => "b"),
2 => array("mySubSub0" => "9","mySubSub1" => "a"),
),
),
I want to check if the value of "mySubSub1" is b. if yes, echo the value of "mySubSub0" and the related key in first-level of the array. It should be like this:
mySub0
2
3
mySub2
8
My failed approach is
foreach ($myArray as $a => $b)
{
foreach ($b as $c)
if($c[mySubSub1]=="b")
{
echo $a
echo $c[mySubSub0];
}
else {
}
}
The result will have one duplicate mySub0
mySub0
2
mySub0
3
mySub2
8
if I move the "echo $a" out of the "if"
foreach ($myArray as $a => $b)
{
echo $a
foreach ($b as $c)
if($c[mySubSub1]=="b")
{
echo $c[mySubSub0];
}
else {
}
}
the result would be
mySub0
2
3
mySub1
mySub2
8
one unwanted "mySub1" because there is no place to verify if there is a value b.
It has bothered my a lot today. I tried to Google but haven't found the right answer.
Really hope someone can help me. Thank you in advance
Here's something that should work:
function find_value($arr, $key, $value, $sibling)
{
foreach ($arr as $k => $v)
{
$results = _find_value($v, $key, $value, $sibling);
if (count($results) > 0) {
echo $k;
foreach ($results as $result) {
echo $result;
}
}
}
}
function _find_value($arr, $key, $value, $sibling)
{
$out = array();
foreach ($arr as $k => $v)
{
if ($v[$key] == $value)
$out[] = $v[$sibling];
}
return $out;
}
find_value($myArray, "mySubSub1", "b", "mySubSub0");
Basically, the main function loops over items in the outer array, calling the inner function to get a list of the "sibling" keys where the main key matches the value you're looking for. If a non-zero number of results are obtained in the inner function, echo and loop.
Hopefully this does the trick for you.
I've got an array that uses UNIX timestamps for array keys. The array will typically hold data for anywhere from 15 minutes to maybe an hours worth of time, however there are only entries for seconds that have data.
Most of the data will be spread out, there will be occasional spans of data for consecutive seconds though. What I'd like to do, is retrieve the first and last second of the longest consecutive span of seconds in the array.
If I have this array
Array
(
[1276033307] => 119.0
[1276033331] => 281.8
[1276033425] => 28.2
[1276033431] => 88.2
[1276033432] => 196.2
[1276034207] => 205.5
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
[1276034230] => 79.8
)
I would either need the keys 1276034226 and 1276034228, or the following array returned.
Array
(
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
)
EDIT:
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$finalArray = array();
foreach($array as $k => $v){
$tempArrays = array();
$index = 0;
while(isset($array[$k + $index])){
$tempArrays[$k+$index] = $array[$k+$index++];
}
if(count($tempArrays) > count($finalArray))
$finalArray = $tempArrays;
}
print_r($finalArray);
the output is the same...
ORIGINAL:
Note: Only the first occurrence of the longest span will be recorded.
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$longspan = 0;
foreach($array as $k => $v){
$index = 1;
while(isset($array[$k+$index])){
$index++;
}
$curspan = --$index;
if($curspan > $longspan){
$longspan = $curspan;
$start = $k;
}
}
for($i=0; $i <= $longspan; $i++)
$results[$start + $i] = $array[$start + $i];
print_r($results);
Outputs:
Array (
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
)
This code does it in a single loop (i.e. is a Greedy algorithm):
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$long_arr = array();
$curr_arr = array();
$last_key = -1;
foreach($array as $k => $v) {
if ($k != $last_key + 1) {
$curr_arr = array();
}
$curr_arr[$k] = $v;
if (count($curr_arr) > count($long_arr)) {
$long_arr = $curr_arr;
}
$last_key = $k;
}
print_r($long_arr);