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...
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.