0

I am working on a php code from Code#A as shown below in which on debug (Line#A) prints the following:

Code#A

$mp4_files = array_values($mp4_files);

print_r($mp4_files);   // Line#A

Output from Line#A:

Array ( [0] => 36031P.mp4 [1] => hello.mp4 )

Code#B

print_r($_POST['id']);   // Line#B

  if (!empty($_POST['id']))
  {
     foreach ($mp4_files as $f)
         {   
             print_r($f); // Line#C prints 2 values 36031P.mp4hello.mp4
             $parts = pathinfo($f);
             switch ($parts['extension'])
             {
                 case 'mp4' :
                 $filePath = $src_dir . DS . $f;
                 system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);
             }
         }
   }

Line#B from Code#B has 2 values: 0 and 1 which comes on the click of a button from 2 different rows.

When 1st row button is clicked, value 0 show up at Line#B

When 2nd row button is clicked, value 1 show up at Line#B


Problem Statement:

I am wondering what changes I should make in the php code (Code#B) above (which have Line#B and Line#C) so that:

When 1st row button is clicked, Line#C prints 36031P.mp4.

When 2nd row button is clicked, Line#C prints hello.mp4.

flash
  • 1,455
  • 11
  • 61
  • 132

2 Answers2

1

AFAIK, usingforeach will loop through the whole array, as no stop point was indicated.
Try the following...

    for($i=0; $i <= count($mp4_files); $i++) {
        if($i == $_POST['id']) {
            $parts = pathinfo( $mp4_files[$i] );
            switch ($parts['extension']) {
                //
            }
        break;
        }
    }

You can build on it from there. I'm sorry I'm typing from a mobile device, so it's not easy. I'll send something better as soon as I lay my hands on my machine.

EDITED
Try the following in your code

    print_r($_POST['id']);   // Line#B
    if (!empty($_POST['id'])) {
        foreach ($mp4_files as $key => $f) {
            if($key == $_POST['id']) {
                print_r($f); // Line#C prints 2 values 36031P.mp4hello.mp4
                $parts = pathinfo($f);
                switch ($parts['extension']) {
                    case 'mp4' :
                    $filePath = $src_dir . DS . $f;
                    system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);
                    break;
                }
            }
            break; //stop further iteration after the condition is met
        }
    }

Note the additional $key inside theforeach declaration and how I later used it for comparison.

maswerdna
  • 315
  • 2
  • 10
  • 1
    **This is a comment on the original question. I don't have enough reputation atm to add it as a comment** => `print_r($f); // Line#C prints 2 values 36031P.mp4hello.mp4` This output is a concatenated result of two different `print_r`'s as you're performing a complete iteration over the whole`Array`. To show this clearly, try => `print_r($f); echo "\n";` – maswerdna Jun 09 '19 at 18:07
  • I am modifying my question. In the foreach loop conversion of mp4 into mp3 is happening. – flash Jun 09 '19 at 18:40
  • Just modified my question. Let me know how it looks. – flash Jun 09 '19 at 18:41
  • 1
    As @parsonsbot has explained and as I understood your question, you only want to print a single value from your array which corresponds with the `$_POST['id']`? Note that calling a `foreach` on an array without a match condition or `break`, `continue` etc statement, the whole array will be iterated, which means, all entries will be printed. To avoid this, check to only use the value when the `Array index == $_POST['id']`, and `break` out of the loop when your condition is met. **I will edit my answer to show the complete code** – maswerdna Jun 09 '19 at 23:36
  • Right and yes, I have removed break from my code. In my code, for some reasons its not going switch statement. Not sure what is the issue. I have put echo "Hello World" inside switch statement and its not printing anything. – flash Jun 09 '19 at 23:44
  • I tried with this code and it seems to be working fine: `if (!empty($_POST['id'])) { for($i=0; $i – flash Jun 10 '19 at 00:17
  • The only issue is that its not going switch statement. Let me know what is the issue. – flash Jun 10 '19 at 00:17
  • I have accepted your answer but I came back with another [problem](https://stackoverflow.com/questions/56519616/how-to-use-pathinfo-in-php). I am wondering you can have a look. – flash Jun 10 '19 at 02:29
1

If the $_POST['id'] is always consistent with the indexes returned by array_values, this will work:

$mp4Files = array_values($mp4Files);

if (
    !empty($_POST['id']) &&
    !empty($mp4Files[$_POST['id']])
) {
    print_r($mp4Files[$_POST['id']]); // Line#C
}