What i am attempting to do is something like this
[Col-3][Col-3][Col-3][Col-3]
[Col-4][Col-4][Col-4]
[Col-3][Col-3][Col-3][Col-3]
I am continuing this with a foreach statement, but without luck at the moment.
Currently i'm just doing something like this.
$post = new mm_post();
$rows = $post->getAllPosts();
$i = 1;
foreach ($rows as $key => $row) {
if ($i % 4 == 0) {
echo "[Col-3] <br>";
} else {
echo "[Col-4] <br>";
}
$i++;
}
I am looking for a solution and explanation for this :)
Obviously atm it just make every 3 post [col-3] but i want it to make 4 col-3 then 3 col-4 is what i am looking for.
So I hope you guys can help me! :)
Think about every 7 posts makes 2 lines...one with 4 cols and one with 3 cols:
so when the $i counter is divisible by 7 you print a < br>. Then when line element is less then 4 print col-3,when is 4 print col-3 and < br> otherwise print col-4
$post = new mm_post();
$rows = $post->getAllPosts();
$i = 1;
$line=0;
for($rows as $key => $row)
{
if($i-($line*7)<4)
{
echo "[col-3]";
}
else if($i-($line*7)==4)
{
echo "[col-3]<br>";
}
else
{
echo "[col-4]";
}
if($i%7==0)
{
echo "<br>";
$line++;
}
$i++;
}
function alternate_slices($arr) {
$slices = array();
$start = 0;
$length = 4;
while ($start < count($arr)) {
$slices[] = array_splice($arr, $start, $length);
$length = (4 - $length + 3);
}
return $slices;
}
$arr = range(1, 20);
$sliced_arr = alternate_slices($arr);
<!-- print_r($sliced_arr);
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
[2] => Array
(
[0] => 8
[1] => 9
[2] => 10
[3] => 11
)
[3] => Array
(
[0] => 12
[1] => 13
[2] => 14
)
[4] => Array
(
[0] => 15
[1] => 16
[2] => 17
[3] => 18
)
[5] => Array
(
[0] => 19
[1] => 20
)
) -->
$sliced_arr = array_map(
function ($slice) {
return implode("||", $slice);
},
$sliced_arr
);
<!-- print_r($sliced_arr);
Array
(
[0] => 1||2||3||4
[1] => 5||6||7
[2] => 8||9||10||11
[3] => 12||13||14
[4] => 15||16||17||18
[5] => 19||20
) -->
$sliced_arr = implode("\n", $sliced_arr);
<!-- print($sliced_arr);
1||2||3||4
5||6||7
8||9||10||11
12||13||14
15||16||17||18
19||20 -->
Read about:
array_splice
array_map
implode
Related
I have about 5.000 record, each record contain an array like this
Array
(
[0] => 96
[1] => 40
[2] => 86
[3] => 17
[4] => 18
[5] => 23
)
Array
(
[0] => 01
[1] => 21
[2] => 96
[3] => 33
[4] => 44
[5] => 02
)
I want to get how many time element appear consecutively and into array like this
array (
[96]=>2, // number 96 appear 2 time consecutively
[40]=>1,
[86]=>1,
[17]=>1,
[18]=>1,
....
)
Any idea for this problem ???
To count the max number of times any given element appears consecutively, assuming your 5,000 arrays are in an array called $arr:
$count = array();
foreach ($arr as $k => $ar) {
foreach (array_unique($ar) as $a) {
$tmp = 1;
if (isset($count[$a])) {
$tmp = $count[$a];
}
$count[$a] = 1;
if ($k > 0) {
$i = 1;
while ($i <= $k && in_array($a, $arr[$k - $i])) {
$count[$a]++;
$i++;
}
}
if ($count[$a] < $tmp) $count[$a] = $tmp;
}
}
See demo
i have the following array:
Array
(
[0] => 3
[1] => 6
[2] => 3
[3] => 4
[4] => 5
[5] => 7
[6] => 6
[7] => 7
)
we have to split the array like (order should be 3,4,5,6,7)
Output should be
Array 1: 3,4,5,6,7 (3 is taken from [0],4 is taken from [3],5 is taken from [4],6 is taken from [6],7 is taken from [7])
Array 2: 3,-,-,-,- (3 is taken from [2] nd position)
Array 3: -,-,-,6,7 (6 is taken from [1] nd position,7 is taken from [5] nd position)
Basically what youre asking is this...
$array = array(3,6,3,4,5,7,6,7);
$array1 = array($array[0],$array[3],$array[4],$array[6],$array[7]);
$array2 = array($array[2],"-","-","-","-");
$array3 = array("-","-","-",$array[1],$array[5]);
$array = array(3,6,3,4,5,7,6,7);
sort($array);
print_r($array);
seems you are trying to sort by array associative values
hope this would be helpful.
try using loop
$arr = array (3,6,3,4,5,7,6,7);
$snewarr =array();
$tnewarr =array();
$i =0;
foreach($arr as $k=>$v) {
if($k=='0' || $k=='3' || $k=='4' || $k=='6' || $k=='7') {
$fnewarr[] = $v;
}
if($k=='2') {
$snewarr[] = $v;
}
if(sizeof($snewarr) > 0 && $i<=5){
array_push($snewarr, '-');
}
if($i<=5){
if($i<=3){
array_push($tnewarr, '-');
}
if($k=='1' || $k=='5') {
$tnewarr[] = $v;
}
}
$i++;
}
$tnewarr[4] = $tnewarr[2];
$tnewarr[2] = '-';
echo '<pre>';
print_r($fnewarr); //Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 )
print_r($snewarr); //Array ( [0] => 3 [1] => - [2] => - [3] => - [4] => - )
print_r($tnewarr); //Array ( [0] => - [1] => - [2] => - [3] => - [4] => 6 [5] => 7 )
or in simple way manually set keys directly like :-
$fnewarr= array($arr[0],$arr[3],$arr[4],$arr[6],$arr[7]);
$snewarr= array($arr[2],'-','-','-','-');
$tnewarr= array('-','-','-',$arr[1],$arr[5]);
This is how $myArray looks like:
Array
(
[0] => Array
(
[month] => 1
[atual] => 0.00
)
[1] => Array
(
[month] => 2
[atual] => 11970.99
)
[2] => Array
(
[month] => 3
[atual] => 2888.00
)
[3] => Array
(
[month] => 5
[atual] => 1500.00
)
)
I want to "fill the gaps" of the months. That is, for those months, where we have no data (4,6,8,9,10,11,12), I want the [atual] to be zero.
I tried:
$novo=array();
for ($i=1; $i <=12 ; $i++) {
$mes=$myArray[$i-1]['month'];
$atual=$myArray[$i-1]['atual'];
if(!$mes){
$novo[$i]=0;
} else{
$novo[$i]=$atual;
}
};
But this is returning:
Array
(
[1] => 0.00
[2] => 11970.99
[3] => 2888.00
[4] => 1500.00
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
[edit] now i see you have another problem, your $myArray indexes aren't matching the months.
$myArray(
array('month' => 1, 'atual' => 0.00),
array('month' => 2, 'atual' => 11970.99),
array('month' => 3, 'atual' => 2888.00),
array('month' => 5, 'atual' => 1500.00)
)
for($i = 1; $i <= 12; $i++){
$novo[$i] = 0;
}
foreach($myArray as $item){
$novo[$item['month']] = $item['atual'];
}
print_r($novo);
This worked:
$novo=array_fill(1,12,0);
for ($i=1; $i <=12 ; $i++) {
$mes=$myArray[$i-1]['month'];
$atual=$myArray[$i-1]['atual'];
$novo[$mes]=$atual;
};
With this code you get the month 1 in position 1 (not in position 0);
Also you only search in the array one time.
It's not a beautiful solution but...
$my_array = array(
array('month'=>3,'actual'=>100)
);
$results =array();
for($i=1;$i<13;$i++){
$results[$i] = 0;
}
foreach($my_array as $a){
$results[$a['month']] = $a['actual'];
}
print_r($results);
PHP has several functions that deal with sorting arrays, and here is a comparison of array's sorting functions
I didn't fully understand your question in the first response. This code should work for you. First we will create a temporary array just to hold the month and the data in an accessible format. Then we create your array :
$temp=array();
// Populate the temp array
foreach ($myArray as $row) {
if (is_array($row) && isset($row["month"])) {
$temp[$row["month"]] = $row["atual"];
}
}
// Create novo array
for ($i=0; $i <12 ; $i++) {
$novo[$i]["month"] = $i+1;
if (array_key_exists($i+1, $temp)) {
$novo[$i]['atual'] = $temp[$i+1];
} else {
$novo[$i]['atual'] = 0;
}
}
At first glance, I had thought this would be pretty simple, just construct an sql query by imploding the arrays, but i just cant wrap my mind around how to get to that point.
Basically i have a program that breaks down user input into a way that is needed for my table, and now i need to get that data into an SQL command, you can see how i am attempting to do it, needless to say im at my wits end.
HTML:
<form action="go.php" method="post">
<h1>Enter port data to view how your table will look</h1>
<label for="ports">Number of Ports:</label><input type="text" id="ports" name="ports" size="30"><br>
<label for="super">Super Columns:</label><input type="text" id="super" name="super" size="30"><br>
<label for="columns">Columns:</label><input type="text" id="columns" name="columns" size="30"><br>
<label for="rows">Rows:</label><input type="text" id="rows" name="rows" size="30"><br>
PHP:
//post vars
$port = (int)$_POST['ports'];
$super = (int)$_POST['super'];
$col = (int)$_POST['columns'];
$row = (int)$_POST['rows'];
//rest of vars
$halfPort=$port/2;//12
$colHalf = $col / $super;//6
$half=$colHalf-1;//5
$i=1;
$count=1;
$and=1;
$one=1;
$s=1;
echo "<h1>here goes nothen</h1><br><br>";
//sperate port
$finalArray = array();
for ($i = $port; $i >= 1; $i -= $colHalf) {
$tempArray = array();
for ($j = 0; $j < $colHalf; $j++) {
$tempArray[] = $i - $j;
}
$tempArray[]= sort($tempArray);
$finalArray[] = implode(",", $tempArray);
}
$finalArray = array_reverse($finalArray);
echo "<pre>" . print_r($finalArray, true) . "</pre>";
echo "<br><br>";
//sql for insert
$sqlinsert='
INSERT INTO '.$tbl_name2.'
(PortNumber) VALUES ';
$start='(';
$end=')';
$c=1;
$b=0;
//while to finish SQL
$queryStart=array();
$queryEnd=array();
while($c<$half){
echo "<br>c:".$c."<br>";
$queryStart[]=explode(',',$finalArray[$b]);
echo "<pre>" . print_r($queryStart, true) . "</pre>";
$queryEnd[]=
$c+=$and;
$b+=$and;
unset($queryStart);
}
This is the ouptut of my first print:
Array
(
[0] => 1,2,3,4,5,6,1
[1] => 7,8,9,10,11,12,1
[2] => 13,14,15,16,17,18,1
[3] => 19,20,21,22,23,24,1
)
And the second:
c:1
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 1
)
)
c:2
Array
(
[0] => Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 10
[4] => 11
[5] => 12
[6] => 1
)
)
c:3
Array
(
[0] => Array
(
[0] => 13
[1] => 14
[2] => 15
[3] => 16
[4] => 17
[5] => 18
[6] => 1
)
)
c:4
Array
(
[0] => Array
(
[0] => 19
[1] => 20
[2] => 21
[3] => 22
[4] => 23
[5] => 24
[6] => 1
)
)
I feel like I am very close, just cant bring myself to slap on the last little bit i suppose. Any ideas?
Also as a side note, ignore that trailing 1 at the end of the arrays, it seems to have come from running sort()
I've a series of arrays with values that goes from 1 to 5. Almost every array has missing values, some even dont have any values. My objective is to fill the missing values with 0. All those arrays are stored into a multidimensional array.
My array looks like:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 5
)
[3] => Array
(
[0] => (this array has no values)
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
etc...
)
How it should be:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 0
)
[2] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0
[4] => 5
)
[3] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
etc...
)
Any help would be appriciated!
For each of your subarrays loop through the numbers 1 to 5, and if that value exists set its key to be one less than its value:
$newarray = array();
foreach($arr as $key => $subarr) {
for ($i = 1; $i <= 5; $i++) {
if (in_array($i, $subarr)) $newarray[$key][$i - 1] = $i;
else $newarray[$key][$i - 1] = 0;
}
}
Where $newarray is your output and $arr is your input array.
You may want to note that PHP does not truly do multidimensional arrays. It only allows you to relate 2 flat arrays together which is not true multidimensionality.
This does not work and will produce results described above.
$menu[1] = "My Training"; //not $menu[1][0]
$menu[1][1] = "To Do List";
$menu[1][2] = "Catalog";
$menu[1][3] = "Self-Report";
$menu[1][4] = "Completions";
$menu[2] = "Manager";
$menu[2][1] = "Direct Reports";
$menu[2][2] = "Incompletes";
$menu[2][3] = "Completions";
$menu[3] = "Instructor";
$menu[3][1] = "My Classes";
$menu[3][2] = "Printables";
$menu[3][3] = "Qualifications";
This does work.
$menu[1] = "My Training"; //not $menu[1][0]
$submenu[1][1] = "To Do List";
$submenu[1][2] = "Catalog";
$submenu[1][3] = "Self-Report";
$submenu[1][4] = "Completions";
$menu[2] = "Manager";
$submenu[2][1] = "Direct Reports";
$submenu[2][2] = "Incompletes";
$submenu[2][3] = "Completions";
$menu[3] = "Instructor";
$submenu[3][1] = "My Classes";
$submenu[3][2] = "Printables";
$submenu[3][3] = "Qualifications";
$submenu is only related to $menu through the first key number as there are no first dimension values to $submenu.
Something like this (array_pad() won't do the trick). $myArray is your source array. Completed array is returned in $result:
$result = array();
foreach( $myArray as $subKey=>$subArray ) {
for( $i=0; $i<5; $i++ ) {
if( isset( $subArray[$i] )) {
$result[$subKey][$i] = $subArray[$i];
} else {
$result[$subKey][$i] = 0;
}
}
}
Note, we do copy of the array. You cannot fill array in-place.
It's been many years since I wrote any PHP but something like this might do the trick I guess?
for($i = 0; $i < 5; $i++)
{
if(empty($myArray[$i])
{
$myArray[$i] = 0;
}
}