[英]replace json string using regex in PHP
How can i replace/remove some string in this JSON ?.I think this problem can be solve using str_replace method or preg_replace but i don't know how to add the regex 我如何替换/删除此JSON中的某些字符串?我认为可以使用str_replace方法或preg_replace解决此问题,但我不知道如何添加正则表达式
Please help me. 请帮我。
here my json 这是我的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
)
will i use this function to export the json to csv 我将使用此功能将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);
the above code work fine but each image link is have one column so looks like bad , i need to make each group of pics on one column I try to add regex to part of link images except the first image url [0] and merge the other with it , that i can putting them together in one column.... so for the experiment i add this to the above code but seems nothing happen 上面的代码可以正常工作,但是每个图像链接只有一列,所以看起来很糟糕,我需要使每一列的图片都位于一列上,我尝试将正则表达式添加到部分链接图像中,除了第一个图像URL [0]并合并其他,我可以将它们放在一列中...。因此,为了进行实验,我将其添加到了上面的代码中,但是似乎没有任何反应
$flat[$k] = str_replace('[1-7] => http', "http", $v);
here i expect the output something like that 在这里我希望输出这样的东西
....
[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
)
.....
edit this the .csv file output look like this 编辑此.csv文件输出如下所示
and I'm looking to be something like that 我希望成为这样的人
ok I've fixed by 好的,我已经固定了
foreach (
new RecursiveArrayIterator($nonFlat) as $k=>$v) {
$flat[$k] = is_array($v)?implode(" ",$v):$v;
now i got each group of images on one column 现在我将每一组图像都放在一列上
thanks :) 谢谢 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.