在PHP中创建关联数组

7hiiyaii  于 2023-01-16  发布在  PHP
关注(0)|答案(3)|浏览(156)

我是一个初学者的程序设计;从现有关联数组创建关联数组时卡住

<?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采用以下格式:$答案数组(“文本”=〉“1,1,1,1,1,3,4”)
但我没有得到正确的答案,因为它以以下方式显示:

Array
(
    [text] => 4
)

这是因为它在迭代时覆盖了所有其他的值,并且只存储最后一个值。我需要存储所有的值,就像我上面提到的。请告诉我哪里出错了。

g6ll5ycj

g6ll5ycj1#

可以使用array_column函数

// 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

vx6bjr1n

vx6bjr1n2#

您可以尝试通过更改以下内容来连接这些值

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

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

fxnxkyjh3#

您可以尝试使用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 );

Attempt This Online

相关问题