How to Display the set of series not from the array? - php

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]);

Related

different output every 3 column

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

How to put all the sub array values in php variables

I'd like to add all the values from each sub arrays below in some distinct php variables.
I can access each value individually but I can't figure out how to put all the values from the first sub array in one variable e.g. $subarray_zero and all the values from the second sub array in another variable e.g. $subarray_one. The idea is then to use those variables to add the values in mysql, so I'll have two columns, one with all the values from the variable $subarray_zero and another column $subarray_one with all the values from the second sub array.
Array
(
[0] => Array
(
[5] => 3.5
[6] => 4.5
[7] => 5.5
)
[1] => Array
(
[8] => 5
[9] => 6
[10] => 7
)
)
Thanks for your help
full code
$period = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
$sma = array(6,9);
foreach ($sma as $range) {
$sum = array_sum(array_slice($period, 0, $range));
$result = array($range - 1 => $sum / $range);
for ($i = $range, $n = count($period); $i != $n; ++$i) {
$result[$i] = $result[$i - 1] + ($period[$i] - $period[$i - $range]) / $range;
}
$array[] = $result;
}
echo '<pre>';
print_r($array);
echo '</pre>';
$smplarr=Array(0 => Array(5 => 3.5,6 => 4.5,7 => 5.5),1 => Array(8 => 5,9 => 6,10 => 7));
list($subarray_zero,$subarray_one)=$smplarr;
print_r($subarray_zero);echo "<br>";
print_r($subarray_one);
see output
Array ( [5] => 3.5 [6] => 4.5 [7] => 5.5 )
Array ( [8] => 5 [9] => 6 [10] => 7 )
Use list Thanks

PHP - Array does not turn into two-dimensional array

I need to make my array better.
I am getting data from database and i have milestones and milestone_parts. i want two-dimensional array. I need data of milestones in the first dimension and milestone_parts in the second dimension.
With this code:
$query = "
SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`,
b.omschrijving AS `milestonefase_omschrijving`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '99'
";
$result= $db->query($dbh, $query);
while ($row = $db->fetchassoc($result))
{
$stone = array($row['milestone_verkocht_id'], $row['milestone_id'], $row['milestone_titel'], $row['client']);
$fase = array($row['milestonefase_milestone_id'],$row['milestonefase_id'],$row['milestonefase_titel']);
$stone[] = $fase;
echo '<pre>'; print_r($stone); echo '</pre>';
}
I get this as result
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 10
[2] => string
)
)
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 11
[2] => string
)
)
but I need (with names) this:
Array
(
[milestone_verkocht_id] => 99 // This is project id
[milestone_id] => 6
[milestone_title] => string
[client] => string
[10] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 10
[milestone_title] => string
)
[11] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 11
[milestone_title] => string
)
[12] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 12
[milestone_title] => string
)
)
Can you help me or do you have a solution? Help me please!
you can cycle each field returned by the query, checking the field name and making new arrays
$stones = array();
while ($row = $db->fetchassoc($result)) {
$fase = array();
$stone = array('milestones' => array());
foreach ($row as $k => $v) {
if (strpos($k, 'milestonefase_') === 0) {
$fase[$k] = $v;
} else {
$stone[$k] = $v;
}
}
if(!isset($stones[$stone['milestone_id']])) {
$stones[$stone['milestone_id']] = $stone;
}
$stones[$stone['milestone_id']]['milestones'][$fase['milestonefase_id']] = $fase;
}
echo '<pre>'.print_r($stones, true).'</pre>';
Edit
made some changes in order to match the request. Now we use $stones to store the information we already have on a milestone, adding to it the different "$fase" returned from the query
Probably a more clean way is to retrieve all the information with two different queries one for milestones and the other for the fases
Edit2
Added a sub-array for the milestone fases

php - rearranging an array into groups

I have an array of filenames:
Array
(
[2] => 1_1_page2-img1.jpg
[3] => 1_2_page2-img1-big.jpg
[4] => 2_1_page2-img1.jpg
[5] => 2_2_page2-img1-big.jpg
[6] => 3_1_page2-img1.jpg
[7] => 4_1_page2-img1.jpg
[8] => 4_2_page2-img1.jpg
[9] => 5_2_page2-img1.jpg
)
I'm trying to rearrange them so they're grouped together by their first number. I'm guessing I could maybe separate them with a pipe so I could then distinguish them afterwards. Either that or a multidimensional array.
I know I can perform an explode("_",$filename); to get the first and second digits before the underscores.
The catch is even though the beginning numbers should always increment, there won't necessarily be 2 files per initial number.
So I'm either trying to make it into the following:
Array
(
[0] => 1_1_page2-img1.jpg|1_2_page2-img1-big.jpg
[1] => 2_1_page2-img1.jpg|2_2_page2-img1-big.jpg
[2] => 3_1_page2-img1.jpg|
[3] => 4_1_page2-img1.jpg|4_2_page2-img1.jpg
[4] => |5_2_page2-img1.jpg
)
Or something a bit tidier perhaps? I just can't work out the foreach to put them together.
Or is there an array related command that will put them together easier?
My preference would be to store them in subarrays, as this will be much easier to deal with in the long run; so this would be a possibility, given your array is in $arr:
$newarr = array ();
while (list($key, $val) = each($arr)) {
$subarray_index = substr($val, 0, strpos($val, "_"));
$newarr[$subarray_index][] = $val;
}
Is this what you mean?
$arr = Array(
2 => '1_1_page2-img1.jpg',
3 => '1_2_page2-img1-big.jpg',
4 => '2_1_page2-img1.jpg',
5 => '2_2_page2-img1-big.jpg',
6 => '3_1_page2-img1.jpg',
7 => '4_1_page2-img1.jpg',
8 => '4_2_page2-img1.jpg',
9 => '5_2_page2-img1.jpg'
);
function orderArray($pArr){
$first = '0';
$newArr = array();
foreach($pArr as $val){
if(strpos($val,$first) !== 0){
if(substr($val,2,1)==='1'){
$newArr[]=$val;
}else{
$newArr[]='|'.$val;
}
$first = substr($val,0,1);
}else{
$curIndex = count($newArr) - 1;
$newArr[$curIndex] = $newArr[$curIndex].'|'.$val;
}
return $newArr;
}
$result = orderArray($arr);
print "number of values: ".count($result)."<br>";
foreach($result as $value){
print $value."<br>";
}
Just worked it out now based on another post in stackoverflow:
foreach ($scanned_directory as $filename){
$ids = explode("_",$filename);
$groups[$ids[0]][] = $filename;
}
echo "<pre>";
ksort($groups);
print_r($groups);
echo "</pre>";
Displays:
Array
(
[1] => Array
(
[0] => 1_1_page2-img1.jpg
[1] => 1_2_page2-img1-big.jpg
)
[2] => Array
(
[0] => 2_1_page2-img1.jpg
[1] => 2_2_page2-img1-big.jpg
)
[3] => Array
(
[0] => 3_1_page2-img1.jpg
[1] => 3_2_page2-img1-big.jpg
)
[10] => Array
(
[0] => 10_1_page2-img1.jpg
)
[11] => Array
(
[0] => 11_2_page2-img1-big.jpg
)
)
There isn't a nice automated way of doing this, but you could use a simple loop:
$array = [];
foreach ($filename as $file) {
$fields = explode('_', $file);
$array[$fields[0]][$fields[1]] = $file;
}
An example is located here.

Filling php array that has missing values

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;
}
}

Categories