我需要在单个.json文件中从我的服务器编译多个数据。
我的意思是:
以下是存储的值:
https://www.design-24.eu/listener/1/catch.php
https://www.design-24.eu/listener/2/catch.php
...and so on.
现在我需要编译成这种格式:
{"all":*all together*, "channels":{"1":*value1*,"2":*value2*,"3":*value3*,"4":*value4*,"5":*value5*,"6":*value6*}}
非常感谢帮助! :)
答案 0 :(得分:0)
这可以解决您的问题:
$urls = array();
$urls[] = 'https://www.design-24.eu/listener/1/catch.php';
$urls[] = 'https://www.design-24.eu/listener/2/catch.php';
$urls[] = 'https://www.design-24.eu/listener/3/catch.php';
// add as many of the above as you want
$x = count( $urls );
$arr = array();
$arr[ 'all' ] = 0;
$arr[ 'channels' ] = array();
for ( $z = 0; $z < $x; $z++ ) {
$json = file_get_contents( $urls[ $z ] );
$json = preg_replace( "/[^0-9]/", "", $json );
$arr[ 'all' ] = $arr[ 'all' ] + $json;
$arr[ 'channels' ][ ( $z + 1 ) ] = $json;
}
$output = json_encode( $arr );
$f = fopen( "result.json", "w" ); // change result.json to the filename you want to write to
fwrite( $f, $output );
fclose( $f );