how can i use loop in array of array (php)? - php

i wanna use Repeat loop (for , while and etc ... )in array of array of in php :
for($x = 0; $x < $arrlength; $x++) {
$poets = array(
'keyboard' => array(
array($saves[$x]) // this is just one array i don't want this !
),
);
$url= "https://api.telegram.org/bot".$token."/sendMessage?
chat_id=".$chat_id."&text=".$text."&reply_markup=".$jsonPoets;
file_get_contents($url);
}
this is not working , i wanna do like this :
for($x = 0; $x < $arrlength; $x++) {
$poets = array(
'keyboard' => array(
//i wanna do like this ...
array($saves[$x])
//array($saves[$x])
//array($saves[$x])
.
.
.
)
);
$url= "https://api.telegram.org/bot".$token."/sendMessage?
chat_id=".$chat_id."&text=".$text."&reply_markup=".$jsonPoets;
file_get_contents($url);
}
it means i wanna make multi array in array with loop repeat .

$poets= array('keyboard' => array());
for($x = 0; $x < $arrlength; $x++) {
$poets['keyboard'][] = array($saves[$x]);
}
Like this?

Related

How to create dummy values with keys + array php

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);

For Loop print results line by line

Hi i have following php codes(part of my full code):
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['serisname1'] = $new_instance['serisname1'];
$instance['serisname2'] = $new_instance['serisname2'];
$instance['serisname3'] = $new_instance['serisname3'];
$instance['serisname4'] = $new_instance['serisname4'];
$instance['serisname5'] = $new_instance['serisname5'];
$instance['serisname6'] = $new_instance['serisname6'];
$instance['serisname7'] = $new_instance['serisname7'];
$instance['serisname8'] = $new_instance['serisname8'];
$instance['serisname9'] = $new_instance['serisname9'];
$instance['serisname10'] = $new_instance['serisname10'];
$instance['serisname11'] = $new_instance['serisname11'];
$instance['serisname12'] = $new_instance['serisname12'];
$instance['serisname13'] = $new_instance['serisname13'];
$instance['serisname14'] = $new_instance['serisname14'];
$instance['serisname15'] = $new_instance['serisname15'];
return $instance;
for($x = 1; $x <= 15; $x++)
$serisname = $instance[serisname.$x];
$items[] = $serisname;
print_r($items);
my export :
array ( [0] => The Flash )
i want its be like :
array ( [0] => The Flash [1] => Arrow [2] => Game Of Throne and etc...)
the problem is its only echo last result but i want its echo every 15 results line by line.
for($x = 1; $x <= 15; $x++){
$serisname = $instance['serisname'.$x];
$items[] = $serisname;
}
print_r($items);
If I understead your question correctly, you can try this:
$count = 0;
for($x = 0; $x <= count($instance); $x++) {
$items[$x][] = $instance[serisname.$x];
if($count == 15) {
$count = 0;
}
}
Maybe can include the instance before the loop in your question so we can replicate the array?

Custom Increment by given array in php

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.

Json array in php code

I need to use Json array in the php code.
The problem is that I'm in a for loop and need to separate the array in 2 and then want to merge it. but so far it didn't work.
I use it to have a graph (jqxChart).
Here is my code
for($i = 0; $i < $nb; $i++){
if ($i%2 == 1){
$time[$i] = (hexdec($hour[$i]));
$orders1[] = array(
'OrderDate' => $time[$i],
);
}else{
$hour[$i] = $hour[$i] + 1;
$orders2[] = array(
'ProductName' => $hour[$i],
);
}
}
$orders[] = array_merge_recursive( $orders1[], $orders2[] );
}
echo json_encode($orders);
Thanks
try this code,
$orders1 = array();
$orders2 = array();
for($i = 0; $i < $nb; $i++){
if ($i%2 == 1){
....
$temp1 = array(
'OrderDate' => $time[$i],
);
array_push($orders1, $temp1);
}else{
....
$temp2 = array(
'ProductName' => $hour[$i],
);
array_push($orders2, $temp2);
}
}
}
$orders = array_merge( $orders1, $orders2 );
echo json_encode($orders);
Remove the square brackets. Instead of:
$orders[] = array_merge_recursive($orders1[], $orders2[]);
^^ ^^ ^^
Just put:
$orders = array_merge($orders1, $orders2);

PHP array unset() behavior - last array key remains?

I have an array constructed out of several strings (HTTP addresses) on which I run a PHP Filter and the unset() method to remove non-valid URLs. However, the last array item is never removed - and I don't know why, or how I solve this. I'm hoping you guys can help.
$url1 = "http://localhost/work/project/scrapes/1.html";
$url2 = "";
$url3 = "";
$urls = array($url1, $url2, $url3);
for($x = 0; $x < sizeof($urls); $x++){
if(!filter_var($urls[$x], FILTER_VALIDATE_URL)){
unset($urls[$x]);
}
}
print_r() gives me this:
Array ( [0] => http://localhost/work/project/scrapes/1.html [2] => )
I have no idea why $urls[2] is still there, and why it's not removed.
Because you calculate the size() dynamically - it reduces as long as you delete elements. So the fix is to get the size before the loop:
$url1 = "http://localhost/work/project/scrapes/1.html";
$url2 = "";
$url3 = "";
$urls = array($url1, $url2, $url3);
$size = sizeof($urls); // <----
for($x = 0; $x < $size; $x++){
if(!filter_var($urls[$x], FILTER_VALIDATE_URL)){
unset($urls[$x]);
}
}
var_dump($urls);
That is because you are calculating the size of the array in each iteration.
By Iteration:
sizeof($urls) = 3, $x = 0, $x < sizeof($urls) TRUE unset($urls[0]);
sizeof($urls) = 2, $x = 1, $x < sizeof($urls) TRUE unset($urls[1]);
sizeof($urls) = 1, $x = 2, $x < sizeof($urls) FALSE ... no more code executed
save the length of the array before start the loop
$length = sizeof($urls);
for($x = 0; $x < $length; $x++){
}

Categories