This question already has answers here:
PHP function to build query string from array
(4 answers)
Closed 7 months ago.
How can I convert this array to query string in PHP? (I'm new to PHP), I'm passing multiple values for the filter $merchantFilter
print_r($merchantFilter)
gives:
Array ( [0] => Diesel Monkeys [1] => Gas Monkeys )
I'm trying to construct the URL like:
&merchantFilter[]=Diesel+Monkeys&merchantFilter[]=Gas+Monkeys
The variable that constructs the query is currently:
$HREF .= "merchantFilter=".urlencode($merchantFilter)."&";
How can I convert this to construct the array?
You can use http_build_query:
$merchantFilter= Array ( 'Diesel Monkeys', 'Gas Monkeys' );
echo http_build_query(array('merchantFilter' => $merchantFilter));
Output:
merchantFilter%5B0%5D=Diesel+Monkeys&merchantFilter%5B1%5D=Gas+Monkeys
Note the result is already urlencoded so needs no further processing.
Related
This question already has answers here:
Query with GROUP_CONCAT not working with PHP
(1 answer)
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 3 years ago.
How do i print array values from this query ?
I need something like 6475,7377,6367. (comma separated).
This is what i get when i do a print_r($myarray):
Array
(
[0] => Array
(
[gift_product] => 6475
)
[1] => Array
(
[gift_product] => 7377
)
[2] => Array
(
[gift_product] => 6367
)
)
Thanks alot!
You could have handled this on the MySQL side using GROUP_CONCAT, something like this:
SELECT GROUP_CONCAT(gift_product) AS products
FROM yourTable
GROUP BY id;
Your current output indicates that you are getting back gift_product values over several records, when in fact you wanted to retrieve a CSV string of products.
You can use implode function after you map your array using array_map:
echo implode(', ', array_map(function ($entry) {
return $entry['gift_product'];
}, $myarray))
Use array_column to fetch column wise data with implode,
echo implode(",", array_column($arr, "gift_product"));
Demo
Output
6475,7377,6367
This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 3 years ago.
I'm trying to slice a multi-array in php. I'm using google's geo-location api and I've managed to get it to return lat,lng and accuracy. I've used
$message = json_decode($result, true);
To convert the Json into an array.
The problem I have is that I have the results in this form:
Array ( [location] => Array ( [lat] => 10.1453652 [lng] => 0.2338764 ) [accuracy] => 33 )
and I can't figure out how to extract the lat and lon. Can anyone help?
Try the following code:
echo $message['location']['lat'];
It's simple, if not what you are expected, its straight forward
echo $message['location']['lat'].' '.$message['location']['lng'];
Working demo.
This question already has answers here:
How can I add all of my array values together in PHP?
(4 answers)
Closed 3 years ago.
I have an array and I want to sum up the values of the array such as [0][1][2] and assign the result of the sum to a variable
My array
Array ( [0] => 1 [1] => 2 [2] => 0 )
Can anyone provide me a solution that would be really helpful?
Use php array_sum() function.
$val = array_sum($yourArray);
It will sum up all of your array elements into a variable.
This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to explode URL parameter list string into paired [key] => [value] Array?
Quick question
I have a URL sting that I wish to output the Key value as an array eg
$url ="www.domain.com?test=1&test2=2&test3=3";
and wish to have the output as an array
key => value so I can call any of the keys
eg
array (
test => 1,
test1 => 2,
test2 => 3,
)
cant use explode &
Just thinking do i have to do a loop and match between & and = for the key
I would use parse_url() with parse_str():
$url = parse_url('www.domain.com?test=1&test2=2&test3=3');
parse_str($url['query'], $keyvalue);
var_dump($keyvalue);
$keyvalue should contain your desired array.