Key value in PHP from URL [duplicate] - php

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.

Related

How to get first key that is dynamic in multidimentional array in php? [duplicate]

This question already has answers here:
Get first key in a (possibly) associative array?
(25 answers)
Closed 2 years ago.
I have a multidimensional array and I want the first key that is
dynamic. How do I get that in PHP?
I want to display the customer name in frontend. But as you can see in the first
array there is the number [12345] => account number (the dynamic part). So if i
want to display customer name how can I get this with this dynamic
account number.
Array
(
[12345] => Array(
['customername'] => ABC
['customerid'] => 456
)
)
Try with Foreach
foreach($array as $value){
echo $value['customername'].'<br>';
echo $value['customerid'].'<br>';
}

How to echo the first key value of an associative array without knowing the name of the key [duplicate]

This question already has answers here:
Get first key in a (possibly) associative array?
(25 answers)
Closed 2 years ago.
print_r of an array gives me this output:
Array (
[id-1.txt] => uploads/id-1.jpg
[id-2.txt] => uploads/id-2.jpg
[id-3.txt] => uploads/id-3.jpg
)
How can i echo always the first key value ( so uploads/id-1.jpg in this case) without knowing the name of the key? Because the name of the keys are changing everytime...
So: catch always the first value which belongs to the first key, whatever the name of the key is...
<?php
$data =
[
'id-1.txt' => 'uploads/id-1.jpg',
'id-2.txt' => 'uploads/id-2.jpg',
'id-3.txt' => 'uploads/id-3.jpg'
];
$copy = $data;
echo reset($copy);
unset($copy);
Output:
uploads/id-1.jpg
Here using a copy for no side effects.

parse_url PHP_URL_QUERY get a l value [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 3 years ago.
with the URL
domain.com/index.php?option=component&ctrl=product&task=show&cid=5076
$parse = parse_url( $full_url, PHP_URL_QUERY);
echo $parse['ctrl']; // I get nothing
How can I get my values from keys (option, ctrl, task, and cid)?
You can use parse_str along with parse_url,
$full_url = "domain.com/index.php?option=component&ctrl=product&task=show&cid=5076";
parse_str(parse_url($full_url,PHP_URL_QUERY), $arr); // parse query string
print_r($arr);
Demo
Output
Array
(
[option] => component
[ctrl] => product
[task] => show
[cid] => 5076
)

Need to extract words from string in php [duplicate]

This question already has answers here:
Extract words from string with preg_match_all
(7 answers)
Closed 8 years ago.
I need to extract the names from the following string:
$contact = "John96783819Dickson97863424"
i tried using this:
preg_match('/[a-zA-Z]/',$contact,$matches);
but i get an array with all the alphabets individually in the array.
Desired Output:
Array ([0] => 'John', [2] => 'Dickson')
And now it gets complicated. The same reggae should extract this
$contact = 'Vincent Tan96123179Lawrence Thoo90603123Ryan Ong91235721'
into this
Array ([0] => 'Vincent Tan', [2] => 'Lawrance Thoo' , [3] => 'Ryan Ong')
How do i do that?
All you need is to quantify the character class using +
/[a-zA-Z]+/
+ matches one occurence of presceding regex
Example : http://regex101.com/r/bI6aH1/1
preg_match_all('/[a-zA-Z]+/',$contact,$matches);
Will give output as
Array ( [0] => John [1] => Dickson )
preg_match('/[a-zA-Z]+/',$contact,$matches);
The /[a-zA-Z]/ means match any ONE letter, anywhere in the string. Adding the + in /[a-zA-Z]+/ means match one or MORE sequential letters.

Take value from array which is in array [duplicate]

This question already has answers here:
PHP - Accessing Multidimensional Array Values
(4 answers)
Closed 8 years ago.
I've got this url:
http://web.com/script.php?identifiers%5Bmc%5D%5Bnick%5D=name1&identifiers%5Bcs%5D%5Bnick%5D=name2&identifiers%5Bcs%5D%5Bpassword%5D=mypass
so i will get array like this:
[identifiers] => Array
(
[mc] => Array
(
[nick] => name1
)
[cs] => Array
(
[nick] => name2
[password] => mypass
)
)
How do I take value name1 and put into variable $mc_name?
That's a simple array containing another array so you can simply specify multiple indexes for included array:
$mc_name = $_GET['identifiers']['mc']['nick'];
To better understand how it works think of it like assigning each array first to a variable like:
$identifiers = $_GET['identifiers'];
$mc_array = $identifiers['mc'];
$mc_name = $mc_array['nick'];
which will essentially do the same thing at once, without the need to specify multiple variables and arrays.
Start with:
identifiers = $_GET['identifiers']
If you know the key names, then simply:
$mc_name = $identifiers['mc']['nick']
If you know it's the first value or the first value, then you can:
$mc_name = array_shift($identifiers); // get the 'mc' array
$mc_name = array_shift($identifiers); // get the 'nick' value
Not that array_shift will actually remove the elements from the original array.

Categories