2

I am using simple html dom to get the contents of a table:

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

print_r($array);

The array returned looks like this:

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%
)

I would like to create a new multidimensional array from the array above that skips one row every three rows starting with the first row like this:

New Array (

[1]
    ['address'] => 0xd35a2d8c651f3eba4f0a044db961b5b0ccf68a2d
    ['amount'] => 309953166.54621424
    ['percent'] => 30.9953%
[2]
    ['address'] => 0xe17c20292b2f1b0ff887dc32a73c259fae25f03b
    ['amount'] => 200000001
    ['percent'] => 20.0000%
[3] 
    ['address'] => 0x0000000000000000000000000000000000000000
    ['amount'] => 129336426
    ['percent'] => 12.9336%

)

In the new array, "address" represents [1] [5] and [9] from the original array. "amount" represents [2] [6] and [10], and "percent" represents [3] [7] and [11].

How can I accomplish this? Thank you

1
  • I believe you have accepted the wrong answer. Both the other answers are much faster as it's producing the array "on the fly", instead of first creating the array and then manipulating it. Commented Jul 8, 2018 at 7:14

4 Answers 4

2

You can use array_chunk to chunk the array. Use array_reduce to loop thru the chuncked array. Use array_combine to use the $keys array as the key.

$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>";

This will result to:

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%
        )

)
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming your array is based on 4 different elements as in in your code

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

in the same relative position you could use a for loop

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)];

}

Comments

2

Given the input array in your example this should do what you have asked:

$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);

Producing:

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%
        )
)

Comments

0

Another option might be to use the modulo % operator.

$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);

Result:

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%
        )

)

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.