Jump to content

How to split data


brooksh

Recommended Posts

http://php.net/manual/en/function.explode.php

 

Explode splits a string into an array e.g.

 

<?php
$string="1&2&3&4&5&6&7";

$array=explode("&",$string);
?>

 

Array will be 7 in lenght containg

 

1

2

3

4

5

6

7

 

so ur example would be

 

<?php
$data = "page=1&data=1:21;2:34;5:57&a=yes";

$array=explode(";",$data):
//array[0] will be 1:21
//array[1] will be 2:34 
//ect u get the idea, it simple from then on.
//just loop though the array do as u wish then use implode to join them back to string if needed.
foreach ($array as $k => $v)
{
// do what u want to the data here 
}

$string=implode($array);

?>

 

Notes on implode

http://php.net/manual/en/function.implode.php ;

 

If u want to remove elements from an array you can use unset($array[4]).

http://php.net/manual/en/function.unset.php

 

 

Link to comment
https://forums.phpfreaks.com/topic/259395-how-to-split-data/#findComment-1329798
Share on other sites

<?php
$data = "page=1&data=1:21;2:34;5:57&a=yes";

$array=explode(";",$data):
//array[0] will be 1:21
//array[1] will be 2:34 
//ect u get the idea, it simple from then on.
//just loop though the array do as u wish then use implode to join them back to string if needed.
foreach ($array as $k => $v)
{
// do what u want to the data here 
}

$string=implode($array);

?>

 

 

$array[0] = page=1&data=1:21;2:34

 

How can I get rid of the page=1&data=

Link to comment
https://forums.phpfreaks.com/topic/259395-how-to-split-data/#findComment-1329903
Share on other sites

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.