how to create dummy data based on what we give number like this format
for example when i give $var=2, so it will create two, when i give $var=100, it will create 100 like this using arrays in php
create like this based on number given, give 2 create like this
[{"email":"test#test.com"},{"email":"test#test.com"}]
give 4 create like this
[{"email":"test#test.com"},{"email":"test#test.com"},{"email":"test#test.com"},{"email":"test#test.com"}]
Use array_fill:
$emails = array_fill(0, 100, 'test#test.com');
With your structure it is:
$emails = array_fill(0, 100, ['email' => 'test#test.com']);
You could do this:
<?php
$dummy = array();
$dummyAmount = 100;
for($i = 0; $i < $dummyAmount; $i++){
$dummy[] = array("email" => "test#test.com");
}
?>
Try this:
$a = [];
$var = 100;
for( $i = 0; $i < $var; ++$i ) {
$a[] = [
'email' => 'test#test.com'
];
}
$json = json_encode($a)
Well, use a loop with as many circles as you need.
And inside the loop, create your email addresses.
Like a so:
$amount = 100;
$emails = [];
for ($i = 0; $i < $amount; $i++) {
$emails[] = ['email' => 'test#test.com' ];
}
And then json encode it to get your expected output:
$emailJSON = json_encode($emails);
Related
I want to create a function that generates an array of elements which consist of incremented numbers appended to a static string/prefix and looks like this:
$arr = [
'file1',
'file2',
'file3',
'file4',
'file5',
];
I know I can create a simple loop like this one:
$arr = [];
for( $i = 1; $i <= 5; $i ++ ){
$arr[] = 'file' . $i;
}
But the idea is to give the parameters $text = file, $start = 1, $elements = 5 and optional $step = 1 and use built-in php functions to achieve it.
I tried something like:
$arr = array_merge( array_fill( 0, 5, 'file' ), range( 1, 5, 1 ) );
But from that one I got
$arr = [
"file",
"file",
"file",
"file",
"file",
0,
1,
2,
3,
4,
5,
];
You can choose a functional style or classic loop approach using your parameters. I am using the same equation in map() and whileloop() to determine the end of the range and avoid the use of an additional counter variable. The forloop() function is a little easier to understand, but has to maintain an extra variable which must be incremented to get the number of elements right. (Demo)
function map($text = 'file', $elements = 5, $start = 1, $step = 1) {
$end = $start + $elements * $step - 1;
return array_map(
fn($i) => $text . $i,
range($start, $end, $step)
);
}
Or
function whileloop($text = 'file', $elements = 5, $start = 1, $step = 1) {
$result = [];
$end = $start + $elements * $step - 1;
while ($start <= $end) {
$result[] = $text . $start;
$start += $step;
}
return $result;
}
Or
function forloop($text = 'file', $elements = 5, $start = 1, $step = 1) {
$result = [];
for ($i = 0; $i < $elements; ++$i, $start += $step) {
$result[] = $text . $start;
}
return $result;
}
You are almost there. You can generate the array via range and then prepend file via array_map like below:
<?php
$arr = array_map(fn($e) => 'file'. $e,range(1,5));
print_r($arr);
If you want to keep your beginner-friendly and readable syntax, you can simply modify your code like such:
$arr = [];
for( $i = $start; $i < $start + ($elements * $step); $i += $step ){
$arr[] = $text . $i;
}
Edit: be careful if your variables come from user input: a negative $step could result in an infinite loop.
If you want to give those parameters, then I believe you are looking at writing a function. In the function a for loop should achieve what you want.
function heaven_knows_why($text, $start, $elements, $step = 1)
{
$arr = [];
for( $i = $start, $j=1, $s=$start; $j <= $elements; $j++, $i++, $s += $step ){
$arr[] = $text . $s;
}
return $arr;
}
print_r(heaven_knows_why('duh', 1, 5));
print_r(heaven_knows_why('duh', 1, 5, 2));
RESULTS
Array
(
[0] => duh1
[1] => duh2
[2] => duh3
[3] => duh4
[4] => duh5
)
Array
(
[0] => duh1
[1] => duh3
[2] => duh5
[3] => duh7
[4] => duh9
)
A Demo
I am trying to insert batch query in code igniter, I am not able to make array_merge work. Don't know whats the problem. M getting blank array.
$epin_amt = $this->input->post('amount');
$qty = $this->input->post('qty');
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
array_merge($data, $array);
}
print_r($data) ; // Produce : array( )
You have to assign the merged array back to your $data variable:
<?php
$epin_amt = /*$this->input->post('amount')*/ 5;
$qty = /*$this->input->post('qty')*/6;
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
$data = array_merge($data, $array);
}
print_r($data) ;
array_merge returns array. You need something like this:
$result = array_merge($data, $array);
You are merging the arrays but it's not done by reference so you're throwing the resulting array away. array_push() it instead, that will keep adding the arrays to your $data array:
<?php
$epin_amt = 10;
$qty = 20;
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
array_push($data, $array);
}
print_r($data) ;
I've got an array of 'contestant entrants' which can be shown like this:
$all_entrants = array(
array('username'=>'122', 'number_of_entries'=>1),
array('username'=>'123', 'number_of_entries'=>4),
array('username'=>'124', 'number_of_entries'=>3),
...
)
From these entries I need to create another array called $draw.
The $draw array will have username repeated as many times as it's corresponding number_of_entries. So for the above example it might look like this:
$draw = array("122", "123", "123", "123", "123", "124", "124", "124")
I want this so that I can later generate a random number, and find the winner by doing something like $draw[$randomNumber];
However I cannot get my head around how to create that $draw array from the $all_entrants array... Any help will be greatly appreciated!
I assume you're looking for something like this?
$draw = array();
foreach($all_entrants as $entrant) // loop through array with entrants
for ($i = 0; $i<$entrant['number_of_entries']; $i++) //get number of entries
$draw[] = $entrant['username']; //add them to the $draw array
I think it's a question about select one from a group of name which has different weight.
maybe an array like this
$group = array(
array('122' => 1),
array('123'=> 4),
array('124'=> 3)
);
First Calculate the sum of the weight, or may be it has been known already
$total_weight = 0;
foreach($group as $weight){
$total_weight += $weight;
}
Then generate a random number from 0 to $total_weight, eg. 0<=$rand_number
$current_total = 0;
foreach($group as $name => $weight){
if($rand_number <= $current_total)
return $name;
$current_total += $weight;
}
--
BTW, I'm new here, more to learn:)
<?php
$draw = array();
foreach($all_entrants as $entrant) {
for($i=0; $i<$entrant['number_of_entries']; $i++) {
$draw[] = $entrant['username'];
}
}
$draw = array();
foreach($all_entrants as $entrant) {
for($i=0; $i<$entrant['number_of_entries']; $i++) {
$draw[] = $entrant['username'];
}
}
$all_entrants = array(
array('username'=>'122', 'number_of_entries'=>1),
array('username'=>'123', 'number_of_entries'=>4),
array('username'=>'124', 'number_of_entries'=>3),
);
$draw = array();
foreach($all_entrants as $entrant) {
$draw = array_merge(
$draw,
array_fill(0, $entrant['number_of_entries'], $entrant['username'])
);
}
var_dump($draw);
Try this
$newarray=array();
foreach($all_entrants as $list){
for($i=1;$i<=$list['number_of_entries'];$i++){
array_push($newarray,$list['username']);
}
}
<?php
$all_entrants = array(
array('username'=>'122', 'number_of_entries'=>1),
array('username'=>'123', 'number_of_entries'=>4),
array('username'=>'124', 'number_of_entries'=>3)
);
$draw = array();
for ($i = 0; $i < count($all_entrants); $i++)
{
$entrants = $all_entrants[$i];
$name = $entrants["username"];
$entry_count = $entrants["number_of_entries"];
for ($j = 0; $j < $entry_count; $j++) $draw[] = $name;
}
print_r($draw);
?>
Hope it helps.
check this :--
$result=array();
$all_entrants = array(
array('username'=>'122', 'number_of_entries'=>1),
array('username'=>'123', 'number_of_entries'=>4),
array('username'=>'124', 'number_of_entries'=>3)
);
foreach($all_entrants as $value)
for($i=0;$i<$value['number_of_entries'];$i++)
array_push($result,$value['username']);
echo '<pre>';
print_r($result);
output :-
Array
(
[0] => 122
[1] => 123
[2] => 123
[3] => 123
[4] => 123
[5] => 124
[6] => 124
[7] => 124
)
First, i have these values.
$Arr1 = array(1/1, 1/2, 3/1);
$Arr2 = array(1/1, 4/1);
$Arr3 = array(1/1);
and i need an output with 3 arrays like these:
$a1 = array (1/1, 1/2, 3/1);
$a2 = array (2/1, 1/1, 4/1);
$a3 = array (1/3, 1/4, 1,1);
What i am trying is :
for ($i=0; $i<count($Arr1); $i++) {
${"a".$i} = array(
//here, the number of array elements depends to the length of $a1
);
}
Any help ? thanks
I think this image helps to understand the problem:
First off, using a 2D array will make your life a lot easier.
So first, initialize your values like this:
$matrix_size = 3;
$matrix = array();
for($i = 0; $i < $matrix_size; $i++){
$matrix[$i] = array_fill(0, $matrix_size, null);
}
$matrix[0][0] = 1/1;
$matrix[0][1] = 1/2;
$matrix[0][2] = 3/1;
$matrix[1][1] = 1/1;
$matrix[1][2] = 4/1;
$matrix[2][2] = 1/1;
Then you can run a loop like this:
foreach($x = 0; $x < $matrix_size; $x++){
foreach($y = 0; $y < $matrix_size; $y++){
if(is_null($matrix[y][x]) && !is_null($matrix[x][y])){
$matrix[y][x] = 1/$matrix[x][y];
}
}
}
I'm sure there is a much more efficient way to do this, but this is a start for you to explore.
I have two arrays like this
array x [Firefox,IE,Chrome,Opera]
array y [40,30,25,5]
Required final [[Firefox,40],[IE,30],[Chrome,25],[Opera,5]]
I need this in PHP.I think I can run a for loop and do something like this .
final23 [0][i] = x[i];
final23 [1][i] = y[i];
Is there any better way or built in function in PHP ?
$result = array();
$size = max(count($x), count($y));
for ($i = 0; $i < $size; $i++) {
$result[] = array(
isset($x[$i]) ? $x[$i] : null,
isset($y[$i]) ? $y[$i] : null
);
}
$x = array("Mozzila","IE","Firefox","Opera");
$y = array(40,30,25,5);
$final = array();
$i = 0;
foreach($x as $a){
$final[] = array($a,$y[$i]);
$i++;
}
Learn about Associated array.
$arr = new Array("40"=>"FireFox","30"=>"IE","25"=>"Chrome","5"=>"Opera");