簡體   English   中英

在 PHP 中創建關聯數組

[英]Creating an associative array in PHP

我是編程初學者; 我在從現有關聯數組創建關聯數組時卡住了

<?php
    $arr = array( 
            array(
                "question"=>"I get irritated easily.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I spend time reflecting on things.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I am quiet around strangers.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I make people feel at ease.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I am exacting in my work.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I often feel blue.",
                "answer"=>"3"
            ),
            array(
                "question"=>"I am full of ideas.",
                "answer"=>"4"
            )
        );
     
     
     $answer_array = array();
     foreach( $arr as $questions ) {
       $answer_array['text']=$questions['answer'];
     }
     print_r( $answer_array );
?>

我想要以下格式的 $answer_array:$answer_array("text"=>"1, 1, 1, 1, 1,3, 4")

但是我沒有得到正確的答案,因為它以下列方式顯示:

Array
(
    [text] => 4
)

這是因為它在迭代時會覆蓋所有其他值,並且只存儲最后一個值。 我需要存儲上面提到的所有值。 請建議我哪里出錯了。

您可以嘗試通過更改來連接這些值

 $answer_array['text']=$questions['answer'];

if(isset($answer_array['text'])){
  $answer_array['text'] .= ', '. $questions['answer'] ;
} else {
 $answer_array['text'] = $questions['answer'];
}

您可以嘗試使用array_map來獲取每個問題的答案。

然后您可以使用implode通過分隔符連接字符串,在本例中為","

     $answer_array = array(
         // Implode joins the strings
         'text'=>implode(
             // Replace this comma by another delimiter if you like
             ',',
             // Array_map applies a function to every element in a array
             array_map(
                 // Define a function that gets the answer to each question
                 fn($arr)=>$arr["answer"],
                 $arr
             )
         )
     );
     print_r( $answer_array );

在線嘗試

您可以使用 array_column function

// Enter your code here, enjoy!
$arr = array( 
            array(
                "question"=>"I get irritated easily.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I spend time reflecting on things.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I am quiet around strangers.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I make people feel at ease.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I am exacting in my work.",
                "answer"=>"1"
            ),
            array(
                "question"=>"I often feel blue.",
                "answer"=>"3"
            ),
            array(
                "question"=>"I am full of ideas.",
                "answer"=>"4"
            )
        );
        
        
        print_r(array_column($arr, 'answer'));

https://onlinephp.io/c/51126

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM