如何替换/删除此JSON中的某些字符串?。我认为可以使用str_replace方法或preg_replace解决此问题 但我不知道如何添加正则表达式
请帮帮我。
这里是我的json
Array
(
[data] => Array
(
[0] => Array
(
[DESC] => bla bal bal
[SOLD] => 0
[contact_no] => 1234
[title] => Hiiiii
[price] => 10900
[big_image] => Array
(
[0] => http://example.com/images/user_adv/14.jpg
[1] => http://example.com/images/user_adv/15.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/14.jpg
[1] => http://example.com/images/user_adv/small/15.jpg
)
[tpe] => user
)
[1] => Array
(
[DESC] => fo fo fof ofof
[SOLD] => 0
[contact_no] => 234522
[title] => Hellooooo sddf
[price] => 0
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
[1] => http://example.com/images/user_adv/144.jpg
[2] => http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
[1] => http://example.com/images/user_adv/small/144.jpg
[2] => http://example.com/images/user_adv/small/147.jpg
)
[tpe] => user
)
)
[pis] => 3
[totals] => 23
[curpage] => 1
[total_ads] => 71
)
我将使用此函数将json导出到csv
function array_flatten ($nonFlat) {
$flat = array();
foreach (new RecursiveIteratorIterator(
new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
$flat[$k] = $v;
}
return $flat;
}
$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
fputcsv($fp, array_flatten($fields));
}
fclose($fp);
上面的代码工作正常但每个图片链接都有一列所以看起来很糟糕,我需要在一列上制作每组图片 我尝试将正则表达式添加到链接图像的一部分,除了第一个图像url [0]并将另一个与其合并,我可以将它们放在一列中.... 所以对于实验我把它添加到上面的代码但似乎没有发生
$flat[$k] = str_replace('[1-7] => http', "http", $v);
这里我希望输出类似
....
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
http://example.com/images/user_adv/144.jpg
http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
http://example.com/images/user_adv/small/144.jpg
http://example.com/images/user_adv/small/147.jpg
)
.....
编辑此.csv文件输出如下所示
我希望有类似的东西
答案 0 :(得分:1)
好的我已经修复了
foreach (
new RecursiveArrayIterator($nonFlat) as $k=>$v) {
$flat[$k] = is_array($v)?implode(" ",$v):$v;
现在我在一列上得到了每组图像
谢谢:)