Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分解数组并运行foreach循环_Php - Fatal编程技术网

Php 分解数组并运行foreach循环

Php 分解数组并运行foreach循环,php,Php,我的数据库中存储了以下字符串:[gallery id=“442443444”] 我想从ids中获取值,即442,443,444并运行foreach循环 所以它基本上会遍历ids中的所有整数 如何使用PHP实现这一点?您需要首先集体提取ID,然后将它们生成一个数组 例如,可以使用双引号拆分字符串: list (,$rawIds) = preg_split('#"#', $string); 然后分解ID: $ids = preg_split('#,\\s*#', $rawIds); \s*处理可

我的数据库中存储了以下字符串:
[gallery id=“442443444”]

我想从
ids
中获取值,即
442
443
444
并运行foreach循环

所以它基本上会遍历
ids
中的所有整数


如何使用PHP实现这一点?

您需要首先集体提取ID,然后将它们生成一个数组

例如,可以使用双引号拆分字符串:

list (,$rawIds) = preg_split('#"#', $string);
然后分解ID:

$ids = preg_split('#,\\s*#', $rawIds);
\s*处理可能的空白(例如“21,22,23,24,25”)

此时,您使用
$ids
作为数组:

foreach ($ids as $index => $id) {
    print "ID #{$index} is {$id}\n";
}

ID #0 is 5
ID #1 is 8
ID #2 is 12

但是,您最好验证字符串是如何创建的:例如,如果它是JSON,那么最好使用
JSON\u decode($string,true)

您需要首先集体提取ID,然后将其放入数组中

例如,可以使用双引号拆分字符串:

list (,$rawIds) = preg_split('#"#', $string);
然后分解ID:

$ids = preg_split('#,\\s*#', $rawIds);
\s*处理可能的空白(例如“21,22,23,24,25”)

此时,您使用
$ids
作为数组:

foreach ($ids as $index => $id) {
    print "ID #{$index} is {$id}\n";
}

ID #0 is 5
ID #1 is 8
ID #2 is 12

但是,您最好验证字符串是如何创建的:例如,如果它是JSON,那么最好使用
JSON\u decode($string,true)

您需要首先集体提取ID,然后将其放入数组中

例如,可以使用双引号拆分字符串:

list (,$rawIds) = preg_split('#"#', $string);
然后分解ID:

$ids = preg_split('#,\\s*#', $rawIds);
\s*处理可能的空白(例如“21,22,23,24,25”)

此时,您使用
$ids
作为数组:

foreach ($ids as $index => $id) {
    print "ID #{$index} is {$id}\n";
}

ID #0 is 5
ID #1 is 8
ID #2 is 12

但是,您最好验证字符串是如何创建的:例如,如果它是JSON,那么最好使用
JSON\u decode($string,true)

您需要首先集体提取ID,然后将其放入数组中

例如,可以使用双引号拆分字符串:

list (,$rawIds) = preg_split('#"#', $string);
然后分解ID:

$ids = preg_split('#,\\s*#', $rawIds);
\s*处理可能的空白(例如“21,22,23,24,25”)

此时,您使用
$ids
作为数组:

foreach ($ids as $index => $id) {
    print "ID #{$index} is {$id}\n";
}

ID #0 is 5
ID #1 is 8
ID #2 is 12

但是,您最好验证字符串是如何创建的:例如,如果它是JSON,那么最好使用
JSON\u decode($string,true)
您可以尝试以下小脚本:

<?php
// get the value from the Database
$ids = $record;  // get some procedure to get the value from the database.
// explode the value to get the list ids
$list = explode("=",$ids);
// get the list ids only
$elements = str_replace("\"","",$list[1]);
// extract all ids from the list
$array_list = explode(",",$elements);
// trigger loop to get each value
foreach($array_list  as $ value){
 echo $value."<br />"; // value for each element, i.e. 442, 443, 444,....
}
?>

您可以尝试以下小脚本:

<?php
// get the value from the Database
$ids = $record;  // get some procedure to get the value from the database.
// explode the value to get the list ids
$list = explode("=",$ids);
// get the list ids only
$elements = str_replace("\"","",$list[1]);
// extract all ids from the list
$array_list = explode(",",$elements);
// trigger loop to get each value
foreach($array_list  as $ value){
 echo $value."<br />"; // value for each element, i.e. 442, 443, 444,....
}
?>

您可以尝试以下小脚本:

<?php
// get the value from the Database
$ids = $record;  // get some procedure to get the value from the database.
// explode the value to get the list ids
$list = explode("=",$ids);
// get the list ids only
$elements = str_replace("\"","",$list[1]);
// extract all ids from the list
$array_list = explode(",",$elements);
// trigger loop to get each value
foreach($array_list  as $ value){
 echo $value."<br />"; // value for each element, i.e. 442, 443, 444,....
}
?>

您可以尝试以下小脚本:

<?php
// get the value from the Database
$ids = $record;  // get some procedure to get the value from the database.
// explode the value to get the list ids
$list = explode("=",$ids);
// get the list ids only
$elements = str_replace("\"","",$list[1]);
// extract all ids from the list
$array_list = explode(",",$elements);
// trigger loop to get each value
foreach($array_list  as $ value){
 echo $value."<br />"; // value for each element, i.e. 442, 443, 444,....
}
?>

试试这种方法

$txt = '[gallery ids="442,443"]';
preg_match_all('!\d+!', $txt, $match);
print_r($match); // get all matches data in an array
这样试试

$txt = '[gallery ids="442,443"]';
preg_match_all('!\d+!', $txt, $match);
print_r($match); // get all matches data in an array
这样试试

$txt = '[gallery ids="442,443"]';
preg_match_all('!\d+!', $txt, $match);
print_r($match); // get all matches data in an array
这样试试

$txt = '[gallery ids="442,443"]';
preg_match_all('!\d+!', $txt, $match);
print_r($match); // get all matches data in an array

这太完美了。你能解释一下我是如何在每个ID上使用foreach循环的吗?这太完美了。你能解释一下我是如何在每个ID上使用foreach循环的吗?这太完美了。你能解释一下我是如何在每个ID上使用foreach循环的吗?这太完美了。你能解释一下我是如何在每个ID上使用foreach循环的吗?