【问题标题】:How can i get Array values to separate php variables如何获取数组值来分隔 php 变量
【发布时间】:2017-01-05 03:34:19
【问题描述】:

我正在尝试检索 MySQL DB 中的记录。我想检索属于 img_path 列的所有记录。从以下代码中,我将结果作为数组获取。但我将它们作为单独的变量。

我的代码

$result_list = array();
while($row = mysqli_fetch_array($query)) {
    $result_list[] = $row;
}
foreach($result_list as $row) {

    $productitems[] = array(
        'img_path' => $row['img_path'],
    );

}

print_r($productitems);

电流输出

Array ( 
[0] => Array ( [img_path] => img/8041171eda3a8fddf508bfd0d9a0866e1472441466.png ) 
[1] => Array ( [img_path] => img/91882b5f9ffa624a9dc81dfa0ec980861472441077.jpg ) 
[2] => Array ( [img_path] => img ) )

预期输出

$variable1 = img/8041171eda3a8fddf508bfd0d9a0866e1472441466.png;
$variable2 = img/91882b5f9ffa624a9dc81dfa0ec980861472441077.jpg;

【问题讨论】:

  • 尝试关注$productitems = array(); // Initialize outside for loop $productitems[] = $row['img_path'];
  • @SauminiNavaratnam 这只是创建了另一个数组,当 OP 要求获取单独变量中的值时,这会适得其反。
  • 为什么要在单独的变量中输出?

标签: php mysql arrays


【解决方案1】:

你可以这样做:

$result_list = array();
while($row = mysqli_fetch_array($query)) {
    $result_list[] = $row;
}

foreach($result_list as $k => $row) {
    $varName = 'var' . $k;

    $$varName = array(
        'img_path' => $row['img_path'],
    );

}

您将可以访问 $var0、$var1 等。

【讨论】:

    【解决方案2】:

    您可以使用 extract() 函数。文档here

    extract() 函数将变量导入本地符号表 来自一个数组。

    此函数使用数组键作为变量名和值作为变量 价值观。对于每个元素,它将在当前创建一个变量 符号表。

    此函数返回成功时提取的变量数。

    【讨论】:

      【解决方案3】:

      使用list()

      http://php.net/manual/en/function.list.php
      

      来自手册:

      $info = array('coffee', 'brown', 'caffeine');
      
      // Listing all the variables
      list($drink, $color, $power) = $info;
      echo "$drink is $color and $power makes it special.\n";
      

      【讨论】:

      • 但你要考虑:list() only works on numerical arrays and assumes the numerical indices start at 0
      • 谢谢@PhilippPalmtag。我不知道:)
      【解决方案4】:

      你可以像这样使用extract函数:

      $result_list = array();
      while($row = mysqli_fetch_array($query)) {
          $result_list[] = $row;
      }
      foreach($result_list as $row) {
          $productitems[] = $row['img_path'];
      }
      extract($productitems, EXTR_PREFIX_ALL, "variable");
      echo $variable_0;
      echo $variable_1;
      echo $variable_2;
      

      【讨论】:

        【解决方案5】:

        您也可以使用以下代码,其中不需要使用list()extract() 等附加函数。这也是一种非常简约的方法。

        $result_list = array();
        while($row = mysqli_fetch_array($query)) {
            $result_list[] = $row;
        }
        
        foreach($result_list as $key => $row) {
            ${'img_path_'.$key} = $row['img_path'];
        }
        
        /*
        Output:
        ["img_path_0"]=>
          string(50) "img/8041171eda3a8fddf508bfd0d9a0866e1472441466.png"
        ["img_path_1"]=>
          string(50) "img/91882b5f9ffa624a9dc81dfa0ec980861472441077.jpg"
        ["img_path_2"]=>
          string(3) "img"
        */
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-24
          • 1970-01-01
          • 2021-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-06
          • 1970-01-01
          相关资源
          最近更新 更多