我有一组可变长度的键名,我想使用这些名称为数组赋值
例如,我有一个列出不同品牌的汽车和自行车数量的数组:
$vehicles = [
'cars' => Array
(
'suzuki' => Array
(
'wagon' => 4,
'baleno' => 2
),
'honda' => Array
(
'civic' => 6
)
),
'bikes' => array
(
'raleigh' => 3,
'scott' => 3
)
];
我还有几个键名数组以及要放在主数组中的值:
$keys1 = ['cars', 'honda', 'jazz'];
$value1 = 3;
$keys2 = ['bikes', 'scott'];
$value2 = 1;
$keys3 = ['motorbikes', 'harley-davidson', 'dyna', 'street-bob'];
$value3 = 2;
输入所有这些值后,数组应如下所示:
$vehicles = [
'cars' => Array
(
'suzuki' => Array
(
'wagon' => 4,
'baleno' => 2
),
'honda' => Array
(
'civic' => 6,
'jazz' => 3
)
),
'bikes' => array
(
'raleigh' => 3,
'scott' => 1
),
'motorbikes' => Array
(
'harley-davidson' => Array
(
'dyna' => Array
(
'street-bob' => 2
)
)
)
];
因此,第一个数组会添加一个$key => $value
对,其中之前没有一个。第二个替换$array2
末尾的键值,最后一个在没有开始时创建一个新数组。
如何以这种方式填充数组?
eval()
可以解决我的所有问题,但数组是从html创建的,因此存在巨大的安全风险。
答案 0 :(得分:1)
构建键/值对列表的便捷方式(在我看来)如下:
onResponse
以下是使用public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View drawer = inflater.inflate(R.layout.fragment_pending, container, false);
pendingnullorder = (TextView) drawer.findViewById(R.id.pendingnullorder);
recyclerView = (RecyclerView) drawer.findViewById(R.id.pending);
progressBar = (ProgressBar) drawer.findViewById(R.id.progressBar);
/*recyclerView.setVisibility(View.GONE);*/
progressBar.setVisibility(ProgressBar.VISIBLE);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(inflater.getContext()));
orderLists = new ArrayList<>();
getPendingData();
adapter = new OrderListAdapter(orderLists, inflater.getContext());
recyclerView.setAdapter(adapter);
updateUI();
return drawer;
}
private void getPendingData() {
String token = SharedPreferencesManager.readPreferenceString("token", "D/N");
JSONObject progressData = new JSONObject();
try{
progressData.put("token", token);
JsonObjectRequest progressObject = new JsonObjectRequest(1, Common.OrderDetails + "progress", progressData, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject progressResponse) {
Log.d("Responseprogress", progressResponse.toString());
try {
int status = progressResponse.getInt("status");
if(status == 1) {
progressOrderProgress(progressResponse);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("Response", "PROGRESS ERROR");
}
});
progressObject.setShouldCache(false);
ServiceBellApp.getInstance().addToRequestQueue(progressObject);
}
catch (JSONException localJSONException){
localJSONException.printStackTrace();
return;
}
}
private void progressOrderProgress(JSONObject progressResponse) throws JSONException {
JSONArray result = progressResponse.getJSONArray("orderdata");
for(int i=0; i<result.length(); i++){
OrderList orderListModule = new OrderList();
JSONObject orderData = null;
try {
orderData = result.getJSONObject(i);
orderListModule.setPackage_name(orderData.getString("name"));
orderListModule.setOrderdate(orderData.getString("date"));
orderListModule.setServicedate(orderData.getString("service"));
orderListModule.setServicetime(orderData.getString("time"));
orderListModule.setOrderid(orderData.getString("id"));
orderListModule.setOrdstatus(orderData.getString("status"));
orderListModule.setOrderamount(orderData.getInt("ramount"));
}catch (JSONException e) {
e.printStackTrace();
}
orderLists.add(orderListModule);
}
swipeRefreshLayout.setRefreshing(false);
adapter.notifyDataSetChanged();
updateUI();
}
private void updateUI() {
if (adapter.getCount() == 0) {
pendingnullorder.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
} else {
pendingnullorder.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
}
}
函数和引用的解决方案:
// $vehicles is your initial array
$key_paths = [
['key_path' => ['cars', 'honda', 'jazz'], 'value' => 3],
['key_path' => ['bikes', 'scott'], 'value' => 1],
['key_path' => ['motorbikes', 'harley-davidson', 'dyna', 'street-bob'], 'value' => 2],
];
输出:
array_slice
答案 1 :(得分:1)
您可以使用与$keys
相同的格式转换$vehicles
数组,然后以递归方式替换它们:
$vehicles = [
'cars' => [
'suzuki' => [
'wagon' => 1,
'boleno' => 2
]
],
'bikes' => [
'scott' => 3
]
];
$keys = ['cars', 'honda', 'jazz'];
$value = 3;
function addValues($vehicles, $keys, $value)
{
$formatted = formatArray($keys, $value);
return array_replace_recursive($vehicles, $formatted);
}
function formatArray($array, $value)
{
$format = function ($carry, $item) {
return [$item => $carry];
};
return array_reduce(array_reverse($array), $format, $value);
}
$vehicles = addValues($vehicles, $keys, $value);
var_dump($vehicles);
输出:
array (size=2)
'cars' =>
array (size=2)
'suzuki' =>
array (size=2)
'wagon' => int 1
'boleno' => int 2
'honda' =>
array (size=1)
'jazz' => int 3
'bikes' =>
array (size=1)
'scott' => int 3