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);
Related
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'
I have an array with some keys and I want to get the array values according to the array keys where the keys are in a string.
Example:
$arr = array(
"COV" => "Comilla Victorians",
"RK" => "Rajshaji Kings"
);
$str = "COV-RK";
Now I want to show Comilla Victorians VS Rajshaji Kings.
I can do it using some custom looping, But I need some smart coding here and looks your attention. I think there are some ways to make it with array functions that I don't know.
You could do something like:
echo implode(' VS ', array_map(function($v) use ($arr) { return $arr[$v]; }, explode('-', $str)));
So explode the string, map the resulting array, returning the value of the matching key in $arr, then just implode it.
You could try this:-
<?php
$arr = array(
"COV" => "Comilla Victorians",
"RK" => "Rajshaji Kings"
);
$str = "COV-RK";
$values = explode("-", $str); // explode string to get keys actually
echo $arr[$values[0]] . " VS " . $arr[$values[1]]; // print desired output
I'm fairly new at php, but this seems to be me overlooking something completely basic?
I have some values in a database column, that are comma separated like so:
1,2,3
When I try to get the sum of the values, I expect the echo of array_sum to be 6, but I only get returned the first value ie. "1"
echo $amount; //Gives 1,2,3 etc.
$amount_array = array($amount);
echo array_sum($amount_array); //Only prints "1"
print_r($amount); // shows 1,2,3
print_r($amount_array); // shows Array ( [0] => 1,2,3 )
It's a string not an array, you have to split it using explode function:
$exploded = explode ( "," , $amount_array);
var_dump($exploded);
To use the array_sum the string needs to be converted to an array
You need to use the explode function:
$amount_array = explode(',', $amount);
So you total code should be like this:
$amount_array = explode(',', $amount);
echo array_sum($amount_array);
array_sum() works by adding up the values in an array. You only have one key=>value pair in your array: key 0 with a value of 1,2,3.
If you have a comma-separated list, and want that to be an array, I would use the explode() function to turn the list into the proper key=>value pairs that array_sum() would expect.
Try
$amount_array = explode(',',$amount);
You can not initialize an array the way you intend. You are passing in a comma-separated string, which is just a single argument. PHP doesn't automagically convert that string into separate arguments for you.
In order to convert a comma-separated string into an array of individual values you can break up the string with a function like explode(), which takes a delimiter and a string as its arguments, and returns an array of the delimiter-separated values.
$amount_array = explode( ',', $amount ); // now $amount_array is the array you intended
I am using an API that returns a CSV string as the following:
Date,Open,High,Low,Close,Volume,Adj Close 2014-06-13,3.10,3.30,3.10,3.30,6638300,3.30
I was wondering if there is an easy way to convert this CSV string to an associative array? I am familiar with working with a CSV file, but not sure what to do with a CSV file string response. I am trying to avoid writing the response to a file, just so I can read it back.
Note, writing this response to a CSV file would result in two rows and seven columns.
If the line terminator is \r\n, then first you need to explode them thru that. Then you can proces from there. Consider this example:
$data = explode("\r\n", $csv_string);
// get the headers first
$headers = array_map('trim', explode(',', array_shift($data)));
$new_data = array();
// get the values and use array combine to use the headers as keys for associative array
foreach($data as $values) {
$pieces = explode(',', $values);
// i just assigned it in an array, since we dont know how many lines of data you will receive
$new_data[] = array_combine($headers, $pieces);
}
$new_data should yield something like this:
Array
(
[0] => Array
(
[Date] => 2014-06-13
[Open] => 3.10
[High] => 3.30
[Low] => 3.10
[Close] => 3.30
[Volume] => 6638300
[Adj Close] => 3.30
)
)
Or just the usual way of handling csv strings. Use str_getcsv().
$data = str_getcsv($csv_string, "\r\n");
$headers = explode(',', array_shift($data));
$data = array_combine($headers, explode(',',reset($data)));
As others have mentioned, str_getcsv() is your friend and is an easy method to convert a CSV string into an array. This function will return an array of results. If you'd like to make it an associative array, use array_combine().
Honestly, though -- str_getcsv() may be overkill. Consider this method using explode():
// so given this string returned by the API
$api_s = "Date,Open,High,Low,Close,Volume,Adj Close\r\n2014-06-13,3.10,3.30,3.10,3.30,6638300,3.30";
// split on carriage return & line feed into two strings
$api_r = explode("\r\n", $api_s);
// split keys and values by comma
$keys = explode(',', $api_r[0]);
$vals = explode(',', $api_r[1]);
// construct associative array
$my_assoc_r = array_combine($keys, $vals);
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);