Foreach 循环在分解变量后不会循环..需要帮助

发布于 2024-11-29 07:15:49 字数 1231 浏览 1 评论 0原文

我无法显示数据库中的一行,该行具有以逗号分隔的数组形式的技能列表。完整注释的代码和下面的解释是我到目前为止所做的。非常感谢所有帮助。提前致谢。

// DB Connection here

    // Execute Query 
    $sql        =   "SELECT id, skills FROM p_info";
    $result     =   mysql_query($sql);
    $row        =   mysql_fetch_array($result);

    // Delimit array by comma
    $myArray = explode(',', $row['skills']);

    // Store as array
    $myArr   = array($myArray);

    /** 
    Assume the value of is skills row in the database is: type 1, type 2,  type 3, type 4 
    i would like to show these values each on a seperate <li> element hence using a loop. 

    <li> type 1</li>
    <li> type 2</li>
    <li> type 3</li>
    <li> type 4</li>

    My guess would be to use foreach loop..but im a little confused how to  go about this. Here whats i think logically:

    **/
    ?>
        <ul>
        <?
            foreach ( $myArr as $skills ) { 
        ?>
            <li><?=$myArr;?></li>

        <? 
            } // End loop 
        ?>
        </ul>

    <!-- Can someone tell me what im doing wrong? im fairly new to PHP but im a quick learner. :) -->

I am having trouble showing a row from a database which has a list of skills as an array which is delimited by a commas. Fully annotated code with explanation below is what i've done sor far. All help is much appreciated. Thanks in advance.

// DB Connection here

    // Execute Query 
    $sql        =   "SELECT id, skills FROM p_info";
    $result     =   mysql_query($sql);
    $row        =   mysql_fetch_array($result);

    // Delimit array by comma
    $myArray = explode(',', $row['skills']);

    // Store as array
    $myArr   = array($myArray);

    /** 
    Assume the value of is skills row in the database is: type 1, type 2,  type 3, type 4 
    i would like to show these values each on a seperate <li> element hence using a loop. 

    <li> type 1</li>
    <li> type 2</li>
    <li> type 3</li>
    <li> type 4</li>

    My guess would be to use foreach loop..but im a little confused how to  go about this. Here whats i think logically:

    **/
    ?>
        <ul>
        <?
            foreach ( $myArr as $skills ) { 
        ?>
            <li><?=$myArr;?></li>

        <? 
            } // End loop 
        ?>
        </ul>

    <!-- Can someone tell me what im doing wrong? im fairly new to PHP but im a quick learner. :) -->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

简单爱 2024-12-06 07:15:49

删除 $myArr = array($myArray); 并执行 foreach ( $myArray as $skills ) {
您将数组放入一个新数组中,因此当您尝试循环它时,您最终会得到原始数组。

还将 更改为 ,您正在循环 $myArray 并将每个部分临时存储在 $skills 中,$myArray 不会改变,因此尝试 echo 它是没有用的。

Remove $myArr = array($myArray); and do foreach ( $myArray as $skills ) { instead.
You are putting your array within a new array, so when you try to loop through it you end up with the original array.

Also change <?=$myArr;?> to <?=$skills;?>, you are looping through $myArray and storing every part in $skills temporarily, $myArray doesn't change so it's useless trying to echo it.

爱你不解释 2024-12-06 07:15:49

explode 已经返回一个数组。将结果包装在数组中将为您提供如下结果:

array(array('foo', 'bar', 'baz'))

当循环外部数组时,当然您不会获得内部数组的结果。只需摆脱 $myArr = array($myArray) 并使用 $myArray 即可。

explode already returns an array. Wrapping that result in an array will give you something like this:

array(array('foo', 'bar', 'baz'))

When looping over the outer array, of course you won't get the results of the inner array. Just get rid of $myArr = array($myArray) and use $myArray.

桃气十足 2024-12-06 07:15:49
  • 不需要“存储为数组”行。 $myArray 已经是一个数组。
  • 编写
  • 而不是 $myArr。这就是数组。
  • 不要使用短的开放标签 (),而是使用
  • The "store as array" line is not needed. $myArray is already an array.
  • Write <li><?php echo $skills; ?> instead of $myArr. That's the array.
  • Don't use short open tags (<?) use <?php instead
南烟 2024-12-06 07:15:49

不需要执行 $myArr = array($myArray); 因为 explode() 已经返回一个数组。

您始终可以对数组或任何其他变量执行 var_dump($myArray) 来查看其中的内容。

No need to do $myArr = array($myArray); as explode() already returns an array.

You can always do a var_dump($myArray) on your array or any other variable to see what's in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文