0

I created a small script which is getting some results from a json file

foreach ($obj as $key) {
    echo "encoder: ".($obj_info->customname)."\n";
    $naam = ($key->Name);
    echo "ChannelName: ". $naam." "."\n";
    if (isset($naam)) {
        foreach ($key->Device->Template->Tracks->AudioTracks as $audiotrack) {
            echo "AUDIO PID: ".$audiotrack->Pid."\n";
            echo "Codec:     ".$audiotrack->Encoding->Codec."\n";
        }

This gives then the result:

ChannelName: Channel X
AUDIO PID: 0
Codec:     MPEG AAC
AUDIO PID: 0
Codec:     MPEG AAC
AUDIO PID: 0
Codec:     MPEG AAC

Because the audio pid result is an unknown factor i have this foreach loop created but there i would like to add based on the result a number to the audio pid

AUDIO PID 1: 0
Codec
AUDIO PID 2: 0
Codec

How can i achieve this?

Thanks

Martin

1
  • 1
    you could use a counter... $counter++ on each loop and concatenate it to "AUDIO PID " . $counter . ":" Commented Jan 22, 2020 at 8:00

1 Answer 1

3

Well, as simple as it is, you can use a variable that you increment on each iteration :

if (isset($naam)) {

    $pidNumber = 1; // <-------------

    foreach ($key->Device->Template->Tracks->AudioTracks as $audiotrack) {
        // -----------------v----------v
        echo "AUDIO PID " . $pidNumber++ . " : " . $audiotrack->Pid."\n";
        echo "Codec:     ".$audiotrack->Encoding->Codec."\n";
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Correct answer, so +1 from me, but it would probably be clearer to the OP if you incremented $pidNumber on a separate line.

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.