/    Sign up×
Community /Pin to ProfileBookmark

How To Echo Keys Alongside Values Using parse_str() ?

Hi,

Ok. To begin with this thread, I am using the codes found on:
https://forum.webdeveloper.com/d/399940-parse-str-how-to-echo-the-keys-or-var-names-rather-than-var-values
But this is a different topic altogether and so don’t get confused with the other thread.

Look at these 2 codes. They do the same thing in 2 different ways. They extract the values from the query part of the string and print on your screen.

1.

[code]
$url = ‘https://www.username:[email protected]:8080/dir/subdir/file.php?arg=value1&key=value2#fragment’;

echo $all_queries_and_values = parse_url($url,PHP_URL_QUERY); echo ‘<br>’;

parse_str($all_queries_and_values,$arr1);

print_r($arr1); echo ‘<br>’; //DEVMODE.

echo $arr1[‘arg’]; echo ‘<br>’; //Echoes the Value. Not the Key.
echo $arr1[‘key’]; echo ‘<br>’; //Echoes the Value. Not the Key.
[/code]

2.

[code]
$url = ‘https://www.username:[email protected]:8080/dir/subdir/file.php?arg=value1&key=value2#fragment’;

echo $all_queries_and_values = parse_url($url,PHP_URL_QUERY); echo ‘<br>’;

parse_str($all_queries_and_values,$arr2);

print_r($arr2); echo ‘<br>’; //DEVMODE.
var_dump($arr2); echo ‘<br>’; //DEVMODE.

foreach($arr2 AS $single_query_value)
{
echo $single_query_value; echo ‘<br>’; //Echoes the Value. Not the Key.
}
[/code]

I now want to print on your screen not only the values (like shown on the above 2 codes) but the keys alongside the values like so:

[code]
key=>value
[/code]

How to do this ?
These ain’t working …

**The difference between the below 3 codes are these ….
key=>value
“key”=>”value”
key->value**

[code]
$url = ‘https://www.username:[email protected]:8080/dir/subdir/file.php?arg=value1&key=value2#fragment’;

echo $all_queries_and_values = parse_url($url,PHP_URL_QUERY); echo ‘<br>’;

parse_str($all_queries_and_values,$arr2);

print_r($arr2); echo ‘<br>’; //DEVMODE.
var_dump($arr2); echo ‘<br>’; //DEVMODE.

foreach($arr2 AS $single_query_key=>$single_query_value)
{
echo $single_query_key=>$single_query_value; echo ‘<br>’; //I want it to echo both the Keys and the Values. Not the Key.
}
[/code]

**Parse error: syntax error, unexpected token “=>”, expecting “,” or “;” in C:xampphtdocsWorkgulfTemplatesparse_string_TEMPLATE.php on line 70**

Neither this working …
**_(Note the comment at the bottom) …_**

[code]
$url = ‘https://www.username:[email protected]:8080/dir/subdir/file.php?arg=value1&key=value2#fragment’;

echo $all_queries_and_values = parse_url($url,PHP_URL_QUERY); echo ‘<br>’;

parse_str($all_queries_and_values,$arr2);

print_r($arr2); echo ‘<br>’; //DEVMODE.
var_dump($arr2); echo ‘<br>’; //DEVMODE.

foreach($arr2 AS “”$single_query_key””=>””$single_query_value””)
{
echo “$single_query_key”=>”$single_query_value”; echo ‘<br>’; //I want it to echo both the Keys and the Values. Not the Key.
}
[/code]

**Parse error: syntax error, unexpected token “=>”, expecting “->” or “?->” or “{” or “[” in C:xampphtdocsWorkgulfTemplatesparse_string_TEMPLATE.php on line 90**

Nope! Fail too ….

[code]
echo $all_queries_and_values = parse_url($url,PHP_URL_QUERY); echo ‘<br>’;

parse_str($all_queries_and_values,$arr2);

print_r($arr2); echo ‘<br>’; //DEVMODE.
var_dump($arr2); echo ‘<br>’; //DEVMODE.

foreach($arr2 AS “$single_query_key”->”$single_query_value”)
{
echo “$single_query_key”->”$single_query_value”; echo ‘<br>’; //Echoes the Value. Not the Key.
}
[/code]

**Parse error: syntax error, unexpected double-quote mark, expecting identifier or variable or “{” or “$” in C:xampphtdocsWorkgulfTemplatesparse_string_TEMPLATE.php on line 110**

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 02.2022 — [code=php]
// The "=>" is a PHP operator in this usage
foreach($arr2 AS $single_query_key => $single_query_value)
{
// the literal "=>" is just text output here, so needs to be within the string
echo "$single_query_key => $single_query_value"; echo '<br>';
}
[/code]
Copy linkTweet thisAlerts:
@novice2022authorJun 02.2022 — @NogDog#1644444

Thanks. I thought of this before but did not test it thinking it's wrong.
Copy linkTweet thisAlerts:
@novice2022authorJun 02.2022 — @NogDog

You may close this thread.
Copy linkTweet thisAlerts:
@Steve_R_JonesmoderatorJun 02.2022 — {"locked":true}
×

Success!

Help @novice2022 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...