Jump to content

[SOLVED] accessing the array value witout putting it in another variable


Recommended Posts

Hi,

 

I'm looking for this syntax.

I wanna explode this string, and I need like the 3rd value on the array :

 

<?php

$a = 'The text must be exploded';

$exploded = explode(' ',$a);

echo $exploded[2];
?>

 

I wanna know, is it possible I echo it without putting it in the $exploded?

I mean directly, something like :

 

echo  explode(' ',$a)[2];

not that i'm aware of...is there a specific reason? this doesn't really help, but it's another trick:

<?php
$a = 'The text must be exploded';
list(,$value) = explode(' ',$a);
echo $value; //prints 'text'
?>

No big reason for that.

I don't need that variable anywhere in the script, so I was wondering about not making it at all.

Personally I love scripts as short as possible.

 

Honestly clean code is nice, but easy to read code is better. I tend to keep my code where people can know what is going on if someone is coming in behind me or to help me. Memory is abundant, so you might as well use it, especially with a simple thing as a variable or an array of variables.

 

The list is good for what you want, but say it explodes 50 characters and you never know which one you want (IE it is not always the second one). The array would be much easier/better imo for that situation.

 

It all depends on your ultimate goal.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.