php - 如何将值拆分为数组
问题描述
如何将值拆分为数组。我有 whmcs api 数据,它看起来像 1、2、3、4、5。我需要将值拆分为数组值建议任何解决方案
我的api数据
Array
(
[result] => success
[totalresults] => 2
[promotions] => Array
(
[promotion] => Array
(
[0] => Array
(
[id] => 6
[code] => 0UP3NU5J7J
[type] => Percentage
[recurring] => 0
[value] => 15.00
[cycles] => One Time,Monthly,Quarterly,Semi-Annually,Annually,Biennially,Triennially,1Years
[appliesto] => 1,2,3
[requires] => 1,2,3
[requiresexisting] => 0
[startdate] => 2018-10-22
[expirationdate] => 2018-10-25
[maxuses] => 0
[uses] => 0
[lifetimepromo] => 0
[applyonce] => 0
[newsignups] => 0
[existingclient] => 0
[onceperclient] => 0
[recurfor] => 0
[upgrades] => 0
[upgradeconfig] => a:4:{s:5:"value";s:4:"0.00";s:4:"type";s:0:"";s:12:"discounttype";s:10:"Percentage";s:13:"configoptions";s:0:"";}
[notes] =>
)
)
)
)
我的代码是
@foreach($response['promotions']['promotion'][0] as $key => $value)
@if($key == 'appliesto')
{{$var=$value}}
@endif
@endforeach
我需要将“appliesto”值拆分为一个数组。请提出任何解决方案
解决方案
不需要循环:-
$response['promotions']['promotion'][0]['appliesto'] = explode(',',$response['promotions']['promotion'][0]['appliesto']);
但是如果promotions
可以有多个子数组然后做: -
@foreach($response['promotions']['promotion'] as &$value)
$value['appliesto'] = explode(',',$value['appliesto']);
@endforeach
参考:-explode()
推荐阅读
- python - 在 telethon.events 中使用“聊天”参数
- android - 如何在 Retrofit2 api 调用中调用 JSONObject?
- html - HTML 辅助功能选项卡无法按预期工作
- javascript - 如何在带有 Hooks 的 react-router v5 中的 POST 请求后重定向
- ms-access - 是否可以根据字段的值更改记录在报告中的显示方式?
- memory-management - MQTT 代理 - CPU 和内存使用过多
- java - 如何在列表日期中获取最大和最小日期就像 java 中的 String ''17.03.2020", ''12.03.2020", ''01.02.2020"
- django - 在 Django 中持续运行 Celery 任务
- python - Python中的日期时间比较
- angular - 我试图通过提供者获取我的本地 json 数据时遇到错误