gpt4 book ai didi

php - 如何将 foreach 放入单独的变量中?

转载 作者:行者123 更新时间:2023-11-30 00:23:55 25 4
gpt4 key购买 nike

我已经搜索这个论坛两天了,但我似乎无法掌握“foreach”的概念。

我发现了这个:

这正是我所需要的,但是!如何获得输出“echo "$key =\"$value\"
";"在可重用变量中???

    <?php
/* Create Array named $profile with contents of jos_users */
$user =& JFactory::getUser();
foreach ($user as $key => $value)
{
$profile[$key] = $value;
}

/* Extract jos_user_profiles data and add to $profile */
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('profile_key, profile_value');
$query->from('#__user_profiles');
$query->where('user_id = '. (int) $user->id);
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row)
{
$profile[$row->profile_key] = $row->profile_value;
}

/* Included for debugging purposes only 8*/
foreach ($profile as $key => $value)
{
echo "$key = \"$value\"<br/>";
}
?>

生成的输出如下:

    id = "636"
name = "test2"
username = "test2"
email = "test2@test.nl"
password = "$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0"
password_clear = ""
block = "0"
sendEmail = "0"
registerDate = "2014-04-11 18:49:24"
lastvisitDate = "2014-04-12 07:51:45"
activation = ""
params = "{}"
groups = "Array"
guest = "0"
lastResetTime = "0000-00-00 00:00:00"
resetCount = "0"
_errors = "Array"
aid = "0"
otpKey = ""
otep = ""
testprofile.phonenumber = "523657543763"

我想要的是输出到变量中,例如“id =“636””变成$id =“636”,或$variablename =“636”

有没有办法可以从上面的代码生成/声明自动变量? (并列出它们,以便我可以看到声明了哪些变量)。

我想做的是:

从 $user 获取数据并重用该数据,通过以下代码将更改的 mysql 数据插入/更新到 MsSQL 数据库中:

    include ('includes/connectwk.php');

$sql = "select DEEL_ID, DEEL_TEAM, DEEL_NAAM, DEEL_TELEFOON, DEEL_MAIL from WK_DEELNEMERS where DEEL_ID = '$usr_id'";
$stmt = sqlsrv_query( $conn, $sql );

if( $stmt === false)
{
die( print_r( sqlsrv_errors(), true) );
echo 'connection failed!';
}

//script part
if ($usr_id === 0 or $usr_login === null)
{}
else
{
if (sqlsrv_has_rows($stmt))
{
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$c_id = $row['DEEL_ID'];
$c_team = $row['DEEL_TEAM'];
$c_naam = $row['DEEL_NAAM'];
$c_tel = $row['DEEL_TELEFOON'];
$c_mail = $row['DEEL_MAIL'];

if ($usr_id === $c_id and $usr_login === $c_team and $usr_mail === $c_mail and $row['DEEL_NAAM'] === $usr_name)
{
$update = false;
}
else
{
$update = true;
}
}
}
else
{
$sql = "insert into WK_DEELNEMERS (DEEL_ID, DEEL_TEAM, DEEL_NAAM, DEEL_TELEFOON, DEEL_MAIL) values ('$usr_id','$usr_login','$usr_name','','$usr_mail')";
$stmt = sqlsrv_query( $conn, $sql );
echo 'insert uitgevoerd';
}
if ($update === true)
{
$sql ="update WK_DEELNEMERS set DEEL_TEAM = '$usr_login',DEEL_NAAM = '$usr_name',DEEL_TELEFOON ='bla3',DEEL_MAIL= '$usr_mail' where DEEL_ID = '$usr_id '";
$stmt = sqlsrv_query( $conn, $sql );
}

}
?>

可能这不是正确的方法,但在我学习的过程中,请随时“插入”我朝正确的方向前进。 :)

简而言之,我想要完成的是,从 mssql prefix_user 表和 prefix_user_profiles 表中获取数据。

将这些结果与我从 MsSQL 表中获得的结果进行比较,如果用户通过网站更新/更改了其个人资料,则插入/更新 MsSQL 表。

希望有人能帮助我。

提前致谢:)

好的,当我这样做时:

    <?php
$profile["id"] = "636";
$profile["name"] = "test2";
$profile["username"] = "test2";
$profile["email"] = "test2@test.nl";
$profile["password"] = "$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0";
$profile["password_clear"] = "";
$profile["block"] = "0";

foreach ($profile as $key => $value)
{
$$key = $value;
}

echo $id;

?>

我得到:636(我想要的)

但是当我这样做时:

    <?php
/* Create Array named $profile with contents of jos_users */
$user =& JFactory::getUser();
foreach ($user as $key => $value)
{
$$key = $value;
}

?>

我得到一个空白页。我所说的空白页面是指,我从 Joomla 文章中运行此 php,整个模板在浏览器中显示为空白。不是因为我没有在第二个脚本中使用 echo。

我不明白:/

@Hüseyin BABAL 这里是 var_dump($user);

    object(JUser)#35 (26) { ["isRoot":protected]=> bool(false) ["id"]=> string(3) "636" ["name"]=> string(5) "test2" ["username"]=> string(5) "test2" ["email"]=> string(13) "test2@test.nl" ["password"]=> string(34) "$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0" ["password_clear"]=> string(0) "" ["block"]=> string(1) "0" ["sendEmail"]=> string(1) "0" ["registerDate"]=> string(19) "2014-04-11 18:49:24" ["lastvisitDate"]=> string(19) "2014-04-12 13:38:55" ["activation"]=> string(0) "" ["params"]=> string(2) "{}" ["groups"]=> array(1) { [2]=> string(1) "2" } ["guest"]=> int(0) ["lastResetTime"]=> string(19) "0000-00-00 00:00:00" ["resetCount"]=> string(1) "0" ["_params":protected]=> object(JRegistry)#36 (1) { ["data":protected]=> object(stdClass)#37 (0) { } } ["_authGroups":protected]=> array(2) { [0]=> int(1) [1]=> int(2) } ["_authLevels":protected]=> array(3) { [0]=> int(1) [1]=> int(1) [2]=> int(2) } ["_authActions":protected]=> NULL ["_errorMsg":protected]=> NULL ["_errors":protected]=> array(0) { } ["aid"]=> int(0) ["otpKey"]=> string(0) "" ["otep"]=> string(0) "" } 

@Hüseyin BABAL 这里是 var_export($user);谢谢:)

    JUser::__set_state(array( 'isRoot' => false, 'id' => '636', 'name' => 'test2', 'username' => 'test2', 'email' => 'test2@test.nl', 'password' => '$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0', 'password_clear' => '', 'block' => '0', 'sendEmail' => '0', 'registerDate' => '2014-04-11 18:49:24', 'lastvisitDate' => '2014-04-12 17:27:36', 'activation' => '', 'params' => '{}', 'groups' => array ( 2 => '2', ), 'guest' => 0, 'lastResetTime' => '0000-00-00 00:00:00', 'resetCount' => '0', '_params' => JRegistry::__set_state(array( 'data' => stdClass::__set_state(array( )), )), '_authGroups' => array ( 0 => 1, 1 => 2, ), '_authLevels' => array ( 0 => 1, 1 => 1, 2 => 2, ), '_authActions' => NULL, '_errorMsg' => NULL, '_errors' => array ( ), 'aid' => 0, 'otpKey' => '', 'otep' => '', )) 

@ Hüseyin BABAL,这是 var_dump(get_object_vars($user)); 的输出

    array(20) { ["id"]=> string(3) "636" ["name"]=> string(5) "test2" ["username"]=> string(5) "test2" ["email"]=> string(13) "test2@test.nlDit e-mailadres wordt beveiligd tegen spambots. JavaScript dient ingeschakeld te zijn om het te bekijken. " ["password"]=> string(34) "$P$DcH9yo/xVoU7vTXgvmwCrCn5dDwfnE0" ["password_clear"]=> string(0) "" ["block"]=> string(1) "0" ["sendEmail"]=> string(1) "0" ["registerDate"]=> string(19) "2014-04-11 18:49:24" ["lastvisitDate"]=> string(19) "2014-04-12 17:50:44" ["activation"]=> string(0) "" ["params"]=> string(2) "{}" ["groups"]=> array(1) { [2]=> string(1) "2" } ["guest"]=> int(0) ["lastResetTime"]=> string(19) "0000-00-00 00:00:00" ["resetCount"]=> string(1) "0" ["_errors"]=> array(0) { } ["aid"]=> int(0) ["otpKey"]=> string(0) "" ["otep"]=> string(0) "" } 

我必须提到的是这个脚本:

    <?php
$user =& JFactory::getUser();
$userArr = (array) $user;
foreach($userArr as $key => $value)
{
$$key = "$value";
}
echo $id;
?>

它抛出:500内部服务器错误?

我刚刚安装 Joomla 的 full_ajax 插件后收到此弹出消息。

好的,现在我尝试了这个(希望我做得正确)

    <?php
$user =& JFactory::getUser();
$userArr = get_object_vars($user);
foreach($userArr as $key => $value)
{
$$key = $value;
}
echo $id;
?>

仍然是 500 内部服务器错误。我是否去掉 echo 并不重要。

最佳答案

PHP 拥有一个有用的 array functionextract()做你想做的事。它将数组的元素拉入当前符号表(本身就是一个数组)。

...   
foreach ($resultset as $row) {
extract( $row, EXTR_OVERWRITE, 'row_');

/*now your row elements are in the symbol table with names starting with row_ */

echo "id = $row_id";
echo "name = $row_name";
}

关于php - 如何将 foreach 放入单独的变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23030002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com