Is there any possible way to dynamically build array in one single line to use it inside http_build_query function?
This would be one of scenarios:
//1. Initial array state
$array = array('value1' => 1);
//2. Clicks on "Sort by name ASC"
$array = array('value1' => 1, 'sort' => array('name' => 'ASC'));
//3. Clicks on "Sort by timestamp DESC"
$array = array('value1' => 1, 'sort' => array('name' => 'ASC', 'timestamp' => 'DESC'));
//4. Clicks on "Sort by name DESC"
$array = array('value1' => 1, 'sort' => array('name' => 'DESC', 'timestamp' => 'DESC'));
//5. Clicks on "Sort by timestamp ASC"
$array = array('value1' => 1, 'sort' => array('name' => 'ASC', 'timestamp' => 'ASC'));
This is what I tried so far:
http_build_query(array_merge($arr, array('sort' => array('email' => 'ASC'))));
http_build_query(array_merge($arr, array('sort' => array('timestamp' => 'ASC'))));
But of course, every time I use this line with different values, everything in $arr['sort'] is overwritten and only single value is kept there.
question from:
https://stackoverflow.com/questions/65905563/any-possible-way-to-build-arrays-in-one-single-line-for-http-build-query-functio 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…