1

My goal is:

If a value in those arrays match with an extention of a file, then I can builds a nice URL encoded string which I can append to a url using the http_build_query() function. If no match is found, it return nothing.

I have tried it every possible way and it doesn't work, as the code below. can you check on which part is wrong ?.

$name = 'myfile.mp4';

$list = array(
     'video' => array('3gp', 'mkv', 'mp4'),
     'photo' => array('jpg', 'png', 'tiff')
);

// File Ext Check
$ext = (strpos($name, '.') !== false) ? strtolower(substr(strrchr($name, '.'), 1)) : '';

$type = null;
$find = $ext;

array_walk($list, function ($k, $v) use ($find, &$type) 
{
  if (in_array($find, $k))
  { 
    $type = $v;
    $data = array();

    switch ($type) 
    {
      case 'video':
         $data['title'] = 'video';
         $data['description'] = 'my video';
         break;
      case 'photo':
        $data['title'] = 'photo';
        $data['description'] = 'my photo';
        break;
    }
  }
  else {
    echo "not both!.";
    exit;
  {
});


if ($type == 'video') {
  $link = 'https://www.video.com' . http_build_query($data);
} 
else 
if ($type == 'photo') {
  $link = 'https://www.photo.com' . http_build_query($data);
}

echo $link;

Thank you...

LF00
  • 27,015
  • 29
  • 156
  • 295

1 Answers1

0

You $data is set in the array_walk() scope, but out of the array_walk() scope is not defined. So in the http_build_query($data), here $data has not defined.

You may referende the $data in the use(), then after the array_walk(), you can use its value. In your code you use in_array() to check the file type in a list, for performance I recomend you to see this post.

Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295
  • Hi there, all method on your recommended link is works. Thank you. BUT, I still can't figure out how to make the `array_walk()` works in my condition above. I want to understand and explore it. Can you gimme an exact answer?. – Romeo LeGagahne Dec 25 '16 at 11:50
  • initial the $data out of array_walk(), then call array_walk($list, function ($k, $v) use ($find, &$type, &$data) ); – LF00 Dec 25 '16 at 12:00