/    Sign up×
Community /Pin to ProfileBookmark

accessing unnamed array

[code]
$temp = array_splice( $data, $index, 1 );
$update_array[] = $temp[0];
[/code]

I would like to be able to access the ‘0’ index of the array being returned by array_splice without having to use the temporary variable. I would like something like this:

[CODE]
$update_array[] = ${array_splice( $data, $index, 1 )}[0];
[/CODE]

This is obviously not the right code, but I hope you get the drift of what I am trying to do. Thanks for any help you may be able to provide.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 27.2004 — [font=arial]You could use a function.[/font]

[code=php]
function ary_splice($data, $index, $num, $backNum){
$temp = array_splice($data, $index, $num);
$temp = $temp[$backNum];
return ($temp);
}
[/code]


[font=arial]Then your code would look like this...[/font]

[code=php]
$update_array[] = ary_splice($data, $index, 1, 1);
[/code]


[font=arial]In order to make it more dynamic, I've changed it so you can choose what part of $temp (0, 1, 2..[i]n[/i]), you want to return from the function.[/font]
Copy linkTweet thisAlerts:
@mrtwice99authorApr 27.2004 — Jona:

Thank you for your reply. A function is not what I was looking for. I just want to access that unnamed variable without having to do any extra work. It is a little thing really, but I figured someone would know how to do it.
Copy linkTweet thisAlerts:
@JonaApr 27.2004 — [font=arial]That I've ever seen, it's not possible. You can't even shorten an explode function:[/font]

[code=php]
# Doesn't work:
$bomb = explode($needle, $haystack)[0];

# Works:
$bomb = explode($needle, $haystack);
$bomb = $bomb[0];
[/code]


[font=arial]Wish I could help more.[/font]
Copy linkTweet thisAlerts:
@mrtwice99authorApr 27.2004 — It is certainly possible that you can not do it in PHP. It is possible in PERL so I figured I could do it in PHP too. Oh well, back to the drawing board. Thanks for your help.
×

Success!

Help @mrtwice99 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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