Indent value from array in php like a table - php

I've an array like:
$array = array(
0 => "A",
1 => "B",
2 => "C",
3 => "D",
4 => "E",
5 => "F",
6 => "G",
7 => "H",
);
Max lenght $array can be is 9, so max is index = 8 and min is 0 (at least 1 element inside array).
I've to indent this list inside a TCPDF box with limited height where, with some test, i've seen can support max 3 lines. But this box is large so, when the array lenght is bigger than 3, the others element need to be aligned in the side of the first column created like:
A D G
B E H
C F
I can't use X,Y coordinated cause i'm using writeHTML method in TCPDF.
I need to create 3 strings like:
$line1 = "A D G";
$line2 = "B E H";
$line3 = "C F";
What's the best method to create this 3 variables from my array?
UPDATE: using suggest method array_chunk is not the solution for me cause for my purpose I'd like to receive an array like:
Array ( [0] => Array (
[0] => A
[1] => D
[2] => G
)
[1] => Array (
[0] => B
[1] => E
[2] => H
)
[2] => Array (
[0] => C
[1] => F )
)

I think a for loop can solve OP's problem. This is another possible solution that use PHP array functions:
<?php
$array = array(
0 => "A",
1 => "B",
2 => "C",
3 => "D",
4 => "E",
5 => "F",
6 => "G",
7 => "H",
);
$cols = 3;
$array = array_chunk($array, $cols);
$results = array_map(function($index) use ($array) {
return array_column($array, $index);
}, range(0, $cols - 1));
var_dump($results);
View online here: https://eval.in/945787

<?php
$array = array(
0 => "A",
1 => "B",
2 => "C",
3 => "D",
4 => "E",
5 => "F",
6 => "G",
7 => "H",
);
$array_chunk = array_chunk($array, 3,false);
$line = '';
foreach ($array_chunk as $chunk_key => $chunk_value) {
$line = '"';
foreach ($chunk_value as $key => $value) {
$line.=$value." ";
}
$line = "$" . "line" . ($chunk_key+1) . " = " . rtrim($line, " ") . '"' . "<br/>";
echo $line;
$line='';
}
Demo

Related

Compare Two Arrays based on key value

I'm working on a Quiz engine and am comparing answers.
I have two arrays
correct answers :
0 => "a"
1 => "a"
2 => "a"
3 => "c"
Chosen Answers...
0 => "c"
1 => "b"
2 => "a"
3 => "b"
So based on this, I know (from comparing myself) that I have 1 correct answer.
Is there a PHP function that can compare the keys and the values and increment a number of similar?
I've looked at array_intersect and array_difference but they don't seem to give me the desired answer.
Thanks
Short solution using array_intersect_uassoc function (on extended input arrays):
$correct = ["a", "a", "a", "c", "a", "c"];
$chosen = ["c", "b", "a", "b", "a", "b"];
$result = array_intersect_uassoc($correct, $chosen, 'strnatcmp');
print_r($result);
The output:
Array
(
[2] => a
[4] => a
)
I would write a function that would generate an array for each key and if the answer was correct (1, or 0 if wrong) that way you can not only quickly calculate the score but use the array to display the questionnaire result later. Like showing which questions were right and which ones were wrong.
<?php
$corrects = array(
0 => "a",
1 => "a",
2 => "a",
3 => "c"
);
$answers = array(
0 => "a",
1 => "a",
2 => "a",
3 => "c"
);
function verify($answers, $corrects) {
$results = array();
foreach($corrects as $question => $correct) {
$results[$question] = $correct == $answers[$question] ? 1 : 0;
}
return $results;
}
$results = verify($answers, $corrects);
$score = array_sum($results);
?>
It leaves room for more complex scoring or multi correct answer question etc.

Filtering a php array using binary

A have a php array
$arr = array(
1 => "a",
2 => "b",
4 => "c",
8 => "d",
16 => "e",
32 => "f"
);
and a binary number
$filter=101101
I want to filter the array and keep only the keys where the respective value on binary is 1
For this example I would have:
$arr = array(
1 => "a",
4 => "c",
8 => "d",
32 => "f"
);
Or for
$filter=110001
to get
$arr = array(
1 => "a",
2 => "b",
32 => "f"
);
Assuming that the length of $filter is always the same as the number of array elements:
$filter_arr = str_split($filter);
$new_arr = array();
$i = 0;
foreach ($arr as $key => $val) {
if ($filter_arr[$i] == 1) {
$new_arr[$key] = $val;
}
$i++;
}
Using your given array, and filter equal to 101101, $new_arr will equal:
Array
(
[1] => a
[4] => c
[8] => d
[32] => f
)
See demo
This should work for you:
<?php
$arr = array(
1 => "a",
2 => "b",
4 => "c",
8 => "d",
16 => "e",
32 => "f"
);
$filter=110001;
$filerValue = str_split($filter);
$count = 0;
foreach($arr as $k => $v) {
if($filerValue[$count] == 0)
unset($arr[$k]);
$count++;
}
var_dump($arr);
?>
Output:
array(3) {
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[32]=>
string(1) "f"
}

PHP split array based on value duplicated [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is an example array I want to split:
(1,2,3,4,5,0,6,7,0,8,9,10,11,12,0)
How do I split it in 3 arrays like this?
(1,2,3,4,5,0) (6,7,0) (8,9,10,11,12,0)
Here's the solution. (Author said he wants to split at '0' instead of duplicate values)
function split_array_by_duplicate_values($a, $duplicate = 0)
{
$c = array_intersect($a, array($duplicate));
$r = array();
$i = 0;
foreach($c as $k => $v) {
$l = ++$k - $i;
$r[] = array_slice($a, $i, $l);
$i = $k;
}
return $r;
}
$a = array(1,2,3,4,5,0,6,7,0,8,9,10,11,12,0);
print_r(split_array_by_duplicate_values($a));
returns
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 0
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 0
)
[2] => Array
(
[0] => 8
[1] => 9
[2] => 10
[3] => 11
[4] => 12
[5] => 0
)
)
Try this:
$array = array(1, 2, 3, 0, 4, 5, 6);
$array_0 = array_splice($array, 0, 4);
$array_1 = array_splice($array, 4);
I think what you're looking for is array_splice.
$array=array(1,2,3,0,4,5,6);
$newarray=array_splice($array,4);
print_r($array); //Array ( [0] => 1 [1] => 2 [2] => 3 [3]=>0);
print_r($newarray); //Array( [0]=>4 [1]=>5 [2]=>6)
If you want to split an array into two arrays in one multidimensional array, you could do
$array=array(1,2,3,0,4,5,6);
$newarray=array_chunk($array,4); //returns [[1,2,3,0],[4,5,6]]
You should use array split to split the array as you would like
<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>

Array showing series without duplicate and after last array begins from first in php?

I am using:
$input = array("1", "2", "3", "4", "5");
Now I want to start by printing the first value from the array (1) and another value for the remaining ones (2,3,4,5). Then next time, the print value will be 2 and further values (3,4,5,1). And so on, until the print value is 5 and the other values will be (1,2,3,4).
I am using a for-loop, but after the value 5, the loop should break...
What to do for this case....
out put should be: 1 the first time, then 2 then 3 then 4 then 5 then again 1 then continue on every refresh
Does this do what you are looking for?
<?php
$input = array("1", "2", "3", "4", "5");
$arr = array();
foreach ($input as $item) {
$i = array_shift($input);
$arr[] = $input;
$input[] = $i;
}
print_r($arr);
OUTPUT
Array
(
[0] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
[1] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 1
)
[2] => Array
(
[0] => 4
[1] => 5
[2] => 1
[3] => 2
)
[3] => Array
(
[0] => 5
[1] => 1
[2] => 2
[3] => 3
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
You want this:
<?php
session_start();
if(!isset($_SESSION['loop_num'])){
$_SESSION['loop_num'] = 1;
}
$loop = true;
echo "Current: {$_SESSION['loop_num']} <br>";
$i = $_SESSION['loop_num'];
while($loop === true) {
if($i >= 5){
$i = 1;
} else {
$i++;
}
if($_SESSION['loop_num'] == $i){
$loop = false;
break;
}
$others[] = $i;
}
if($_SESSION['loop_num'] >= 5){
$_SESSION['loop_num'] = 1;
} else {
$_SESSION['loop_num']++;
}
print_r($others);
?>
Output:
Current: 4
Array ( [0] => 5 [1] => 1 [2] => 2 [3] => 3 )
Current: 5
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
etc..
try implementing this solution:
$size = count($input);
$input = array_merge($input,$input);
for($i=0;$i<$size;$i++){
$output = array_slice($input,$i,$size-1);
print_r($output);
}
If you just want a constantly rotating array that loops forever, this works:
$array = range(1,5);
foreach($array as &$number) {
echo $number . PHP_EOL;
array_push($array, array_shift($array));
}
If you want it to rotate per page load, setting the front number to one variable the rest in line in a dedicated array, this works:
session_start();
if(!$_SESSION['loop_array']) {
$_SESSION['loop_array'] = range(1,5);
}
$current_value = array_shift($_SESSION['loop_array']);
$others_values = $_SESSION['loop_array'];
// Push current value to back of rotation, leaving next in line for
// next page load.
array_push($current_value, $_SESSION['loop_array']);
This will also work with the following arrays (or any array):
$_SESSION['cute'] = array("dog", "cat", "pony", "bunny", "moose");
$_SESSION['ordinals'] = array("first", "second", "third", "fourth", "fifth");
$_SESSION['tick_tock_clock'] = array("I", "II", "III", "IV", "V", "VI",
"VII", "VIII", "IX", "X", "XI", "XII");
$_SESSION['randomness'] = array('butter', 'pillow', 'Alabama', 'bleeding gums');
<?php
$input = array("1", "2", "3", "4", "5");
while(!empty($input)) {
foreach($input as $i) {
var_dump($i);
}
$input = array_splice($input, 0, count($input) - 1);
}
?>

How to convert string to array?

I need to convert string
"name1", "b", "2", "name2", "c", "3", "name3", "b", "2", ....
to an array like
$arr[0]['name'] = "name1";
$arr[0]['char'] = "b";
$arr[0]['qnt'] = "2";
$arr[1]['name'] = "name2";
$arr[1]['char'] = "c";
$arr[1]['qnt'] = "3";
$arr[2]['name'] = "name3";
$arr[2]['char'] = "b";
$arr[2]['qnt'] = "2";
I used explode to extract an string to array but it not work
Any idea?
$input = '"name1", "b", "2", "name2", "c", "3", "name3", "b", "2"';
$input = str_replace('"', '', $input);
$input = explode(', ', $input);
$output = array();
$i = 0;
while ($i < count($input)) {
$output[] = array(
'name' => $input[$i++],
'char' => $input[$i++],
'qnt' => $input[$i++]
);
}
print_r($output);
Output:
Array
(
[0] => Array
(
[name] => name1
[char] => b
[qnt] => 2
)
[1] => Array
(
[name] => name2
[char] => c
[qnt] => 3
)
[2] => Array
(
[name] => name3
[char] => b
[qnt] => 2
)
)
If you do not care about the array keys being numeric, you can do:
$string = 'name1, b, 2, name2, c, 3, name3, b, 2';
print_r( array_chunk( explode(',', $string), 3 ) );

Categories