I'm trying to get the checked values into a PHP array so that I am able to loop through the array but I can't seem to convert it to an array.
jQuery Ajax Posts
checked=28,24
PHP
$checked = $_POST['checked'];
$arr = array($checked);
print_r($arr);
OUTPUT
Array
(
[0] => 28,24
)
Use explode function to convert comma separated string to an array,
$arr = explode(",",$checked);
Related
I did
file_put_contents("x.txt", print_r($_POST, true));
And I got the below output
Array
(
[merchant_id] => ece6ba4ecx24d070b4c13b
[invoice_id] => 1772fb6eafda04ceb11c77cf719a347f
[invoice_created] => 1634467789
[invoice_expires] => 1634469589
[invoice_amount] => 10
)
How do I store $_POST values only (not keys) separated by & in form of a string like the one below:
Will this work
echo implode("&",$_POST);
Output example:
ece6ba4ec435986db93924d070b4c13b&1772fb6eafda04ceb11c77cf719a347f&1634467789&1634469589&10
Use implode() and set & as the separator:
$string = implode('&', $_POST);
You can use the built-in function of PHP array_values() to get only the values from the $_POST array.
Then, use the built in function implode, to combine the whole array into one string seprated by & sign.
$values = array_values($_POST);
$string_values = implode("&",$values);
How to get particular value in this array in PHP
[{"key1":"value1","key2":"value2"}]
You can use to find specific key with value by using array_search function
$json = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($json, true);
echo array_search("value2", $array[0]); //key2
echo $array[0]["key2"] //value2
You can get specific value from the array by using PHP array_search() Function. Below is an example code that you need to follow,
Since provided PHP array is in JSON format you first need to decode it as follows,
$jsonArray = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($jsonArray, true);
So the decoded array will be like this,
Array
(
[0] => Array
(
[key1] => value1
[key2] => value2
)
)
And then you can use array_search() Function,
<?php
$jsonArray = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($jsonArray, true);
echo array_search("value1", $array[0]); // Search an array for the value "value1" and return its key. In this case the key will be "key1"
?>
Note:
array_search() function is the best bet. But you also can use a for-loop to iterate all values and check if matches the value that you need. But I recommend using array_search() function.
Hope this helps you!
Hello i want to store my array data into variable.
$array = array('0' => "14254",'1' => "145245");
// I want to store this array value into normal single variable
// store array data to $string.
// something like this >> $string is 14254,145245
Use array's implode() method.The implode() function returns a string from the elements of an array.
<?php
$array = array('0' => "14254",'1' => "145245");
$script = implode(',',$array);//outputs 14254,145245
echo $script;
?>
For more see manual PHP Implode
Look at 'serialize' function in PHP. It will provide you array as a string that later could be converted back to array with 'unserialize'
$var = {"4":true,"6":true,"8":true}
In The above string I want to get numbers into array.
Need: $var2 = [[0]=>4, [1]=>6, [2]=>8];
All response will be appreciated.
You should use json_decode and array_keys to accomplish it:
array_keys(json_decode($var, true));
First you decode the string using json_decode, the second argument means that the function should return an associative array and non an array of objects. This willl help up get the array keys.
$decoded = json_decode($var, true);
You get the array keys with this loop and place them in $var
foreach($decoded as $key => $value){
$var2[] = $key;
}
As i comment, use array_keys, and json_decode.
I don't believe that this question has an answer, So i did't answer
it. But i did it later.
You have an json, so you need to use json_decode now you jave an array where your keys are the desired value. so use array_keys.
$var = '{"4":true,"6":true,"8":true}';
$arr = json_decode($var, true);
echo '<pre>';
print_r(array_keys($arr));
Result:
Array
(
[0] => 4
[1] => 6
[2] => 8
)
Is there any function or method to remove array value for example, i have an array like this:
Array (
[0] => (Some string value)51351
[1] => (Some string value)43822
)
So the question is, How do i get the value that is not in the "( )". and counting the value of array after remove "( Some string value )" to do some looping process?
Thank you!
<?php
function digits_only($str){
return preg_replace("/\(.*\)/", "", $str);
}
$arr = array("(Some content)531", "(Another Content)613");
$digits_array = array_map("digits_only", $arr);
var_dump($digits_array);
echo array_sum($digits_array);
Live Demo:
http://codepad.org/mYPL3PYH