php - 如何使用 php 在第三行的两个字符之间获取字符串?
问题描述
$abc = "['Fund', 'Amount'],
['Seed Fund (Investor)', 50000],
['Loan Fund (Spartan)', 50000],
['Profit (Investor)', 3000],";
如何使用 php 从上面的字符串中获取“Loan Fund (Spartan)”和 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 对象/字符串)。
推荐阅读
- java - Java:无法在尝试之外返回变量?
- prolog - 如何在序言中创建一个包含从 1 到 N 的元素的列表
- java - 使用自定义异常将函数转换为可完成的
- javascript - 如何将按钮克隆父 div 到正文
- javascript - 为什么从我的笔记本电脑上的节点服务器发送多个 http 请求会扭曲互联网访问
- reactjs - 如何在 Flatlish 中随机选择项目?
- java - 在Java中为什么数字操作的结果与变量操作不同
- firebase - 写入 Firebase Firestore 被拒绝 - Flutter
- c# - 在 C# 中使用 Process.Start
- powershell - Powershell不显示RSS提要