I have a probrem but I don't know how to fix it,
here is two array
first is$X = A,B,C,D
second likes this a[1]a[2]a[3]a[4]a[5]a[6]a[7]a[8]
I want to loop likes this
Aa[1]a[2]
Ba[3]a[4]
Ca[5]a[6]
Da[7]a[8]
now i write likes this
foreach($X as $key) {
for ($i = 0; $i < 8; $i++) {
$a[$i];
}
$g=$a[$key][$i];
}
But it isn't that thing I want.
how can I fix it? thank you for your help.
You can try for
<?php
$X = ['A','B','C','D'];
$Y = ['a[1]a[2]','a[3]a[4]','a[5]a[6]','a[7]a[8]'];
$result = [];
for ($i=0; $i < sizeof($X) ; $i++) {
$result[] = $X[$i].$Y[$i];
}
?>
Output
Array
(
[0] => Aa[1]a[2]
[1] => Ba[3]a[4]
[2] => Ca[5]a[6]
[3] => Da[7]a[8]
)
Related
My for loop should be adding 9 values to an array, but for some reason stops on 6. This only happens when I use the square bracket syntax to add the key and value to the array. Here's the code:
$sentences = $this->sentences($sentence);
$n = count($sentences);
echo $n;
$values = array();
for($i = 0; $i < $n; $i++){
$s1 = $sentences[$i];
for($j = 0; $j < $n; $j++){
$s2 = $sentences[$j];
$values[$i][$j] = $this->checkvalues($s1,$s2);
}
}
$sentences_dic = array();
$other = array();
$otherTwo = array();
for($i = 0; $i < $n; $i++){
$score = 0;
for($j = 0; $j < $n; $j++){
$score = $score+$values[$i][$j];
}
$other[$i] = $score;
$otherTwo[$i] = $sentences[$i];
$sentences_dic[($sentences[$i])]=$score;
var_dump($otherTwo);
}
//maybe need
return $sentences_dic;
I am not sure why this is happening. The array is only printing
Here is what it is printing. It should be printing the seventh, eigth and ninth terms but it isnt. All the terms it isn't adding were all able to print in the other and otherTwo arrays.
Array
(
[first] => banana
[second] => banana2
[third] => banana3
[fourth] => banana4
[fifth] => banana5
[sixth] => banana6
)
when it should be printing all 9 values and keys. I don't understand why I am able to add all sentences and scores to their full extent(9) to two seperate arrays, but when I try to use square bracket syntax it only goes to 6.
Given your output $sentences appears to be an array and has duplicates. Array keys must be unique and duplicate keys are overwritten.
You could do something like this for a multidimensional array:
if(isset($sentences_dic[$sentences[$i]])) {
$sentences_dic[$sentences[$i]][] = $score;
} else {
$sentences_dic[$sentences[$i]] = $score;
}
Or to sum the scores of duplicates:
if(isset($sentences_dic[$sentences[$i]])) {
$sentences_dic[$sentences[$i]] += $score;
} else {
$sentences_dic[$sentences[$i]] = $score;
}
I am trying to create an array from 2 other arrays. I am having a problem getting them merged outside of their loops.
Here is the code to build the 2 arrays:
$redDogs = '3';
$blueDogs = '2';
$i = 1;
// build red dogs array
$redDogs = (int)$redDogs;
while($i <= $redDogs) {
$reds[] = 'red_dog_' . $i++;
}
foreach ($reds as $red) {
print '<pre>'; print_r($red); print '</pre>';
}
$j = 1;
// build blue dogs array
$blueDogs = (int)$blueDogs;
while($j <= $blueDogs) {
$blues[] = 'blue_dog_' . $j++;
}
foreach ($blues as $blue) {
print '<pre>'; print_r($blue); print '</pre>';
}
How could I create an array like:
print_r($alldogs);
To produce the following output:
Array {
[0] => red_dog_1
[1] => red_dog_2
[2] => red_dog_3
[3] => blue_dog_1
[4] => blue_dog_2
}
I have tried array_merge($red, $blue) but don't seem to get any values.
Any help is greatly appreciated.
$redDogs = '3';
$blueDogs = '2';
$i = 1;
// build red dogs array
$redDogs = (int)$redDogs;
while($i <= $redDogs) {
$reds[] = 'red_dog_' . $i++;
}
$j = 1;
// build blue dogs array
$blueDogs = (int)$blueDogs;
while($j <= $blueDogs) {
$blues[] = 'blue_dog_' . $j++;
}
print_r(array_merge($reds, $blues));
Merge your array before looping through the array.
Your foreach statements were converting your array to strings.
I have array like this
Array ( [0] => F [1] => F [2] => N [3] => )
How to run the two loops with above array ?
for ($x = 0; $x <= 2; $x++) {
//In this loop i need the output like above
F-01
F-02
}
I have an loop im fetching some data in it like
for ($x = 0; $x <= 1; $x++) {
//In this loop i need the output like above
N-01
}
For that on you need to do something like below
<?php
$myarray = Array ( 'F' ,'F' ,'N' , 'l');
$j =0;
for($x = 0; $x < 2; $x++ ){
echo $myarray[$x].'-'.$j.$x;
echo "<br>";
}
echo "<br>";
$m=0;
for($k=2;$k<=3;$k++){
echo $myarray[$k].'-'.$j.$m;
echo "<br>";
}
?>
I hope this will help you.
I have array in php like
$randomarray = array('1106'=>'5','1110'=>'2','11867'=>'3','1206'=>'2','1210'=>'1','1223'=>'6','1235'=>'3','12565'=>'4','1258'=>'5','12690'=>'2','12693'=>'3','1283'=>'1','12944'=>'5');
I want to randomly pick elements from the array with the count of exactly 20. Each element have to only one time
I tried some array random example. I can't able to get the exact total which i expect.
this is the example of what i did that. But loop went to infinitive,
function randomTo($numIn) {
global $randomarray;
$numOut = 0;
$numbers = array();
do {
$key = array_rand($randomarray );
$add = $mainarray[$key];
if($numOut + $add > $numIn)
continue;
$numOut += $add;
$numbers[] = $add;
unset($mainarray[$key]);
} while( $numOut != $numIn );
return $numbers;
}
$testdata = randomTo(20);
The problem that you're trying to solve is called Subset sum and it's a subset of the Knapsack problem.
Just google for it, and while you're at it, google for Dynamic programming as it's a way of approaching the problem.
if(count($randomarray)) > 20
print_r(array_rand($randomarray, 20));
Get some Idea from this :
An example for getting random value from arrays
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
$a = array("apple", "banana", "cherry");
print_r(array_random($a));
print_r(array_random($a, 2));
?>
cherry
Array
(
[0] => banana
[1] => apple
)
And example for getting random value from assoc arrays;
<?php
function array_random_assoc($arr, $num = 1) {
$keys = array_keys($arr);
shuffle($keys);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[$keys[$i]] = $arr[$keys[$i]];
}
return $r;
}
$a = array("a" => "apple", "b" => "banana", "c" => "cherry");
print_r(array_random_assoc($a));
print_r(array_random_assoc($a, 2));
?>
Array
(
[c] => cherry
)
Array
(
[a] => apple
[b] => banana
)
I have an array with a single value collected from database.
Within a while loop I wish to explode this for every two values.
Example:
$data = array('20;40;60;80;100;150;200;300;500;1000');
I want to explode this value and end up with the following loop:
$lowprice = "20";
$highprice = "40";
I couldn't find any examples of this.
Many thanks everyone who answered!
You can use preg_match_all().
Example:
$text = '20;40;60;80;100;150;200;300;500;1000';
preg_match_all("/([^;]+);([^;]+)/", $text, $pairs, PREG_SET_ORDER);
foreach ($pairs as $pair) {
// ...
$lowvalue = $pair[1];
$highvalue = $pair[2];
// ...
}
If you really must use explode and a while loop, the following will also work:
$text = '20;40;60;80;100;150;200;300;500;1000';
$data = explode(';', $text);
$i = 0;
$count = count($data);
while ($i < $count) {
// ...
$lowvalue = $data[$i++];
$highvalue = $data[$i++];
// ...
}
If you just want the first and second values as your low/high:
$data = array('20;40;60;80;100;150;200;300;500;1000');
list($lowprice,$highprice) = explode(';',current($data));
echo '$lowprice=',$lowprice,PHP_EOL;
echo '$highprice=',$highprice,PHP_EOL;
If you want an array of lows and highs:
$data = array('20;40;60;80;100;150;200;300;500;1000');
$lowprices = $highprices = array();
$data = explode(';',current($data));
$dataCount = count($data);
var_dump($data);
for ($i=0; $i < $dataCount; $i += 2) {
$lowprices[] = $data[$i];
$highprices[] = $data[$i+1];
}
echo '$lowprices=';
var_dump($lowprices);
echo '$highprices=';
var_dump($highprices);
EDIT
Why not start out with a proper array of values in the first place: it would simplify this a lot
$data = array(20,40,60,80,100,150,200,300,500,1000);
list($lowprice,$highprice) = $data;
echo '$lowprice=',$lowprice,PHP_EOL;
echo '$highprice=',$highprice,PHP_EOL;
or
$data = array(20,40,60,80,100,150,200,300,500,1000);
$lowprices = $highprices = array();
$dataCount = count($data);
var_dump($data);
for ($i=0; $i < $dataCount; $i += 2) {
$lowprices[] = $data[$i];
$highprices[] = $data[$i+1];
}
echo '$lowprices=';
var_dump($lowprices);
echo '$highprices=';
var_dump($highprices);
Explode initially on the ;. Then in a for loop incrementing by 2, you can assign the current and next values to a sub-array:
$initarray = explode(";", "20;40;60;80;100;150;200;300;500;1000");
$num = count($initarray);
// Main array to hold subarrays
$outputarray = array();
for ($i=0; $i<$num; $i=$i+2) {
// Add the current pair ($i and $i+1) to a sub-array
$outputarray[] = array("lowprice"=>$initarray[$i], "highprice"=>$initarray[$i+1]);
}
Outputs:
Array
(
[0] => Array
(
[lowprice] => 20
[highprice] => 40
)
[1] => Array
(
[lowprice] => 60
[highprice] => 80
)
[2] => Array
(
[lowprice] => 100
[highprice] => 150
)
[3] => Array
(
[lowprice] => 200
[highprice] => 300
)
[4] => Array
(
[lowprice] => 500
[highprice] => 1000
)
)
You may use loop like this one (using explode() and array_shift()):
$data = explode( ';', '20;40;60;80;100;150;200;300;500;1000');
while( count( $data) > 1){
$lowprice = array_shift( $data);
$highprice = array_shift( $data);
}
Or use for loop (should be more effective than calling count() in every iteration):
$count = count( $data);
for( $i = 0; $i < $count; $i+=2){}