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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.