Jump to content

Really simple: list / array / explode


johnsmith153

Recommended Posts

I want to do this:


$value = "23**44**22**97**46**65";
list($a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8, $a9, $a10, $a11, $a12, $a13, $a14, $a15 ) = explode("**",$value);

$array[]=$a1;
$array[]=$a2;
$array[]=$a3;

//etc...	

 

But there is obviously a better way.

 

Also, there could be one or hundreds of values seperated by the **. (**1**2**3...109**110**111 etc.)

 

Surely this would be the best method (if it was possible):


$value = "23**44**22**97**46**65";

list( convert to $array ) = explode("**",$value);

 

What is the best way?

Link to comment
https://forums.phpfreaks.com/topic/159784-really-simple-list-array-explode/
Share on other sites

simply using

 

<?php
$list = explode("**",$value);

 

will get you what you want. you do not have to predefine the number of array elements prior to the explode.  you can then access the elements either like

 

<?php
echo $list[0];
echo $list[1];

//or

foreach($list as $element) {
    echo $element;
}

 

If this is not what you were looking for, try to explain in more detail

Im sorry i miss understand here. But do you want all those values in multiple arrays? so for every "**" break that value will be put into an array?

 

I wrote this. It works the same as lonewolf

 

$a4 = explode("**",$value);
echo '<pre>';
print_r($a4);
echo '</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.