将PHP数组字符串转换为数组

时间:2014-11-27 05:57:06

标签: php arrays

如果我执行var_dump(array($output))则显示

array(1) { [0]=> string(573) " __(Developer’s Must Have Gray Cardigan|:89.77,gravityforms), __(Polka Dots Blue Dress|:55.45,gravityforms), __(Classic Brown Leather Bag with Orange Details|:66.84,gravityforms), __(Cycling Pack Steel Blue|:360.00,gravityforms), __(Classic Brown Leather Bag with Orange Details|:,gravityforms), __(The Black Cat Winter Jacket|:254.45,gravityforms), __(Split Slit Gold Threading Cardi|:165.74,gravityforms), __(Slim Fit Pants|:85,gravityforms), __(Get the Complete WordPress Developer Outfit|:65.55,gravityforms), __(Biodiesel Cardigan Dreamcatcher|:175.00,gravityforms)," }

但我想要喜欢这个

array(10) { [0]=> string(44) "Developer’s Must Have Gray Cardigan|:89.77" [1]=> string(28) "Polka Dots Blue Dress|:55.45" [2]=> string(52) "Classic Brown Leather Bag with Orange Details|:66.84" [3]=> string(31) "Cycling Pack Steel Blue|:360.00" [4]=> string(47) "Classic Brown Leather Bag with Orange Details|:" [5]=> string(35) "The Black Cat Winter Jacket|:254.45" [6]=> string(39) "Split Slit Gold Threading Cardi|:165.74" [7]=> string(18) "Slim Fit Pants|:85" [8]=> string(50) "Get the Complete WordPress Developer Outfit|:65.55" [9]=> string(39) "Biodiesel Cardigan Dreamcatcher|:175.00" }

我该怎么做?

2 个答案:

答案 0 :(得分:1)

使用array_push

$outputArr = array();
if ($loop->have_posts()) {
    while ($loop->have_posts()) {
        $loop->the_post();
        $price = get_post_meta(get_the_ID(), '_regular_price', true);
        array_push($outputArr, ' __(' . get_the_title() . '|:' . $price . ',gravityforms)');
    }
}

var_dump($outputArr);

希望这有帮助

如果你想使用这个数组,请进一步制作foreach并执行

答案 1 :(得分:0)

代码

$l = array(
    " __(Developer’s Must Have Gray Cardigan|:89.77,gravityforms), __(Polka Dots Blue Dress|:55.45,gravityforms), __(Classic Brown Leather Bag with Orange Details|:66.84,gravityforms), __(Cycling Pack Steel Blue|:360.00,gravityforms), __(Classic Brown Leather Bag with Orange Details|:,gravityforms), __(The Black Cat Winter Jacket|:254.45,gravityforms), __(Split Slit Gold Threading Cardi|:165.74,gravityforms), __(Slim Fit Pants|:85,gravityforms), __(Get the Complete WordPress Developer Outfit|:65.55,gravityforms), __(Biodiesel Cardigan Dreamcatcher|:175.00,gravityforms),"
);

$m = explode('__', $l[0]);
$n = array();

array_walk($m, function ($x, $y) use (&$n) {
    $w = trim(preg_replace('/,(|\s+)$/', '', $x));
    $n[] = ($w) ? '__'.$w : '';
});

$result = array_slice(array_filter($n), 0);

var_dump($result);

输出

OUTPUT

修改

或者你可以使用这个

$l = array(
    " __(Developer’s Must Have Gray Cardigan|:89.77,gravityforms), __(Polka Dots Blue Dress|:55.45,gravityforms), __(Classic Brown Leather Bag with Orange Details|:66.84,gravityforms), __(Cycling Pack Steel Blue|:360.00,gravityforms), __(Classic Brown Leather Bag with Orange Details|:,gravityforms), __(The Black Cat Winter Jacket|:254.45,gravityforms), __(Split Slit Gold Threading Cardi|:165.74,gravityforms), __(Slim Fit Pants|:85,gravityforms), __(Get the Complete WordPress Developer Outfit|:65.55,gravityforms), __(Biodiesel Cardigan Dreamcatcher|:175.00,gravityforms),"
);

$n = explode(', ', $l[0]);