Reputation: 119
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
Upvotes: 2
Views: 66
Reputation: 163217
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%
)
)
Upvotes: 0
Reputation: 245
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%
)
)
Upvotes: 2
Reputation: 26844
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%
)
)
Upvotes: 2
Reputation: 133360
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)];
}
Upvotes: 2