Jump to content

printing a specific number from an array


php_begins

Recommended Posts

Hello, i retrieve an array from the database that looks something like this:

 

$var=a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}};

 

I need to retrieve the number corresponding to the character i: and print it(11 in the first case). 

For example ,

If,

$var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}};

I would need to print 15.

$var=a:1:{s:2:"cc";a:1:{i:3;s:11:"TESTING";}};

I would need to print 3.

 

Thanks in advance.

Hello, i retrieve an array from the database that looks something like this:

 

$var=a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}};

 

I need to retrieve the number corresponding to the character i: and print it(11 in the first case). 

For example ,

If,

$var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}};

I would need to print 15.

$var=a:1:{s:2:"cc";a:1:{i:3;s:11:"TESTING";}};

I would need to print 3.

 

Thanks in advance.

Heres a simple way, kind of hacky, but it works nonetheless :)

 

<?PHP
  $dbText = '$var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}};';
  $string = explode(';',end(explode('{i:',$dbText,2)),2);
  $number = $string[0];

  echo $number;
?>

 

Regards, PaulRyan.

By using list if the array always has 1 value pair. If not you would need to use a foreach loop:

 

list($key, $value) = $var['bcc'];

 

Foreach:

foreach ($var['bcc'] as $key => $value) {
       echo $key . " " . $value . " " ;
}

 

$key would hold 898 and $value would hold tester. And just for the record, this has nothing to do with regex :)

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.