Welcome Guest, Not a member yet? Register   Sign In
Exploding Array Elements... Using explode TWICE
#1

[eluser]PoetaWD[/eluser]
Hello,

I am having a problem that I think it is simple...

I need to explode TWICE a string....

I am able to explode a string and that returns me a array();

What I need is create a loop to go over each element in the array and EXPLODE it again !

The result would be a array inside a array.

Example:

Code:
$str = 'engine,browser,platform,version,avaliator/name'

//Exploding it
array = explode(',', $str);
That would return me this array...
Code:
array(
[0] => 'engine',
[1] => 'browser',
[2] => 'platform',
[3] => 'version',
[4] => 'avaliator/name'
)

Now I need to explode each element with the '/'. Check the last value of my array.

The result I want would be:

Code:
array(
[0] => 'engine',
[1] => 'browser',
[2] => 'platform',
[3] => 'version',
[4] => array( [0] => 'avaliator'
              [1] => 'name')
)

Thanks for helping me
#2

[eluser]WanWizard[/eluser]
Something like:
Code:
$str = 'engine,browser,platform,version,avaliator/name';
$array = array = explode(',', $str);
foreach ($array as $key => $value)
{
    $array[$key] = explode('/', $value);
}
#3

[eluser]richthegeek[/eluser]
WanWizard's code would result in every element being an array, mostly of length 1. This is probably a good thing though!

Two ways to make it work nicer/differently would be to use either strstr to check for a / before exploding, or array_pad to allow easy testing of element[1] without firing "Undefined var" notices.

Code:
$array = explode( ",", $str );
foreach( $array as $key=>$value ) $array[ $key ] = strpos( $value, "/" ) ? explode( "/", $value ) : $value;

Code:
$array = explode( ",", $str );
foreach( $array as $key=>$value ) $array[ $key ] = array_pad( explode("/",$value), 2, false );
#4

[eluser]PoetaWD[/eluser]
Thanks guys,

@richthegeek : Thanks, I dont understant what you did but....

I did it different:

Code:
foreach($columns as $position => $column)
            {
                $c = explode('/',$column);                
                
                if(count($c) == 1):                
                    
                    $columns[$position] =  $column;    
                
                else:                    
                    
                    $columns[$position] = $c;
                
                endif;
            }

I used count() to check if the array had only one value...

I will study your way to learn more about it ! I am just learning PHP...

THANKS A LOT !!!
#5

[eluser]richthegeek[/eluser]
Sure, I've added comments to each line to explain it a bit better;

Code:
$array = explode( ",", $str );
foreach( $array as $key=>$value )                 // foreach without {} wrap, as it is only one line
    $array[ $key ] = strpos( $value, "/" ) ?        // this form is the "ternary operator" where "return = expression ? if true : if false;"
                explode( "/", $value ) :     // this is if the $value contains a "/", so returns the second explosion
                $value;                // this is when there is no "/", so returns the original value
#6

[eluser]WanWizard[/eluser]
[quote author="richthegeek" date="1272282533"]WanWizard's code would result in every element being an array, mostly of length 1. This is probably a good thing though![/quote]
Indeed. This way your array is consistent.

Otherwise, when you continue processing the result, you always have to test if the array element is a string or an array, and you need more code to process the element.




Theme © iAndrew 2016 - Forum software by © MyBB