简体   繁体   English

如何在PHP中合并2个输出数组

[英]How to merge 2 outputs array in PHP

I'm a beginer in PHP programming, i want to ask for my problem here. 我是PHP编程的初学者,我想在这里提问。

Before, i get the PHP scripts from: https://github.com/VosCast/SHOUTcast-PHP-Stats 之前,我从以下网址获取PHP脚本: https : //github.com/VosCast/SHOUTcast-PHP-Stats

This is the code, how i get the array: 这是代码,我如何获取数组:

require_once 'vc_shoutcast.class.php'; // get stats
require_once 'vc_shoutcast_json_relay.class.php'; // produce json

$lists = array(
    array(
        'host'  => 'host.net',
        'port'  => '9898'
    ),
    array(
        'host'  => 'host.net', 
        'port' => '8787'
    )
);

$i = 1;
foreach ($lists as $list => $radio) {
    $vc_shoutcast = new vc_shoutcast( $radio['host'], $radio['port'], false );
    $vc_shoutcast_json_relay = new vc_shoutcast_json_relay( $vc_shoutcast, 1, $cache = './stats_' . $i . '.json' );
    $vc_shoutcast_json_relay->run( 'both' );
$i++;
}

This is the vc_shoutcast_json_relay.class.php ( $vc_shoutcast_json_relay->run( 'both' ); ) code: 这是vc_shoutcast_json_relay.class.php($ vc_shoutcast_json_relay-> run('both');)代码:

foreach ($vars as $value) {
    $data[$value] = $this->vc_shoutcast->$value;
}

var_dump( $data );

From the code above, I'll get two outputs array, like this: 从上面的代码中,我将获得两个输出数组,如下所示:

Array(
     [currentlisteners] => 2,
     [maxlisteners] => 3,
     [songtitle] => Some song title 2
)

Array(
    [currentlisteners] => 12,
    [maxlisteners] => 13,
    [songtitle] => Some song title 2
)

How do I merge two arrays into one array become: 如何将两个数组合并为一个数组成为:

Array(
   [0] => (
        [currentlisteners] => 2,
        [maxlisteners] => 3,
        [songtitle] => Some song title 2
   ),

   [1] => (
       [currentlisteners] => 12,
       [maxlisteners] => 13,
       [songtitle] => Some song title 2
   )
)

I know, i can merge 2 arrays with array_merge( $data ) or with something similiar functions, but it isn't works. 我知道,我可以将2个数组与array_merge($ data)或类似函数合并,但这是行不通的。

Thanks for your help. 谢谢你的帮助。

Simplest way is to just create a new one with your existing arrays as the values: 最简单的方法是使用现有数组作为值来创建一个新的数组:

$newArray = array($array1, $array2);

This is the same as this: 与此相同:

$newArray = array();
array_push($newArray, $array1);
array_push($newArray, $array2);

Or this: 或这个:

$newArray = array();
$newArray[] = $array1;
$newArray[] = $array2;

Depending on your code, you might prefer to add your data to the main array using one of the last two methods as you go along, rather than trying to create the whole thing in one go at the end. 根据您的代码,您可能更喜欢在执行过程中使用最后两种方法之一将数据添加到主数组中,而不是尝试一口气创建整个过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM