1

I'm a beginer in PHP programming, i want to ask for my problem here.

Before, i get the PHP scripts from: 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:

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.

Thanks for your help.

5
  • How are you getting two arrays with the first code block? There's just one array $data. Commented May 16, 2015 at 16:40
  • 2
    Your arrays aren't merged, they're nested. array($data1, $data2). Commented May 16, 2015 at 16:41
  • Do you run foreach twice to get 2 arrays ? Commented May 16, 2015 at 16:43
  • do you even rtfm bro? Commented May 16, 2015 at 17:01
  • I'm sorry, Thanks for your comments all! Commented May 16, 2015 at 17:03

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.