PHP: Split arrays

China☆狼群 提交于 2019-12-12 03:47:08

问题


I am still a beginner and I need some help. I've got an array like this;

$_POST=

Array ( [0] => aaa@gmail.com [1] => bbb [2] => ccc [3] => ddd [4] => eee [5] => fff [6] => ggg [7] => hhh [8] => iii [9] => jjj [10] => 31 [11] => k )

I want to split the elements up into keys and values, and then get the values and put it into an array.

Then I want to put htmlentities around each value like this:

foreach (something as something){

echo "htmlentities(".$valuearray.")";
}

Can you please help me?


回答1:


What this code does is create an associative array with keys and values. Then we loop through and push values in to the values array.

We also use htmlentities on each value which is pushed.

If we echo first element in the values array it will display value1.

<?php
$arr = [
    "key1" => "value1",
    "key2" => "value2"
];

$valuesArr = [];

foreach ($arr as $key => $value) {
    array_push($valuesArr, htmlentities($value));
}
?>

What you can do is replace my array with your array and change the names in the foreach loop if you need too.




回答2:


foreach($something as $key => $val){
  $a[] = $val;
}

var_dump($a);

Try this code. It will get only the values of that array.




回答3:


You can apply a function such as htmlentities() to the values of an array by using array_map:

$escapedArray = array_map('htmlentities', $_POST);

PHP documentation: array_map



来源:https://stackoverflow.com/questions/31868775/php-split-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!