如何从查询结果集中组合多个列值以创建格式化句子?

发布于 2024-12-05 13:39:54 字数 9177 浏览 1 评论 0原文

如何更改此 php:

<?php 
$result = $sth->fetchAll();
print_r($result); //or var_dump($result); for more info
foreach($result as $row){
    print_r($row);
}   
?>      

输出,例如:

Joseph Dickinson wants an Xbox 360 for 150
   I need a one quick!

  John Doe offered it for 149.99 
  Jane Doe offered it for 154.99 

如何使用输出的数组 print_r 结果执行上面的操作?

Array
    (
        [0] => Array
            (
                [uFName] => Joseph
                [0] => Joseph
                [uLName] => Dickinson
                [1] => Dickinson
                [listTitle] => Need Xbox 360
                [2] => Need Xbox 360
                [listPropPrice] => 150
                [3] => 150
                [listCmt] => I need one quick!
                [4] => I need one quick!
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => John
                [6] => John
                [mLName] => Doe
                [7] => Doe
                [moAmt] => 149.99
                [8] => 149.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [1] => Array
            (
                [uFName] => Joseph
                [0] => Joseph
                [uLName] => Dickinson
                [1] => Dickinson
                [listTitle] => Need Xbox 360
                [2] => Need Xbox 360
                [listPropPrice] => 150
                [3] => 150
                [listCmt] => I need one quick!
                [4] => I need one quick!
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Jane
                [6] => Jane
                [mLName] => Doe
                [7] => Doe
                [moAmt] => 154.99
                [8] => 154.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [2] => Array
            (
                [uFName] => Joseph
                [0] => Joseph
                [uLName] => Dickinson
                [1] => Dickinson
                [listTitle] => Need Xbox 360
                [2] => Need Xbox 360
                [listPropPrice] => 150
                [3] => 150
                [listCmt] => I need one quick!
                [4] => I need one quick!
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Diana
                [6] => Diana
                [mLName] => Matthews
                [7] => Matthews
                [moAmt] => 160.00
                [8] => 160.00
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [3] => Array
            (
                [uFName] => Joseph
                [0] => Joseph
                [uLName] => Dickinson
                [1] => Dickinson
                [listTitle] => Need Xbox 360
                [2] => Need Xbox 360
                [listPropPrice] => 150
                [3] => 150
                [listCmt] => I need one quick!
                [4] => I need one quick!
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Amanda
                [6] => Amanda
                [mLName] => Koste
                [7] => Koste
                [moAmt] => 174.99
                [8] => 174.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [4] => Array
            (
                [uFName] => Warren
                [0] => Warren
                [uLName] => Kennan
                [1] => Kennan
                [listTitle] => Need New Sofa
                [2] => Need New Sofa
                [listPropPrice] => 1000
                [3] => 1000
                [listCmt] => Need one quick
                [4] => Need one quick
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Diana
                [6] => Diana
                [mLName] => Matthews
                [7] => Matthews
                [moAmt] => 495.99
                [8] => 495.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [5] => Array
            (
                [uFName] => Warren
                [0] => Warren
                [uLName] => Kennan
                [1] => Kennan
                [listTitle] => Need New Sofa
                [2] => Need New Sofa
                [listPropPrice] => 1000
                [3] => 1000
                [listCmt] => Need one quick
                [4] => Need one quick
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Amanda
                [6] => Amanda
                [mLName] => Koste
                [7] => Koste
                [moAmt] => 489.99
                [8] => 489.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
    )
    Array
    (
        [uFName] => Joseph
        [0] => Joseph
        [uLName] => Dickinson
        [1] => Dickinson
        [listTitle] => Need Xbox 360
        [2] => Need Xbox 360
        [listPropPrice] => 150
        [3] => 150
        [listCmt] => I need one quick!
        [4] => I need one quick!
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => John
        [6] => John
        [mLName] => Doe
        [7] => Doe
        [moAmt] => 149.99
        [8] => 149.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Joseph
        [0] => Joseph
        [uLName] => Dickinson
        [1] => Dickinson
        [listTitle] => Need Xbox 360
        [2] => Need Xbox 360
        [listPropPrice] => 150
        [3] => 150
        [listCmt] => I need one quick!
        [4] => I need one quick!
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Jane
        [6] => Jane
        [mLName] => Doe
        [7] => Doe
        [moAmt] => 154.99
        [8] => 154.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Joseph
        [0] => Joseph
        [uLName] => Dickinson
        [1] => Dickinson
        [listTitle] => Need Xbox 360
        [2] => Need Xbox 360
        [listPropPrice] => 150
        [3] => 150
        [listCmt] => I need one quick!
        [4] => I need one quick!
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Diana
        [6] => Diana
        [mLName] => Matthews
        [7] => Matthews
        [moAmt] => 160.00
        [8] => 160.00
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Joseph
        [0] => Joseph
        [uLName] => Dickinson
        [1] => Dickinson
        [listTitle] => Need Xbox 360
        [2] => Need Xbox 360
        [listPropPrice] => 150
        [3] => 150
        [listCmt] => I need one quick!
        [4] => I need one quick!
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Amanda
        [6] => Amanda
        [mLName] => Koste
        [7] => Koste
        [moAmt] => 174.99
        [8] => 174.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Warren
        [0] => Warren
        [uLName] => Kennan
        [1] => Kennan
        [listTitle] => Need New Sofa
        [2] => Need New Sofa
        [listPropPrice] => 1000
        [3] => 1000
        [listCmt] => Need one quick
        [4] => Need one quick
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Diana
        [6] => Diana
        [mLName] => Matthews
        [7] => Matthews
        [moAmt] => 495.99
        [8] => 495.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Warren
        [0] => Warren
        [uLName] => Kennan
        [1] => Kennan
        [listTitle] => Need New Sofa
        [2] => Need New Sofa
        [listPropPrice] => 1000
        [3] => 1000
        [listCmt] => Need one quick
        [4] => Need one quick
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Amanda
        [6] => Amanda
        [mLName] => Koste
        [7] => Koste
        [moAmt] => 489.99
        [8] => 489.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )

How do I change this php:

<?php 
$result = $sth->fetchAll();
print_r($result); //or var_dump($result); for more info
foreach($result as $row){
    print_r($row);
}   
?>      

That outputs, to give me for example:

Joseph Dickinson wants an Xbox 360 for 150
   I need a one quick!

  John Doe offered it for 149.99 
  Jane Doe offered it for 154.99 

How do I do that above using the outputted array print_r results?

Array
    (
        [0] => Array
            (
                [uFName] => Joseph
                [0] => Joseph
                [uLName] => Dickinson
                [1] => Dickinson
                [listTitle] => Need Xbox 360
                [2] => Need Xbox 360
                [listPropPrice] => 150
                [3] => 150
                [listCmt] => I need one quick!
                [4] => I need one quick!
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => John
                [6] => John
                [mLName] => Doe
                [7] => Doe
                [moAmt] => 149.99
                [8] => 149.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [1] => Array
            (
                [uFName] => Joseph
                [0] => Joseph
                [uLName] => Dickinson
                [1] => Dickinson
                [listTitle] => Need Xbox 360
                [2] => Need Xbox 360
                [listPropPrice] => 150
                [3] => 150
                [listCmt] => I need one quick!
                [4] => I need one quick!
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Jane
                [6] => Jane
                [mLName] => Doe
                [7] => Doe
                [moAmt] => 154.99
                [8] => 154.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [2] => Array
            (
                [uFName] => Joseph
                [0] => Joseph
                [uLName] => Dickinson
                [1] => Dickinson
                [listTitle] => Need Xbox 360
                [2] => Need Xbox 360
                [listPropPrice] => 150
                [3] => 150
                [listCmt] => I need one quick!
                [4] => I need one quick!
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Diana
                [6] => Diana
                [mLName] => Matthews
                [7] => Matthews
                [moAmt] => 160.00
                [8] => 160.00
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [3] => Array
            (
                [uFName] => Joseph
                [0] => Joseph
                [uLName] => Dickinson
                [1] => Dickinson
                [listTitle] => Need Xbox 360
                [2] => Need Xbox 360
                [listPropPrice] => 150
                [3] => 150
                [listCmt] => I need one quick!
                [4] => I need one quick!
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Amanda
                [6] => Amanda
                [mLName] => Koste
                [7] => Koste
                [moAmt] => 174.99
                [8] => 174.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [4] => Array
            (
                [uFName] => Warren
                [0] => Warren
                [uLName] => Kennan
                [1] => Kennan
                [listTitle] => Need New Sofa
                [2] => Need New Sofa
                [listPropPrice] => 1000
                [3] => 1000
                [listCmt] => Need one quick
                [4] => Need one quick
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Diana
                [6] => Diana
                [mLName] => Matthews
                [7] => Matthews
                [moAmt] => 495.99
                [8] => 495.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
        [5] => Array
            (
                [uFName] => Warren
                [0] => Warren
                [uLName] => Kennan
                [1] => Kennan
                [listTitle] => Need New Sofa
                [2] => Need New Sofa
                [listPropPrice] => 1000
                [3] => 1000
                [listCmt] => Need one quick
                [4] => Need one quick
                [listDt] => 2011-09-15
                [5] => 2011-09-15
                [mFName] => Amanda
                [6] => Amanda
                [mLName] => Koste
                [7] => Koste
                [moAmt] => 489.99
                [8] => 489.99
                [moDtOff] => 2011-09-15
                [9] => 2011-09-15
            )
    
    )
    Array
    (
        [uFName] => Joseph
        [0] => Joseph
        [uLName] => Dickinson
        [1] => Dickinson
        [listTitle] => Need Xbox 360
        [2] => Need Xbox 360
        [listPropPrice] => 150
        [3] => 150
        [listCmt] => I need one quick!
        [4] => I need one quick!
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => John
        [6] => John
        [mLName] => Doe
        [7] => Doe
        [moAmt] => 149.99
        [8] => 149.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Joseph
        [0] => Joseph
        [uLName] => Dickinson
        [1] => Dickinson
        [listTitle] => Need Xbox 360
        [2] => Need Xbox 360
        [listPropPrice] => 150
        [3] => 150
        [listCmt] => I need one quick!
        [4] => I need one quick!
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Jane
        [6] => Jane
        [mLName] => Doe
        [7] => Doe
        [moAmt] => 154.99
        [8] => 154.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Joseph
        [0] => Joseph
        [uLName] => Dickinson
        [1] => Dickinson
        [listTitle] => Need Xbox 360
        [2] => Need Xbox 360
        [listPropPrice] => 150
        [3] => 150
        [listCmt] => I need one quick!
        [4] => I need one quick!
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Diana
        [6] => Diana
        [mLName] => Matthews
        [7] => Matthews
        [moAmt] => 160.00
        [8] => 160.00
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Joseph
        [0] => Joseph
        [uLName] => Dickinson
        [1] => Dickinson
        [listTitle] => Need Xbox 360
        [2] => Need Xbox 360
        [listPropPrice] => 150
        [3] => 150
        [listCmt] => I need one quick!
        [4] => I need one quick!
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Amanda
        [6] => Amanda
        [mLName] => Koste
        [7] => Koste
        [moAmt] => 174.99
        [8] => 174.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Warren
        [0] => Warren
        [uLName] => Kennan
        [1] => Kennan
        [listTitle] => Need New Sofa
        [2] => Need New Sofa
        [listPropPrice] => 1000
        [3] => 1000
        [listCmt] => Need one quick
        [4] => Need one quick
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Diana
        [6] => Diana
        [mLName] => Matthews
        [7] => Matthews
        [moAmt] => 495.99
        [8] => 495.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )
    Array
    (
        [uFName] => Warren
        [0] => Warren
        [uLName] => Kennan
        [1] => Kennan
        [listTitle] => Need New Sofa
        [2] => Need New Sofa
        [listPropPrice] => 1000
        [3] => 1000
        [listCmt] => Need one quick
        [4] => Need one quick
        [listDt] => 2011-09-15
        [5] => 2011-09-15
        [mFName] => Amanda
        [6] => Amanda
        [mLName] => Koste
        [7] => Koste
        [moAmt] => 489.99
        [8] => 489.99
        [moDtOff] => 2011-09-15
        [9] => 2011-09-15
    )

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

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

发布评论

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

评论(1

十级心震 2024-12-12 13:39:54

听起来您想将关联数组转换为句子,例如:

$array = array('noun'=>'spot', 'verb'=>'run');

您可以简单地从各个部分构建句子:

$sentence = "See ".$array['noun'].".\n".
            "See ".$array['noun']." ".$array['verb'].".\n". 
            $array['verb']." ".$array['noun'].", ".$array['verb']."!";

这是最简单、最有效的方法。然而,它也有一些缺点;

  • 它可能很难阅读,尤其是当有大量文本或值需要替换时。
  • 它不能很容易地更新——你必须修改程序来改变句子结构。

另一种方法是在字符串中使用占位符,并替换值:

$sentence_template = "See {noun}.\nSee {noun} {verb}. {verb} {noun}, {verb}!";
$from = array();
foreach(array_keys($array) as $label)
    $from[] = '{'.$label.'}';
$to = array_values($array);

$sentence = str_replace($from, $to, $sentence_template);

这种方法允许将模板字符串单独定义到程序中,例如在数据库或配置文件中,也更易于阅读和维护。另一方面;

  • 灵活性较差。例如,在第一种方法中,将第三行的动词大写很简单,但这样就比较困难。

听起来对于你的问题,无论哪种方式都可以。

如果您使用第二种方法,请务必将数据库对象配置为仅返回关联(字符串)元素,而不是数字元素;否则它将不必要地搜索 {0}、{1} 等)

It sounds like you want to convert an associative array into a sentence, eg:

$array = array('noun'=>'spot', 'verb'=>'run');

You could simply build the sentence from parts:

$sentence = "See ".$array['noun'].".\n".
            "See ".$array['noun']." ".$array['verb'].".\n". 
            $array['verb']." ".$array['noun'].", ".$array['verb']."!";

This is the simplest and most efficient method. However, it has some drawbacks;

  • It can be hard to read, especially if there are large amounts of text or values to substitute.
  • It can't be updated very easily - you'd have to modify the program to change the sentence structure.

Another approach is to use placeholders in a string, and substitute in the values:

$sentence_template = "See {noun}.\nSee {noun} {verb}. {verb} {noun}, {verb}!";
$from = array();
foreach(array_keys($array) as $label)
    $from[] = '{'.$label.'}';
$to = array_values($array);

$sentence = str_replace($from, $to, $sentence_template);

This approach allows the template string to be defined separately to the program, for instance in a database or configuration file, it's also easier to read and maintain. On the other hand;

  • It's less flexible. For instance, in the first approach capitalising the verb on the third line would be simple, but it's more difficult in this way.

It sounds like for your question, either way would be fine.

(If you use the second approach, be sure to configure the database object to just return associative (string) elements, not numeric elements; otherwise it will be needlessly searching for {0}, {1}, etc)

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