tags:

views:

48

answers:

3

Iam having hidden field which contains the value in this format ENO:123,EQNO:231,loc:CHICAGO;ENO:567,EQNO:898,loc:FLORIDA;

In the above line iam having two pair of records separated by ";" and for each record iam having three columns separated by ":" Now How Do i loop through this and put each column in session

A: 
$columns = array();
foreach (explode(',', $_POST['whatever']) as $record) {
  list($key, $value) = explode(':', $record);
  $columns[] = $value;
  }
}

Edited in response to Derby's comment. (Also corrected the separator from '.' to ':')

Colin Fine
@Colin Fine: ENO123,EQNO231,loc CHICAGOENO567,EQNO898,loc FLORIDAIam getting in the above format How do i get the Values only for each column such as 123,231,cHICAGO
Derby
@collinFine:getting Error Undefined offset: 1 with the above edited code
Derby
+1  A: 

Easy. You would have to split the string by ";", then each split again by ",". You can use either split or explode.

// Sample code: 
$foo = "ENO:123,EQNO:231,loc:CHICAGO;ENO:567,EQNO:898,loc:FLORIDA;";
$arr = split(";", $foo);
$column1 = split(",", $arr[0]);
$column2 = split(",", $arr[1]);

// loop
foreach($column1 as $col){
  // do something
}
ToadyMores
A: 

If you are interested in the values only as you said in one comment, you could try this function (tested under PHP 5.2):

<?php

header('Content-Type: text/plain');

$str = 'ENO:123,EQNO:231,loc:CHICAGO;ENO:567,EQNO:898,loc:FLORIDA;';

function parseStrToSets(&$out, $str, array $separators, $idx = 0)
{
    $chunks = explode($separators[$idx], trim($str, $separators[$idx]));

    if(!isset($separators[$idx + 1])) return $out = $chunks[1];

    foreach($chunks as $key => $chunk) 
        parseStrToSets($out[$key], $chunk, $separators, $idx + 1);  
}

$out = array();
parseStrToSets($out, $str, array(';', ',', ':'));
print_r($out);

Gives:

Array
(
    [0] => Array
        (
            [0] => 123
            [1] => 231
            [2] => CHICAGO
        )

    [1] => Array
        (
            [0] => 567
            [1] => 898
            [2] => FLORIDA
        )

)
Max
@MAx: In this $out Array How do i get Each individual Item to Session
Derby
@MAx:I mean $_SESSION['STUNBR'] = out[0][0] which will get me 123 .How do i do this
Derby
What is STUNBR?
Mike C
@Derby If you want to store the array in the session, call sesstion_start() at the very top of your file, then just put the array in a session Variable like $_SESSION['myKey'] = $out; Read more about sessions at: http://www.php.net/manual/en/session.examples.basic.php
Max