开发者

Transform Array using Php

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
        [lis开发者_运维技巧tCmt] => 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
    )


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜