/    Sign up×
Community /Pin to ProfileBookmark

How To Make Associative Array Into Numerical Array ?

Hello,

Is it not true that, parse_url() extract all the properties of a url and save each property into array ?
By properties I talking about:
scheme
host
port
user
pass
path
query
fragment

I reckon, the array actually looks like this:

[“0″=>”scheme”,”1″=>”host”,”2″=>”port”,”3″=>”user”,”4″=>”pass”,”5″=>”path”,”6″->”query”,”7″=>”fragment”]

True or not ? Tell me this!
Anyway, I now adding all this array to $url_properties = array();
To echo each value of the array. Like this ofcourse …

““
<?php

$url = ‘http://user:[email protected]/path.php?query_string=value#fragment1’;

print_r(parse_url($url)); echo ‘<br>’;
echo ‘<br>’;
var_dump(parse_url($url)); echo ‘<br>’;

$url_properties = array();
$url_properties = parse_url($url);

echo $url_properties[0];
echo $url_properties[1];
echo $url_properties[2];
echo $url_properties[3];
echo $url_properties[4];
echo $url_properties[5];
echo $url_properties[6];
echo $url_properties[7];

?>
““

But I get error:
**Notice: Undefined offset: 0 in C:xampphtdocs...php on line 12
Notice: Undefined offset: 1 in C:xampphtdocs...php on line 13
Notice: Undefined offset: 2 in C:xampphtdocs...php on line 14
Notice: Undefined offset: 3 in C:xampphtdocs...php on line 15
Notice: Undefined offset: 4 in C:xampphtdocs...php on line 16
Notice: Undefined offset: 5 in C:xampphtdocs...php on line 17
Notice: Undefined offset: 6 in C:xampphtdocs...php on line 18
Notice: Undefined offset: 7 in C:xampphtdocs...php on line 19**

Why is this ?

This working fine, though….

““
<?php

$url = ‘http://user:[email protected]/path.php?query_string=value#fragment1’;

print_r(parse_url($url)); echo ‘<br>’;
echo ‘<br>’;
var_dump(parse_url($url)); echo ‘<br>’;

$url_properties = array();
$url_properties = parse_url($url);

foreach($url_properties AS $property)
{
echo $property;
}

?>
““

I get echoed:
**httpdomain.comuserpassword/path.phpquery_string=valuefragment1**

This is proof that the $url_properties array is not empty.
So why the error on my first code when trying to echo each of the value of this array ? Strange!

EDIT:
I have figured my error. The array is associative array and so this is working:

““
echo $url_properties[‘scheme’];
echo $url_properties[‘user’];
echo $url_properties[‘pass’];
echo $url_properties[‘host’];
echo $url_properties[‘port’];
echo $url_properties[‘path’];
echo $url_properties[‘query’];
echo $url_properties[‘fragment’];
““

But what few lines of code should I add so that even the following works and does not give undefined offset error ?

““
echo $url_properties[0];
echo $url_properties[1];
echo $url_properties[2];
echo $url_properties[3];
echo $url_properties[4];
echo $url_properties[5];
echo $url_properties[6];
echo $url_properties[7];
““

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmSep 18.2021 — You could just use a foreach to loop thru the array without referencing the key values.

<i>
</i>foreach ($ar as $k=&gt;$v)
echo "$v&lt;br&gt;";
Copy linkTweet thisAlerts:
@ginerjmSep 18.2021 — But if you really want to remove the keys from your array, you could read up in the official PHP manual under 'array' and see the great list of functions there. Check out "array_values".
Copy linkTweet thisAlerts:
@NogDogSep 18.2021 — But if you really want to be a good programmer, use the associative array. That way your code is explicit and self-documenting, and when you have to maintain it a year from now, it's obvious what it's doing. Saving a few keystrokes when you first write your code is a false economy that will bite you in the butt later.
Copy linkTweet thisAlerts:
@JabajabaSep 20.2021 — Use foreach for the loop
Copy linkTweet thisAlerts:
@ginerjmSep 20.2021 — @Jabajaba#1637266 As was posted 2 days ago....
Copy linkTweet thisAlerts:
@developer_webauthorSep 21.2021 — @ginerjm#1637202

Wallah!

<i>
</i>&lt;?php

$url = 'http://username:[email protected]:8080/path.php?query_string=value#fragment1';

$url_properties = array_values(parse_url($url));

echo $url_properties[0]; echo '&lt;br&gt;';
echo $url_properties[1]; echo '&lt;br&gt;';
echo $url_properties[2]; echo '&lt;br&gt;';
echo $url_properties[3]; echo '&lt;br&gt;';
echo $url_properties[4]; echo '&lt;br&gt;';
echo $url_properties[5]; echo '&lt;br&gt;';
echo $url_properties[6]; echo '&lt;br&gt;';
echo $url_properties[7]; echo '&lt;br&gt;';

?&gt;

&lt;?php
echo 'LINE: '.__LINE__; echo '&lt;br&gt;';
echo '&lt;br&gt;';
?&gt;


Now Ginerjm mate, do you mind helping me out here ?

https://www.webdeveloper.com/d/396711-http-build-query-with-encodings
Copy linkTweet thisAlerts:
@developer_webauthorSep 21.2021 — @Jabajaba#1637266

Now Jabajab mate, do you mind helping me out here ?

https://www.webdeveloper.com/d/396711-http-build-query-with-encodings
Copy linkTweet thisAlerts:
@developer_webauthorSep 21.2021 — @NogDog#1637208

Now NogDog mate, do you mind helping me out here as I really think you've got the expertise to put me in the right direction, like you did here ?

https://www.webdeveloper.com/d/396711-http-build-query-with-encodings
Copy linkTweet thisAlerts:
@ginerjmSep 21.2021 — Its not something that I EVER see a need to do, so no - I will not attempt to help you. Let it be your own personal learning experience. You seem to find many many things to puzzle you so I'll let you continue on your journey on your own here.

AND - what you are doing here with these last 3 posts of yours goes against the policy of this forum and probably any other forum. If I wanted to contribute to this 'other' posting of yours I would do so with a post on that topic. I resent your using this avenue to try to convince me (and others) to help you with another of your many dis-jointed topics.

Btw - do you ever accomplish anything with these things you bring up?
Copy linkTweet thisAlerts:
@developer_webauthorSep 22.2021 — @ginerjm#1637321

Yes, I do.

I learn how to deal with data.

Like user inputs.

In past, built membership site (reg, login, logout, member acc homepage, search page, pagination page, etc.).

Now trying to build searchengine. Spider/bot/crawler.

Latest threads not regarding bot.

Regarding security in user inputs. raw_urlencode(),urlencode(), htmspecialchars(), htmlentities(), intval() and the like.

Nowadays fascinated by arrays. My weakpoint.

Also trying to learn shorter ways to cut-down on coding. Hence, these latest threads. DRY stuff.

That thread:

https://www.webdeveloper.com/d/396711-http-build-query-with-encodings

I asked you to help, will teach me DRY stuff to build pagination section. I know how to build without adding encodings and without DRY stuff. Now trying to learn to add these security things and DRY stuffs. Make code shorter. Understand ?
×

Success!

Help @developer_web 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 4.27,
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,
)...