我喜欢将单个数组转换为多维数组.这是我得到的网页抓取页面,除了它不是我正在寻找的最终结果.
更改:
Rooms: Array (
[0] => name
[1] => value
[2] => size
[3] =>
[4] => name
[5] => value
[6] => size
[7] =>
[8] => name
[9] => value
[10] => size
[11] =>
[12] => name
[13] => value
[14] => size
[15] =>
)
成:
Rooms: Array (
Room: Array (
[0] => name
[1] => value
[2] => size
),
Room: Array (
[0] => name
[1] => value
[2] => size
),
Room: Array (
[0] => name
[1] => value
[2] => size
)
)
解决方法:
首先使用array_filter()来摆脱& nbsp;节点:
$array = array_filter($array, function($x) { return trim($x) != ' '; });
// Or if your PHP is older than 5.3
$array = array_filter($array, create_function('$x', 'return trim($x) != " ";'));
然后使用array_chunk()将数组拆分为3个块:
$array = array_chunk($array, 3);
这当然假设您将始终只按顺序获取包含名称,值和大小的元组.
标签:php,arrays,multidimensional-array
来源: https://codeday.me/bug/20190518/1128839.html