[php]如何用逗号将数组值转换为一个数组值?

时间:2016-02-18 07:30:00

标签: php arrays

是否可以在一个值中连接关键类型country 例如[genres] => value1,value2,value3 因为存在一个问题,即前一个循环的值被添加到下一个循环

这是printscreen http://s12.postimg.org/sv53nakej/Bez_tytu_u.png

Change:  
require_once __DIR__.'../public/index.php';  
To:  
require_once __DIR__.'../public_html/new_project/index.php';  
Save and close. 

这是我的代码:

  1./////////////////////////////////////////////////////////////////////
           stdClass Object
            (
                [title] => Marsjanin
                [origTitle] => The Martian
                [genres] => Array
                    (
                        [0] => Array
                            (
                                [key] => 28
                                [value] => akcja
                            )

                        [1] => Array
                            (
                                [key] => 33
                                [value] => sci-fi
                            )

                    )

                [country] => Array
                    (
                        [0] => Array
                            (
                                [key] => 53
                                [value] => USA
                            )
                        [1] => Array
                            (
                                [key] => 53
                                [value] => Australia
                            )
                    )

            )

    2./////////////////////////////////////////////////////////////////////

            stdClass Object
            (
                [title] => Pięćdziesiąt twarzy Greya
                [origTitle] => Fifty Shades of Grey
                [genres] => Array
                    (
                        [0] => Array
                            (
                                [key] => 16
                                [value] => melodramat
                            )
                        [1] => Array
                            (
                                [key] => 33
                                [value] => sci-fi
                            )
                    )

                [country] => Array
                    (
                        [0] => Array
                            (
                                [key] => 53
                                [value] => USA
                            )


                    )


            )

上一个循环的值将添加到下一个

离。

  1. A
  2. C
  3. d
  4. 结果:

    A = A

    A + B = B

    A + B + C = C

    A + B + C + D = D

    输出[genres]

    foreach( $urll as $urle){
    
    $results = My_Parser::getMovie($urle);
    
            foreach($results as $itemz => $valuez) {
            if($itemz=='genres'){
            foreach($valuez as $val) {
                $genr[] = $val['value'];
            }}
            }
    
            $genre = implode(', ', $genr);
    
            echo '<tr><th>'.$genre.'</th><th>Importuj</th></tr>'; 
    
    }
    

2 个答案:

答案 0 :(得分:0)

您的$genr会不断被追加。在进入嵌套循环之前重置它:

foreach( $urll as $urle){
    ...
    $genr = array(); // Add this!
    foreach($results as $itemz => $valuez) {
       ...
    }
    ...
    $genre = implode(', ', $genr); // Now you're good.
    ...
}

答案 1 :(得分:0)

您需要在foreach($valuez as $val)

的foreach之前初始化数组
 foreach( $urll as $urle){

    $results = My_Parser::getMovie($urle);

        foreach($results as $itemz => $valuez) {
            if($itemz=='genres'){
             $genr=array();  //initialization
            foreach($valuez as $val) {
                $genr[] = $val['value'];
                }
               $genre = implode(', ', $genr); // use the variable -- As per as your requirement.
               echo '<tr><th>'.$genre.'</th><th>Importuj</th></tr>'; 
             }

            }

    }