匹配数组键与字符串,然后添加一个函数键值- php


Match array key with a string then add a function to the key value - php

我正在尝试将字符串与数组键进行比较,如果它们匹配,我想调用一个函数来替换匹配键的值。

我的数组:

Array ( [id] => 1 [username] => Ahmed [name] => Ahmed Jalal [avatar] => no_avatar.png )

我的函数:

resizer($avatar,'50');

我正在使用这个cod来完成工作

$query = $db->prepare($mysql);
$db->stmt_assoc($query, $table);
$query->fetch();
if ($table['avatar']) {
$data[] = call_user_func('resizer', $table['avatar'],'50');
$table['avatar']=$data;
}
if($type == 'json'){
return json_encode($data);
}else{
return $table;
}

好的,抱歉我的误解,这里是一些基于你给的数组的代码。它将把每个键转换为一个变量的名称,并将该键的值存储在命名的变量中。

$g['id'] = 1;
$g['username'] = 'Ahmed';
$g['name'] = 'Ahmed Jalal';
$g['avatar'] = 'no_avatar.png';
foreach($g as $k => $v)
{
    $$k = $v;
}
echo $avatar; // displays no_avatar.png

是的,如果你使用gd或imagemagick或其他gfx添加到php中,你可以使用resize,如果你在avatar或新的数组键中包含图像的路径。