/    Sign up×
Community /Pin to ProfileBookmark

WHat’s The Array Called That Explode Dumps The Data ?

[code]
<?php

$url = ‘http://www.google.com/?search=cars’;

print_r(explode(‘?’,$url));

?>
[/code]

It’s obvious, the explode divided the url into 2 parts. One part of data before the “?” and the other part after the “?”. And then it dumped the two data onto an array and that’s why we need to use print_r to check the two data and cannot just use echo to check the two data. But what exactly is the array called where the two data got dumped ?

You see, usually we know what the array is called because we assign a named variable to it.
I mean, usually when we create arrays we name it with a variable like so:

$arr = array();

Then we dump data onto it like so:

$arr[] = ‘blah;’

And so, we know what the array is called. In this case $arr.

But in the following case, what is the array called where the 2 data (2 parts of the url) got dumped ? What variable did php assign to the array ?
I mean, php did auto create, in the background an array, and dump the two data there. Or, how else can we print_r(explode) ? afterall, you view the data (values) of array with print_r as you cannot with echo without doing a loop.

[code]
<?php

$url = ‘http://www.google.com/?search=cars’;

print_r(explode(‘?’,$url));

?>
[/code]

If I can know the array’s var name then I can loop like this:

[code]
<?php

foreach($array_var AS $value)
{
echo $value;
}

?>
[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorSep 21.2021 — Folks,

Q2.

Why didn't this work ?
<i>
</i>$url = 'http://www.google.com/?search=cars';

$array_var = array();
$array_var[] = explode('?',$url);

foreach($array_var AS $value)
{
echo $value; //LINE 25
}


Shows error:

**Notice: Array to string conversion in ..... test.php on line 25

Array**


Don't forget to also answer my Q1 from my previous post.
Copy linkTweet thisAlerts:
@NogDogSep 21.2021 — Instead of...
[code=php]
$array_var = array();
$array_var[] = explode('?',$url);
[/code]

...just do...
[code=php]
$array_var = explode('?',$url);
[/code]
Copy linkTweet thisAlerts:
@ginerjmSep 21.2021 — For your first question - Why do you think that there is an array name? Do you see one being assigned anywhere?
Copy linkTweet thisAlerts:
@developer_webauthorSep 21.2021 — @ginerjm#1637348

The explode() I suspect dumps exploded data to an array in the background. or, why else do you need to print_r to see the exploded data ? You use print_r to view array values.

That's why.

Now what's the answer to my Q2 ?
Copy linkTweet thisAlerts:
@ginerjmSep 21.2021 — It doesn't dump it anywhere. It just does something for you and when you try to output the result it can't. A programmer would know that.

Q2 belongs in another post.
Copy linkTweet thisAlerts:
@developer_webauthorSep 21.2021 — @NogDog#1637343

<i>
</i>$url = 'http://www.google.com/?search=cars';
$array_var = explode('?',$url);

foreach($array_var AS $AV)
{
echo $AV; echo '&lt;br&gt;';
}


Working as expected.

Cheers!
Copy linkTweet thisAlerts:
@developer_webauthorSep 21.2021 — @ginerjm#1637353

NogDog helped me fix Q2.

See above.
×

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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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