Home >Backend Development >PHP Tutorial >Extract a certain item value in the json array in text (txt)

Extract a certain item value in the json array in text (txt)

WBOY
WBOYOriginal
2016-10-22 00:14:211467browse

1. The project has been connected to the third-party platform. The previous technical staff wrote the return results into a txt file, which contains the name of the orderer, mobile phone number, and Jason array (the array contains the results returned by the third-party platform);
Now I need to extract a certain value from json and submit it to Alipay. I don’t know how to operate

Extract a certain item value in the json array in text (txt)

I want to get the value PayDeclareAmount":0.0075 in the array. Does anyone have any ideas?

Reply content:

1. The project has been connected to the third-party platform. The previous technical staff wrote the return results into a txt file, which contains the name of the orderer, mobile phone number, and Jason array (the array contains the results returned by the third-party platform);
Now I need to extract a certain value from json and submit it to Alipay. I don’t know how to operate

Extract a certain item value in the json array in text (txt)

I want to get the value PayDeclareAmount":0.0075 in the array. Does anyone have any ideas?

There are two methods:

  1. If the previous formats are similar, starting with n lines, directly skip n lines, the above data, and then, after the row information read later is spliced ​​together, json_decode(xxx, true); , and then return based on the field. To read a line use fgets.

  2. Filter with regular matching.

Because I am sure that as long as the value after PayDeclareAmount
matching rule $checktxt="/^PayDeclareAmount&/"is defined like this, the question is how to get out the subsequent value?

<code>public function getText(){
        $file=APPPATH.'upload/aaa.txt';
        $content=file($file);
        foreach ($content as $cont){
            echo $cont."<br>";
            }
        }</code>

I just listed the text and got stuck later

Regular really wants to have an indexOf attribute, which returns the matched index value. If you match the '{' symbol and intercept all of it, you will get a string in json format. Then the rest will be easy

Thanks for the invitation.

<code>json_decode("json字符串", true)["PayDeclareAmount"] 即可。</code>

Let’s learn. Everyone’s answers are really good

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn