我有这个API响应数据的堆栈

时间:2012-02-20 07:00:39

标签: php arrays api

让我们谈谈: 我有像这样的回复代码

  

的字符串(141)   “000 | 0123456789 |的 0987654321 | namexxxxx | 081xxxx | 10000 | 1231231230 | namexxxxxx | 081xxxx | 10000 | 3213213210 | namaxxxxxx | 081xxxx | 10000 |页| total_page”

(我为主要数据加粗)

有3个主要数据。主要数据是动态的(ID在名称前面,最后一个数据是“10000”),max是10循环。我如何用PHP获取主要数据? 帮助:)

2 个答案:

答案 0 :(得分:1)

使用函数explode

爆炸字符串
$array = explode ( '|' , $string );
print_r($array);

找出数组索引上的所需数据

explode function

答案 1 :(得分:1)

我发现最好的做法是为我工作是创建一个索引数组并使用它,你已经知道每个param的表示,所以我将创建命名数组元素来帮助我处理我的响应数据。 / p>

<?php
$key = array(
    'code1' => 0
    'code2' => 1
    'code3' => 2
    'name' => 3
    'code4' => 4
    ...
);
$data_str = "000|0123456789|0987654321|namexxxxx|081xxxx|10000|1231231230|namexxxxxx|081xxxx|10000|3213213210|namaxxxxxx|081xxxx|10000|page|total_page";
$data = explode('|',$data_str);

$response_name = $data[$key['name']];

希望这会有所帮助。