Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have written this function for my telegram bot in PHP but it doesn't work:
if($text == "t"){
$keyboard = array(
"inline_keyboard" => array(
array(
array(
"text" => "My Button Text",
"callback_data" => "myCallbackData"
)
)
)
);
$key = array(
"keyboard" => $keyboard,
);
keyboard($chatId, "asadsd", $key);
}
function keyboard($chatID, $text, $t)
{
$t2 = $t;
$t3 = json_encode($t2);
api("sendMessage?chat_id=$chatId&text=$text&parse_mode=HTML&reply_markup=$t3");
}
It should work but it is not doing so.
How can I fix this? (me and my bot)
Your code isn't working because you are trying to put the inline_keyboard array into a classic keyboard array. So:
Remove $key = array(...); and when you're calling the keyboard function put $keyboard instead of $key .
Put $chatID instead of $chatId in the keyboard function because php variable names are case-sensitive.
Remove the $t2 and $t3 since they are useless.
I suggest putting an arguments variable and using http_build_query to build the args, so it will automatically urlencode and build the vars.
Your code should result like this:
if($text == "t"){
$keyboard = array(
"inline_keyboard" => array(
array(
array(
"text" => "My Button Text",
"callback_data" => "myCallbackData"
)
)
)
);
keyboard($chatId, "asadsd", $keyboard);
}
function keyboard($chatID, $text, $t)
{
$args = array(
"chat_id" => $chatID,
"text" => $text,
"parse_mode" => "HTML",
"reply_markup" => json_encode($t),
);
api("sendMessage?".http_build_query($args));
}
Assuming everything is set, the code should work fine. Next time, please, be more specific
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have an array with following elements:
$arr = array(
'nick' => "blabla",
'pass => "blabla2"'
);
I would like to convert it somehow to strings, the first string would be the value of nick - "blabla", the second string would be the value of pass - "blabla2"
Thank you.
If you want to convert elements of the array to separate string variables you can use extract function to import elements from an array to variables. For example:
$arr = array(
'nick' => "blabla",
'pass' => "blabla2"
);
extract($arr);
echo $nick, ' ', $pass;
$arr = [
'nick' => "blabla",
'pass' => "blabla2"
];
$string_one=$arr['nick'];
$string_two=$arr['pass'];
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
$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");
above is my output but i want an array like below format. Please help me.
Correct Output:
$data = array ("id"=>"1","name"=>"mani","lname"=>"ssss");
check this, use is is_numeric to check number or string.
$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");
foreach ($data as $key => $val)
{
if(!is_numeric($key))
{
$new_array[$key] = $val;
}
}
print_r($new_array);
OUTPUT :
Array
(
[id] => 1
[name] => mani
[lname] => ssss
)
DEMO
The Code-Snippet below contains Self-Explanatory Comments. It might be of help:
<?php
// SEEMS LIKE YOU WANT TO REMOVE ITEMS WITH NUMERIC INDEXES...
$data = array( "0" => "1",
"id" => "1",
"1" => "mani",
"name" => "mani",
"2" => "ssss",
"lname"=> "ssss"
);
// SO WE CREATE 2 VARIABLES TO HOLD THE RANGE OF INTEGERS
// TO BE USED TO GENERATE A RANGE OF ARRAY OF NUMBERS
$startNum = 0; //<== START-NUMBER FOR OUR RANGE FUNCTION
$endNum = 10; //<== END-NUMBER FOR OUR RANGE FUNCTION
// GENERATE THE RANGE AND ASSIGN IT TO A VARIABLE
$arrNum = range($startNum, $endNum);
// CREATE A NEW ARRAY TO HOLD THE WANTED ARRAY ITEMS
$newData = array();
// LOOP THROUGH THE ARRAY... CHECK WITH EACH ITERATION
// IF THE KEY IS NUMERIC... (COMPARING IT WITH OUR RANGE-GENERATED ARRAY)
foreach($data as $key=>$value){
if(!array_key_exists($key, $arrNum)){
// IF THE KEY IS NOT SOMEHOW PSEUDO-NUMERIC,
// PUSH IT TO THE ARRAY OF WANTED ITEMS... $newData
$newData[$key] = $value;
}
}
// TRY DUMPING THE NEWLY CREATED ARRAY:
var_dump($newData);
// YIELDS::
array (size=3)
'id' => string '1' (length=1)
'name' => string 'mani' (length=4)
'lname' => string 'ssss' (length=4)
Or even concisely, you may walk the Array like so:
<?php
$data = array(
"0" => "1",
"id" => "1",
"1" => "mani",
"name" => "mani",
"2" => "ssss",
"lname" => "ssss"
);
array_walk($data, function($value, $index) use(&$data) {
if(is_numeric($index)){
unset($data[$index]);
}
});
var_dump($data);
// YIELDS::
array (size=3)
'id' => string '1' (length=1)
'name' => string 'mani' (length=4)
'lname' => string 'ssss' (length=4)
$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");
$new_data = array();
foreach($data as $k => $v)
if(strlen($k)>1) $new_data[$k] = $v;
print_r($new_data);
I'm a little confused as to what exactly you're looking for. You give examples of output, but they look like code. If you want your output to look like code you're going to need to be clearer.
Looking at tutorials and documentation will do you alot of good in the long run. PHP.net is a great resource and the array documentation should help you out alot with this: http://php.net/manual/en/function.array.php
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm building a gallery application and I've been having Issues finding out If there Is a way I can put HTML around each PHP array value In my case images,
I believe that If I echo It I'll get the values as text. I don't want to echo the values as text any options?
Thanks in advance.
If I understand correctly the following may be of some help, If you want to display the images in HTML you could use something like the following.
$images = array(
"1" => "image1.png",
"2" => "image2.jpeg",
"3" => "image3.gif"
);
foreach ($images as $key => $image) {
echo "<img src=\"{$image}\" alt=\"image {$key}\"/>";
}
and if you want to place the images inside HTML and back into your array you can use the following.
$images = array(
"1" => "image1.png",
"2" => "image2.jpeg",
"3" => "image3.gif"
);
foreach ($images as $key => $val) {
$images[$key] = "<img src=\"{$val}\" alt=\"image {$key}\"/>";
}
Not sure if this is just a "coders block", but it's very simple. grab the array, iterate it, wrap the output of each array element in a div and echo it back to the requesting page.
foreach($array as $item):
echo '<div>', $item ,'</div>';
endforeach;
Option 1
You seem to be looking for array_walk:
bool array_walk(array &$Input, callable $Fn [, mixed $extra = null ])
Applies the user-defined function Fn to
each element of the array Input, optionally
passing it the user-specified object $extra.
This is an example of wrapping every element of the array between two user-specified values.
<?php
$arry = array(
'pear',
'orange',
'banana',
);
array_walk($arry, function(&$item, $key, $data) {
$item = $data['before'].$item.$data['after'];
}, array(
'before' => 'this is a ',
'after' => ' squash.',
));
print_r($arry);
?>
Output:
Array
(
[0] => this is a pear squash.
[1] => this is a orange squash.
[2] => this is a banana squash.
)
Option 2
Another option could be that of using preg_replace_callback to perform a bulk replace on each element. This allows greater flexibility both in specifying what to replace, and how to replace it:
<?php
$arry = array(
'pear squash',
'squishy orange',
'squoshed banana',
);
// FIRST - wrap everything in double round brackets
$arry = preg_replace_callback('/^.*$/', function($matches){
return "(($matches[0]))";
}, $arry);
// SECOND - convert anything like "SQUASH" (but with whatever letter instead
// of the "A" - to "juice":
$arry = preg_replace_callback('/(.*)squ(.)sh(.*)/', function($matches){
// $matches[2] contains the whatever letter.
return $matches[1].'juice'.$matches[3];
}, $arry);
print_r($arry);
returns now
Array
(
[0] => ((pear juice))
[1] => ((juicey orange))
[2] => ((juiceed banana))
)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got an array for events on my site that looks like this:
array(
'title' => 'Name',
'link' => 'http://www.eventssite.com',
'image' => '_img/event_img.jpg',
'location' => 'Florida, US',
'year' => '2013',
'date' => 'Dec. 12-14',
'desc' => 'Description about the event.',
'dateid' => '1212013'
),
I'd like to sort the array before the foreach by the dateid so that they show in the proper Date order.
Additionally I'm trying to identify which one of the events is closest to the actual date, as I am using a carousel type system that needs to know which to display first.
I've researched usort and am not able to get it to go on my own, Thank you for any assistance on these!
Using this function: http://php.net/usort
An example would be something like:
<?php
//just an array of arrays with the date as one of the values of the array
$array = array(
array(
'date' => '05/02/1988',
'name' => 'Jacob'
),
array(
'date' => '12/12/1968',
'name' => 'Sherry'
),
array(
'date' => '05/15/1978',
'name' => 'Dave'
)
);
//usort is used for non conventional sorting.
//which could help in this case
//NOTICE - we are not setting a variable here!
//so dont call it like $array = usort(...) you will just be setting $array = true
usort($array,'sortFunction');
//display the results
var_dump($array);
//function called by usort
function sortFunction($a,$b){
//turn the dates into integers to compare them
//
$strA = strtotime($a['date']);
$strB = strtotime($b['date']);
//don't worry about sorting if they are equal
if($strA == $strB){
return 0;
}
else{
//if a is smaller than b, the move it up by one.
return $strA < $strB ? -1 : 1;
}
}
?>
(in case youre interested, line 40 is called a Ternary)
edited for clarity
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have one code, I cannot imagine, how can I read this array, using php. please help me to..
array(
'serialize_data' => array(
array('name' => 'cadidate_id_0', value => '81112890V'),
array('name' => 'cadidate_id_1', value => '822312890V'),
array('name' => 'cadidate_id_2', value => '873312110V'),
array('name' => 'cadidate_id_3', value => '873312890V')
)
);
foreach($array as $key=>$value){
echo $value[0]['name'];
echo $value[0]['value'];
}
use above line to print your array element, like index number 0.
Have you tried
print_r($your-array);
?
To access individual levels it looks like you need to go down a level or two. I.e.
echo $your-array['serialize_data'][0]['name'];
you can use following method.
$array = array(
'serialize_data' => array(
array('name' => 'cadidate_id_0', value => '81112890V'),
array('name' => 'cadidate_id_1', value => '822312890V'),
array('name' => 'cadidate_id_2', value => '873312110V'),
array('name' => 'cadidate_id_3', value => '873312890V')
)
);
foreach($array as $key=>$value){
echo '<pre>'; print_r($value); echo '</pre>';
}
using this method you can read or access its name and value by passing the index number.
like:
echo '<pre>'; print_r($value[0]); echo '</pre>';
try
//grab array of name and value
$array=$data-array['serialize_data']
//traverse
foreach($nv as $array)
{
$name=$nv['name'];
$value=$nv['value'];
//do something to name
print $name;
//do something to value
print $value;
}