Jump to content

How to split data


brooksh

Recommended Posts

How can I split this data and then remove it? I need to split data by ; then split each one of those.

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

 

So I will get:

 

$category[0] = 1;

$sub_category[0] = 21;

$category[1] = 2;

$sub_category[1] = 34;

$category[2] = 5;

$sub_category[2] = 57;

 

 

Link to comment
Share on other sites

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
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
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.