爆炸两次 [英] Exploding a string twice

查看:86
本文介绍了爆炸两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样组成的字符串:

I have a string composed like this:

87||1|nuovo#88||4|#209|||#89|||#41||1|#5|||#3||1|116#20|||#13||3|#148|||

模式是:
Id1|Mq1|Q.ta1|Tipo1#Id2|Mq2|Q.ta2|Tipo2#Id3|Mq3|Q.ta3|Tipo3 ,依此类推...

The pattern is:
Id1|Mq1|Q.ta1|Tipo1#Id2|Mq2|Q.ta2|Tipo2#Id3|Mq3|Q.ta3|Tipo3 and so on...

基本上每个项目都有4个以"|"分隔的属性每个项目都以#"分隔

Basically each item has 4 attributes separated by "|" and each item is separated by "#"

我需要将字符串分解为每个项目的属性具有单个变量.

I'd need to explode the string to have single variables for each item's attributes.

现在我在这里:

<?php
$str = "87||1|nuovo#88||4|#209|||#89|||#41||1|#5|||#3||1|116#20|||#13||3|#148|||#36|||91#29|||68";
$caratteristica = print_r (explode("#",$str));
?>

哪个给了我这个结果:

Array ( [0] => 87||1|nuovo [1] => 88||4| [2] => 209||| [3] => 89||| [4] => 41||1| [5] => 5||| [6] => 3||1|116 [7] => 20||| [8] => 13||3| [9] => 148||| [10] => 36|||91 [11] => 29|||68 ) 

此数组的每个元素都需要四个变量
(类似$id[], $mq[],$qt[],$tipo[]).

I'd need four variables for each element of this array
( something like $id[], $mq[],$qt[],$tipo[] ).

推荐答案

<?php
$input = '87||1|nuovo#88||4|#209|||#89|||#41||1|#5|||#3||1|116#20|||#13||3|#148|||';
$items = explode('#', $input);
$result = [];
# or $id = $mq = $qr = $tipo = [];
foreach ($items as $i) {
  list($id, $mq, $qr, $tipo) = explode('|', $i);
  $result[] = ['id' => $id, 'mq' => $mq, 'qr' => $qr, 'tipo' => $tipo];
  # or $id[] = $id; $mq[] = $mq; $qr[] = $qr; $tipo[] = $tipo;
}

这篇关于爆炸两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆