从常规数组创建多维数组

时间:2018-07-08 06:51:46

标签: php arrays multidimensional-array

我正在使用简单的html dom来获取表的内容:

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<section class="second__offers">
    <div class="container-fluid">
              <div class="card-group">
            <div class="card text-black face">
                <div class="">
                    <h3 class="card-title">Card title</h3>
                    <h4 class="card-subtitle">Card subtitle</h4>
                    <p class="card-text">This is a simple Card example</p>
                    <a href="#" class="btn btn-primary service-btn">About</a>
                </div>
            </div>
            <div class="card text-black body">
                <div>
                    <h3 class="card-title">Card title</h3>
                    <p class="card-text">This is a simple Card example</p>
                    <a href="#" class="btn btn-primary service-btn">About</a>
                </div>
            </div>
            <div class="card text-black">
                <div>
                    <h3 class="card-title">Card title</h3>
                    <p class="card-text">This is a simple Card example</p>
                    <a href="#" class="btn btn-primary service-btn">About</a>
                </div>
            </div>
          </div>
    <div class="card-group">
        ...
     </div>
    </div>
  </section>

返回的数组如下:

foreach($html->find('#maintable .table tr td') as $a) {
     $array[] = $a->plaintext;
}

print_r($array);

我想从上面的数组创建一个新的多维数组,这样从第一行开始每三行跳过一行:

Array ( 
    [0] => 1 
    [1] => 0xd35a2d8c651f3eba4f0a044db961b5b0ccf68a2d
    [2] => 309953166.54621424
    [3] => 30.9953%
    [4] => 2
    [5] => 0xe17c20292b2f1b0ff887dc32a73c259fae25f03b
    [6] => 200000001
    [7] => 20.0000%
    [8] => 3
    [9] => 0x0000000000000000000000000000000000000000
    [10] => 129336426
    [11] => 12.9336%
)

在新数组中,“地址”代表原始数组中的[1] [5]和[9]。 “金额”代表[2] [6]和[10],“百分比”代表[3] [7]和[11]。

我该如何完成?谢谢

4 个答案:

答案 0 :(得分:2)

您可以使用array_chunk对数组进行分块。使用array_reduce遍历分块数组。使用array_combine$keys数组用作键。

$array = //Your array
$keys = array( 'address', 'amount', 'percent' );
$result = array_reduce( array_chunk( $array, 4 ), function($c, $o) use ($keys) {
    $c[ array_shift($o) ] = array_combine( $keys, $o );
    return $c;
}, array() );

echo "<pre>";
print_r( $result );
echo "</pre>";

这将导致:

Array
(
    [1] => Array
        (
            [address] => 0xd35a2d8c651f3eba4f0a044db961b5b0ccf68a2d
            [amount] => 309953166.54621424
            [percent] => 30.9953%
        )

    [2] => Array
        (
            [address] => 0xe17c20292b2f1b0ff887dc32a73c259fae25f03b
            [amount] => 200000001
            [percent] => 20.0000%
        )

    [3] => Array
        (
            [address] => 0x0000000000000000000000000000000000000000
            [amount] => 129336426
            [percent] => 12.9336%
        )

)

答案 1 :(得分:2)

假设您的数组基于代码中的4个不同元素

foreach($html->find('#maintable .table tr td') as $a) {
     $array[] = $a->plaintext;
}

在相同的相对位置,您可以使用for循环

for ($i =0; $i<(count($array)/4) ; $i++){
  $myNewArra[$i]['address'] = $array[($i*4+1)];
  $myNewArra[$i]['amount'] = $array[($i*4+2)];
  $myNewArra[$i]['percent'] = $array[($i*4+3)];

}

答案 2 :(得分:2)

给出示例中的输入数组,它应该按照您的要求进行操作:

$newArray = [];
for($i=1; $i<count($oldArray); $i=($i+4)){
    $newArray[] = [
        'address'   => (isset($oldArray[$i]))     ? $oldArray[$i] : '',
        'amount'    => (isset($oldArray[($i+1)])) ? $oldArray[($i+1)] : '',
        'percent'   => (isset($oldArray[($i+2)])) ? $oldArray[($i+2)] : '',
    ];
}

print_r($newArray);

制作:

Array
(
    [0] => Array
        (
            [address] => 0xd35a2d8c651f3eba4f0a044db961b5b0ccf68a2d
            [amount] => 309953166.54621424
            [percent] => 30.9953%
        )

    [1] => Array
        (
            [address] => 0xe17c20292b2f1b0ff887dc32a73c259fae25f03b
            [amount] => 200000001
            [percent] => 20.0000%
        )

    [2] => Array
        (
            [address] => 0x0000000000000000000000000000000000000000
            [amount] => 129336426
            [percent] => 12.9336%
        )
)

答案 3 :(得分:0)

另一种选择可能是使用modulo List<Integer> result = slashString.stream() .map(x-> Arrays.stream(x.split("/")) .skip(1) .collect(Collectors.toList())) .filter(x -> x.size() >= 3) .map(list -> Integer.valueOf(list.get(2))) .collect(Collectors.toList()); 运算符。

%

结果:

$innerKeys = ["address", "amount", "percent"];
$result = [];

for ($i = 0; $i < count($array); $i++) {
    $rem = $i % 4;
    if ($rem === 0) $outerKey = $array[$i];
    if ($rem > 0) $result[$outerKey][$innerKeys[$rem - 1]] = $array[$i];
}
print_r($result);

Demo