繁体   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