dongmei1828 2014-04-26 23:18
浏览 59
已采纳

子串和爆炸的PHP难度

I have an input of some products as "id" parameter, information about the purchase, status, and the user who's purchasing as you can see in this input example.

{"id":2694301,"state":3,"timeout":25,"date":"2014-04-27 00:32:37","date_timeout":"2014-04-27 01:00:02","user":"wolf_359","purchase":341.52},{"id":34017,"state":1,"timeout":10,"date":"2014-04-27 01:59:34","date_timeout":"2014-04-27 01:00:02","user":"mik10","purchase":217.90},{"id":12317,"state":2,"timeout":5,"date":"2014-04-27 03:51:31","date_timeout":"2014-04-27 01:00:02","user":"assemblyuser","purchase":1321.17};

What i'm trying is to get only one product, ordered with all the information, knowing the id. That means, for example, if I know that my Id is 34017, i just wanna show this row ignoring the others:

{"id":34017,"state":1,"timeout":10,"date":"2014-04-27 01:59:34","date_timeout":"2014-04-27 01:00:02","user":"mik10","purchase":217.90}

So the reason for what I'm asking help is that I don't know if there's any other possibility getting with substrings and explodes the data cause this looks very very complicated. This is what I've done:

preg_match_all('/{"id":2694017/', $page, $matches, PREG_OFFSET_CAPTURE); 
$firstkey = $matches[0][0][1];
$page = substr($page, $firstkey, 210);
$values = explode('/:,', $page);
$value1 = $values[1];
$value2 = $values[2];
$value3 = $values[3];
$value4 = $values[4];
$value5 = $values[5];
$value6 = $values[6];
$value7 = $values[7];

In the first lane I obtain the position in the string the { is. Then I cut with a substring the register plus a little bit more of the next preventing that the username could be very long. After, due to I know the registers I'm interested are in the next 7 colons, I just explode.

  • 写回答

2条回答 默认 最新

  • douzhuo1858 2014-04-26 23:34
    关注

    Heh it is too simple using json_decode function; Make sure that you have a correct format of data. In your case you need to wrap your input data with "[ ]" Just see the code:

    $inputData = '[
    {"id":2694301,"state":3,"timeout":25,"date":"2014-04-27 00:32:37","date_timeout":"2014-04-27 01:00:02","user":"wolf_359","purchase":341.52},
    {"id":34017,"state":1,"timeout":10,"date":"2014-04-27 01:59:34","date_timeout":"2014-04-27 01:00:02","user":"mik10","purchase":217.90},
    {"id":12317,"state":2,"timeout":5,"date":"2014-04-27 03:51:31","date_timeout":"2014-04-27 01:00:02","user":"assemblyuser","purchase":1321.17}
    ]';
    
    $products = json_decode($inputData);
    $productId = 34917; // product id wich you want to show;
    
    $result = array();
    foreach($products as $product) {
      if ($product['id'] == $productId) {
         $result = $product;
         break;
      }
    }
    
    
    var_dump($result); // here you will data for product you want.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法