如何使用爆炸?

发布于 2024-12-17 18:28:55 字数 1217 浏览 3 评论 0原文

所以我有这个 php:

<?php
if (isset($_COOKIE["currentsearchctrl"])) {
    $cookz = $_COOKIE["currentsearchctrl"];
    $data = explode(';', $cookz, -1);
    $newdata = array();

    foreach($data as $value){
        $newdata[] = explode(':', $value, -1);
    }    
    print_r($newdata);

} else { }
?>

php 正在读取的 cookie:

%20Rod%20Stewart%20%3A%2059088763306%3B%20Led%20Zeppelin%20Official%20%3A%20131572223581 891%3B%20Lynrd%20Skynyrd%20%3A%206983887641%3B%20Black%20Sabbath%20%3A%2056848544614%3B

但“print_r”正在输出:

Array ( [0] => Array ( [0] => Rod Stewart ) [1] => Array ( [0] => Led Zeppelin Official ) [2] => Array ( [0] => Lynyrd Skynyrd ) [3] => Array ( [0] => Black Sabbath ) )

“id”丢失。我需要它。 Array([0] => Array ([0] => 罗德·斯图尔特, [1] => 1200420 ) [1] => Array ( [0] => 齐柏林飞船官方, [1] => ; 110024294 )...

为了提供帮助,这是 print_r($data) 的输出:

Array ( [0] => Rod Stewart : 59088763306 [1] => Led Zeppelin Official : 131572223581891 [2] => Lynyrd Skynyrd : 6983887641 [3] => Black Sabbath : 56848544614 )

有人可以帮忙吗?

So i have this php:

<?php
if (isset($_COOKIE["currentsearchctrl"])) {
    $cookz = $_COOKIE["currentsearchctrl"];
    $data = explode(';', $cookz, -1);
    $newdata = array();

    foreach($data as $value){
        $newdata[] = explode(':', $value, -1);
    }    
    print_r($newdata);

} else { }
?>

The cookie that is being read by php:

%20Rod%20Stewart%20%3A%2059088763306%3B%20Led%20Zeppelin%20Official%20%3A%20131572223581891%3B%20Lynyrd%20Skynyrd%20%3A%206983887641%3B%20Black%20Sabbath%20%3A%2056848544614%3B

but the 'print_r' is outputting:

Array ( [0] => Array ( [0] => Rod Stewart ) [1] => Array ( [0] => Led Zeppelin Official ) [2] => Array ( [0] => Lynyrd Skynyrd ) [3] => Array ( [0] => Black Sabbath ) )

The 'id' is missing. I needed it to be. Array([0] => Array ([0] => Rod Stewart, [1] => 1200420 ) [1] => Array ( [0] => Led Zeppelin Official, [1] => 110024294 )...

To help, this is the output of print_r($data):

Array ( [0] => Rod Stewart : 59088763306 [1] => Led Zeppelin Official : 131572223581891 [2] => Lynyrd Skynyrd : 6983887641 [3] => Black Sabbath : 56848544614 )

Could someone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木落 2024-12-24 18:28:55

我认为这就是你想要做的:

$data = array (0 => "Rod Stewart : 59088763306", 1 => "Led Zeppelin Official : 131572223581891", 2 => "Lynyrd Skynyrd : 6983887641", 3 => "Black Sabbath : 56848544614" );


$data = array_map(function($item){
    list($artist, $id) = explode(' : ', $item);
    return array('id'=>$id, 'artist'=>$artist);
}, $data);

print_r($data);

输出

Array
(
    [0] => Array
        (
            [id] => 59088763306
            [artist] => Rod Stewart
        )

    [1] => Array
        (
            [id] => 131572223581891
            [artist] => Led Zeppelin Official
        )

    [2] => Array
        (
            [id] => 6983887641
            [artist] => Lynyrd Skynyrd
        )

    [3] => Array
        (
            [id] => 56848544614
            [artist] => Black Sabbath
        )

)

I think this is what you're trying to do:

$data = array (0 => "Rod Stewart : 59088763306", 1 => "Led Zeppelin Official : 131572223581891", 2 => "Lynyrd Skynyrd : 6983887641", 3 => "Black Sabbath : 56848544614" );


$data = array_map(function($item){
    list($artist, $id) = explode(' : ', $item);
    return array('id'=>$id, 'artist'=>$artist);
}, $data);

print_r($data);

Output

Array
(
    [0] => Array
        (
            [id] => 59088763306
            [artist] => Rod Stewart
        )

    [1] => Array
        (
            [id] => 131572223581891
            [artist] => Led Zeppelin Official
        )

    [2] => Array
        (
            [id] => 6983887641
            [artist] => Lynyrd Skynyrd
        )

    [3] => Array
        (
            [id] => 56848544614
            [artist] => Black Sabbath
        )

)
不爱素颜 2024-12-24 18:28:55

为什么不尝试使用正则表达式呢?

<?php
if (isset($_COOKIE["currentsearchctrl"])) {
    $cookz = urldecode($_COOKIE["currentsearchctrl"]);
    preg_match_all("#(?P<artist>(?:\w|\s)+)\s:\s(?P<id>\d+)#", $cookz, $matchs, PREG_SET_ORDER);    
    print_r($matchs);
} else { }
?>

编辑:抱歉,我没有注意到名称不完整

示例:preg_match_all

Why don't you try with regular expressions?

<?php
if (isset($_COOKIE["currentsearchctrl"])) {
    $cookz = urldecode($_COOKIE["currentsearchctrl"]);
    preg_match_all("#(?P<artist>(?:\w|\s)+)\s:\s(?P<id>\d+)#", $cookz, $matchs, PREG_SET_ORDER);    
    print_r($matchs);
} else { }
?>

EDIT: Sorry I didn't notice the name wasn't complete

Example: preg_match_all

花之痕靓丽 2024-12-24 18:28:55

使用serialize()和unserialize()

$data = serialize($cookz);

序列化字符串可确保将精确的结构存储到字符串中以便稍后恢复。

use serialize() and unserialize()

$data = serialize($cookz);

Serializing string ensures that you store exact structire into the string to be later recovered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文