如何使用php获取第三行两个字符之间的字符串


$abc = "['Fund', 'Amount'],
['Seed Fund (Investor)', 50000],
['Loan Fund (Spartan)', 50000],
['Profit (Investor)', 3000],";

如何使用php来获得"贷款基金(斯巴达(";和50000从上面的字符串?

使用foreach循环?

使用数组?

使用$substring((?

有更好的解决方案吗?

由于它是一个字符串,您不能循环它,但您可以将它变成一个数组,然后循环它:

// Prepare the string to represent a JSON object
$abc = str_replace("'", '"', trim($abc, ','));
// Actually turn it into a string representation of a JSON object
$abc = '[['.$abc.']]';
// Turn it into an array by json_decoding it
$abc = json_decode($abc, true);
// Grab the initial data
$abc = array_pop($abc);
// Walk the data
foreach ($abc as $set) {
if (in_array('Loan Fund (Spartan)', $set)) {
...
}
}

尽管这只是为了好玩——但首先要尝试获得更有用的数据表示(一个数组,一个真正的JSON对象/字符串(。

最新更新