[英]How do you split a string of pipe separated values and return an array?
I have an array like the following: 我有一个像下面这样的数组:
array(1) {
[0]=>
string(160) "|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|"
}
I'm trying to find a way to strip the pipes and turn them each into an array. 我正在尝试找到一种方法来剥离管道并将它们每个都变成一个数组。
Here's the code that will output the results. 这是将输出结果的代码。
<?php
if( in_array( 'gb', get_field('rights_management_control_by_continent_europe') ) or 'gb' == get_field('rights_management_control_by_continent') ) {
?>
STUFF HERE
<?php } ?>
And just out of curiosity, is this doable in JavaScript? 只是出于好奇,这在JavaScript中可行吗?
In Javascript you can split string to array by using 在Javascript中,您可以使用
s = "a|b|c"
arr = s.split('|')
//access your array
arr[0]
arr[1]
.....
Use the PHP explode
tag. 使用PHP
explode
标签。
<?php
$arr = ["|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|"];
$pieces = explode("|", $arr[0]);
Each item separated by the pipe symbol would be a new item in the away, with ad being [1] as you start with a pipe. 用管道符号分隔的每个项目都是遥不可及的新项目,从管道开始,广告为[1]。
[ and ] can start and close an array [和]可以启动和关闭数组
So you have this array, I'll just put it in a variable $old_array
: 因此,您有了这个数组,我将其放在变量
$old_array
:
$old_array = array(0=>"|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|");
To split the string on index 0 we use the explode function on the first element in the $old_array
array: 要在索引0上拆分字符串,我们在
$old_array
数组的第一个元素上使用explode函数 :
$exploded_array = explode("|", $old_array[0]);
The variable $exploded_array
will now hold an array with all the pairs of letters as separate elements: 变量
$exploded_array
现在将包含一个数组,其中所有字母对都作为单独的元素:
["","ad","al","at","ax","ba","be",...]
In JavaScript it would look a little different but still similar: 在JavaScript中,看起来有些不同,但仍然相似:
var old_array = ["|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|"];
var split_array = old_array[0].split('|');
The split_array
variable will contain all the pairs of letters as separate elements: split_array
变量将包含所有字母对作为单独的元素:
["","ad","al","at","ax","ba","be",...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.