i have one php array
and i want to draw output of this array in something like deciding type.
Here is my php code
<?php
$data = array('A','B','C','D','E','F');
$count = count($data);
for($k = 0;$k<$count;$k++){
foreach($data as $key => $value){
if($key == $k){
$datanew = $count - $k;
for($i=0 ; $i<$datanew ; $i++){
echo "X";
}
}else{
echo "V";
}
}
echo "</br>";
}
?>
current output
XXXXXXVVVVV
VXXXXXVVVV
VVXXXXVVV
VVVXXXVV
VVVVXXV
VVVVVX
excepted output
XXXXXX
VXXXXX
VVXXXX
VVVXXX
VVVVXX
VVVVVX
insort
after X no V
what logic i want to implicit to get perfect output.
thanks
I hope you're happy with the following solution:
<?php
$data = array('A', 'B', 'C', 'D', 'E', 'F');
$count = count($data);
for ($k = 0; $k < $count; $k++) {
echo str_repeat("V", $k);
echo str_repeat("X", $count-$k);
echo "<br />";
}
?>
I used str_repeat to repeat the chars X and V. So you just need only one for-loop.
Output:
XXXXXX
VXXXXX
VVXXXX
VVVXXX
VVVVXX
VVVVVX
Example on Ideone.com
Simple just add break in your code as below:
<?php
$data = array('A','B','C','D','E','F');
$count = count($data);
for($k = 0;$k<$count;$k++){
foreach($data as $key => $value){
if($key == $k){
$datanew = $count - $k;
for($i=0 ; $i<$datanew ; $i++){
echo "X";
}
break;
}
else{
echo "V";
}
}
echo "</br>";
}
?>
And output as you wanted
Related
I have these for loop to determine consecutive number. What I achieve so far is to print the output in string.
$arr = [1,2,3,6,11,5,4,8,9,3];
for($start=0; $start<=count($arr); $start++){
for($end=$start+1; $end<=count($arr); $end++){
$total = $arr[$end] - $arr[$start];
if($total == 1){
echo 'Number is '.$arr[$start].','.$arr[$end].'<br/>';
} else {
echo '';
}
$arr[$start++];
}
}
My goal is to add the output into array.
I tried to use multidimensional array but no output display.
$arr = [1,2,3,6,11,5,4,8,9,3];
$arr3 = [];
for($start=0; $start<=count($arr); $start++){
for($end=$start+1; $end<=count($arr); $end++){
$total = $arr[$end] - $arr[$start];
if($total == 1){
$arr2 = array();
$arr2[] = $arr[$start].','.$arr[$end].'';
$arr3[] = $arr2;
} else {
}
$arr[$start++];
}
}
echo '<pre>';
print_r($arr3);
echo '</pre>';
exit;
Appreciate if someone can help me. Thanks.
you can simply use array functions, if sorting is important to you as #nice_dev said, you must sort your array before.
$arr = [1,2,3,6,11,5,4,8,9,3];
$cons = [] ;
while (array_key_last($arr) != key($arr)) {
if ((current($arr)+1) == next($arr)) {
prev($arr);
$cons[] = current($arr) . ',' . next($arr);
}
}
print_r($cons);
the output will be :
Array
(
[0] => 1,2
[1] => 2,3
[2] => 8,9
)
You can better sort() the input array first. This way, collecting all consecutive elements would get much simpler. If value at any index isn't +1 of the previous one, we add the $temp in our $results array and start a new $temp from this index.
Snippet:
<?php
$arr = [1,2,3,6,11,5,4,8,9,3];
$result = [];
sort($arr);
$temp = [];
for($i = 0; $i < count($arr); ++$i){
if($i > 0 && $arr[ $i ] !== $arr[$i - 1] + 1){
$result[] = implode(",", $temp);
$temp = [];
}
$temp[] = $arr[$i];
if($i === count($arr) - 1) $result[] = implode(",", $temp);
}
print_r($result);
Online Demo
$a = array('a','b','c','d','e','f');
$b = array('1','2');
$count = 0;
$d = 0 ;
$input = array('ina', 'inb','inc');
foreach ($a as $key => $v) {
$count++;
echo $v;
echo $input[$key];
if ($count%3 == 0){
echo $b[$d++];
reset($input);
}
}
I want like this output
1
a-ina
b-inb
c-inc
2
d-ina
e-inb
f-inc
Actually I want $input two times in a foreach loop. $a have 6 items $input have 3 items and $b have 2 items. I need
To make it more applicable, Demo
$a = array('a','b','c','d','e','f');
$input = array('ina', 'inb','inc');
$loop = 1;
$input_length = count($input); // TODO process the length with 0 case.
foreach($a as $index => $value){
if(!($i = $index % $input_length)){
echo $loop . PHP_EOL;
$loop++;
}
echo $value . "_" . $input[$i] . PHP_EOL;
}
You're keeping a few variables that you don't really need as they can be derived from the $key value from $a. To get the output you want, you could do this:
$a = array('a','b','c','d','e','f');
$b = array('1','2');
$input = array('ina', 'inb','inc');
$len = count($input);
foreach ($a as $key => $v) {
$idx = $key % $len;
if ($idx == 0){
echo $b[floor($key/3)] . PHP_EOL;
}
echo $v . "-";
echo $input[$idx] . PHP_EOL;
}
Output:
1
a-ina
b-inb
c-inc
2
d-ina
e-inb
f-inc
Demo on 3v4l.org
while loop generates 12 digit number. Store this numbers outside the while loop in array.
for($i=1;$i<5;$i++){
$active_id='';
$count=0;
$active_code='';
$myarray = array();
while ( $count < 12 ) {
$random_digit = mt_rand(0, 9);
$active_id .= $random_digit;
$count++;
}
echo $active_id;
}
for($i=1;$i<5;$i++){
$active_id='';
$count=0;
$active_code='';
while ( $count < 12 ) {
$value .= mt_rand(0, 9);
$count++;
$active_id[$i] = $value;
}
unset($value);
$myarray[] = $active_id;
}
var_dump($myarray);
it generates an array with for numbers each having 12 digits.
You get a huge number because you just print the values one after the other. Do it like this, instead:
$nums = array();
for($i = 1; $i < 5; $i++){
$active_id='';
$count=0;
while ( $count < 12 ) {
$active_id .= mt_rand(0, 9);
$count++;
}
$nums[] = $active_id;
}
echo implode('<br>', $nums);
Outputs something like:
547806804306
795608578570
440070793444
942559796496
You can also use a loop to display the values, like so:
foreach($nums as $v){
echo $v . '<br>';
}
The output:
462186324671
222725884540
242904883364
589742052131
Note that I removed a couple variables that weren't really needed.
i just want ask how i can use for loop to print
123
456
i'm try with this code:
<?php
$a = array(1,2,3,4,5,6);
foreach($a as $r){
for($q = 0; $q < 3; $q++) {
echo $r;
}
echo "<br />";
}
?>
But the problem is, it prints:
111
222
333
444
555
666
You're printing the value of $r three times for each value of $r; what you want is to print each value and print a break after every third.
Something like this would work:
foreach ($a as $i => $r) {
echo $r;
// insert break after every third value
if ($i > 0 && ($i + 1) % 3 == 0) {
echo '<br />';
}
}
Or, you could use array_chunk() to split the array up in chunks of three and print each of those.
foreach (array_chunk($a, 3) as $chunk) {
foreach ($chunk as $nr) {
echo $nr;
}
echo '<br />';
}
Use array_chunk($a, 3) and then use implode , that will give you required result.
$a = array(1,2,3,4,5,6,7,8,9);
$result='';
$i = 1;
foreach($a as $r)
{
$result.=$r;
if($i%3 == 0)
{
echo $result."<br />";
$result='';
}
$i++;
}
Is there a way in php to make two strings combine to one? I want to combine strings with the same size together?
$string1 = "apple"
$string2 = "block"
//FUNCTION STUFF HERE
$output = "abplpolcek";
You could try this:
$output='';
for($i=0;$i<strlen($string1);$i++)
{
$output.=$string1[$i];
$output.=$string2[$i];
}
echo $output;
Or you can write a simple function like this:
function funnyConcatStrings($str1, $str2)
{
$output='';
$leng=strlen($str1);
if(strlen($str1)==strlen($str2))
{
for($i=0;$i<$leng;$i++)
{
$output.=$str1[$i];
$output.=$str2[$i];
}
}
else
{
$output='Strings were not equal.\n';
}
return $output;
}
// Use it like this:
$mashedString=funnyConcatStrings($string1, $string2);
// or
echo funnyConcatStrings($string1, $string2);
$str_length = 5;
$output = '';
for($i = 0; $i < $str_length; $i++)
{
$output .= $string1[$i] . $string2[$i];
}
use for instance $string1[0] ( letter 'a' ) to access the first letter and make a for loop
Really easy;
$a = 'abcdef';
$b = 'ghijkl';
$l = strlen($a);
$s='';
for($i=0;$i<$l;$i++)$s .= $a[$i] + $b[$i];
echo $s;
1.) Check if the string have the same lengts with strlen
2.) Then you can iterate through the string and access them as an array
$string = 'test123';
echo $string[0] -> 't'
Then you can combine the string and safe them in a new variable.
This will work for strings that are different lenght as well
$string1 = "apple";
$string2 = "block";
$arr1 = str_split($string1);
$arr2 = str_split($string2);
if(count($arr1) > 0)
{
foreach($arr1 as $key => $value)
{
$_tmp[] = $value;
if(isset($arr2[$key]))
{
$_tmp[] = $arr2[$key];
}
}
}
else
{
$key = 0;
}
if($key + 1 < count($arr2))
{
for($i = $key + 1; $i < count($arr2); $i++)
{
$_tmp[] = $arr2[$key];
}
}
echo implode("", $_tmp);
echo str_shuffle("apple" . "block");
result: aekbplopcl