Implode php array [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array like this:
$Array = [
[1, 33, 55, 18],
[8, 9, 12, 67],
[3, 33, 76, 88],
];
And I want add ; to output like this:
1, 33, 55, 18;
8, 9, 12, 67;
3, 33, 76, 88
Anyone know how to do this?

Use array map to achieve this
$temp = array_map(function($item){
return implode(", ", $item);
}, $Array);
foreach($temp as $v){
// implode with ';' and \n for line break
// you can use "<br/>" if you are using web.
echo $v.";\n";
}
Demo.
One looper:
foreach($Array as $v){
echo implode(", ",$v).";\n";
}
Demo.
Output
1, 33, 55, 18;
8, 9, 12, 67;
3, 33, 76, 88;

Related

how to get same values if an array have values same with values of anther array values in PHP?

Having two arrays of authRoom and partiRoom with one same value inside them. Want to find that same value if it was matched
Found array_search function that work only with single variable
$authRoom = [8, 7, 1, 22, 13, 18, 10];
$partiRoom= [3, 6, 5, 9, 8];
I want the output to be 8 which is the same value of these two arrays
You can use array_intersect which will give you an array of the same values in both $authRoom and $partiRoom like so:
$authRoom = [8, 7, 1, 22, 13, 18, 10];
$partiRoom = [3, 6, 5, 9, 8];
$res = array_intersect($authRoom, $partiRoom);
print_r($res); // [8]
If you want to get the value 8 outside of the array, you can simply access the first value using index 0:
$res = array_intersect($authRoom, $partiRoom)[0];
echo $res; // 8

how can i arrange array like this [duplicate]

This question already has answers here:
Move elements using array_chunk with PHP
(4 answers)
Closed 3 years ago.
I have array like below:
$matrix = array(1, 2, 1,1, 2, 1,1, 1, 1);
how can I get array like below?
//OUTPUT:
minesweeper(matrix) = [[1, 2, 1],
[2, 1, 1],
[1, 1, 1]]
You can use array_chunk
$matrix = array(1, 2, 1,1, 2, 1,1, 1, 1);
$res = array_chunk($matrix,3);
print_r($res);
Live working example

How to make a table like this with fpdf using PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do you make a table like this with FPDF using PHP?
I can't seem to figure out how to do this with $this->Cell.
OK .. let's try. Just an example .. an idea. ok ?
$pdf->SetFillColor(255,0,0);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128,0,0);
$pdf->SetLineWidth(.3);
// Title row
$pdf->SetFont('', 'B');
$pdf->Cell(100, 8, "LEARNING POTENTIAL", 1, 0, 'L', true);
$pdf->SetFont('', '');
$pdf->Cell(20, 8, "Score", 1, 0, 'C', true);
$pdf->Cell(20, 8, "Results", 1, 0, 'C', true);
$pdf->>Ln();
// Data rows
// (loop) foreach($data as $row) {
$pdf->SetFont('', 'B');
$pdf->Cell(100, 8, "Reasoning", "LTR", 0, 'L');
$pdf->Cell(20, 8, "", "LTR");
$pdf->Cell(20, 8, "", "LTR");
$pdf->>Ln();
$pdf->SetFont('', 'B');
$pdf->Cell(100, 8, "The ability to interpret information and", "LR", 0, 'L');
$pdf->SetFont('', '');
$pdf->Cell(20, 8, "6", "LR");
$pdf->Cell(20, 8, "Effective", "LR");
$pdf->>Ln();
$pdf->SetFont('', 'B');
$pdf->Cell(100, 8, "drawing accurate conclusions", "LBR", 0, 'L');
$pdf->Cell(20, 8, "", "LBR");
$pdf->Cell(20, 8, "", "LBR");
$pdf->>Ln();
// (end loop)
I divided each data row in 3 rows.

How to add the value 0 at the beginning of each multidimensional array [duplicate]

This question already has answers here:
Adding an element to the beginning of an array without changing other array keys [duplicate]
(9 answers)
Closed 8 years ago.
I have 52 weeks array's and each week array has a sub array with 9 values.
now I need to add a value 0 at the begin of each array and every next week I need 1 value more.
For example (notice that the 0-8 will be in a for loop)
$vruchtzettings_week["week1"][0-8] = 1, 2, 3, 4, 5, 6, 7, 8, 9
$vruchtzettings_week["week2"][0-8] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
$vruchtzettings_week["week3"][0-8] = 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
$vruchtzettings_week["week4"][0-8] = 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Use array_unshift(), like this
$week=[1,2,3,4,5,6,7,8,9];
array_unshift($week,0); //[0,1,2,3,4,5,6,7,8,9]

sort multidimensional array by values in PHP

I have an array fetched from mysql database tables of type. I want to sort it in order of particular value.
$arr1=array(array(12, 8, 5, 34),
array(54, 87, 32, 10),
array(23, 76, 98, 13),
array(53, 16, 24, 19));
How can i sort it by value?
Like sorting by 2nd value should result to.
$arr1=array(array(12, 8, 5, 34),
array(53, 16, 24, 19),
array(23, 76, 98, 13),
array(54, 87, 32, 10));
I like to use usort to solve these problems.
$sortKey = 1;
usort($arr1, function($a, $b) use($sortKey){
return $a[$sortKey] - $b[$sortKey];
});
Got to agree with #RocketHazmat, array_multsort is a royal pain in the backside. usort is much easier to follow but I thought I'd have a go anyway:
$sortKey = 1;
array_multisort(array_map(function($v) use($sortKey){
return $v[$sortKey];
}, $arr1), $arr1);
It only took 20 minutes... :(
Here's a demo: http://ideone.com/2rZYIz

Categories