php将基本数组转换为基于元素序列的数组,数组解析

php将基本数组转换为基于元素序列的数组,数组解析

问题描述:

I've used simple_html_dom.php to create an array of td element values. I'd like re-organize this array into something I can work, and not just a simple list.

The array looks like this (I have shortened the amount of attributes to simplify):

ret[0] = 1       // player position
ret[1] = Tiger Woods  // player name
ret[3] = 68    // score
ret[4] = 2     // start of second record, player #2 position
ret[5] = Phil Mickleson // player #2 name
ret[6] = 69     // player #2 score

I'd like to turn this into another array:

$ret['position'] = 1,2
$ret['name'] = Tiger Woods, Phil Mickleson
$ret['score'] = 68, 69

How do I take: elements 0, 4 and place them into position elements 1, 5 and place them into name elements 6, 3 and place them into score

Each record contains the same amount of attributes. Please consider that my complete list of attributes is 10 not 3 as shown above.

Here is the actual code I have so far, it works and will give the array I'm working with. I would be open to any solutions that makes better use of simple_html_dom.php

$html = file_get_html('http://sports.yahoo.com/golf/pga/leaderboard');

// Use simple_html_dom to find all td elements as an array

$ret = $html->find('td');

// Code to remove all unwanted td elements from the array

unset($ret[0]);
unset($ret[1]);
$rem=count($ret)-1;
unset($ret[$rem]);

// Code to display all values in the array

foreach($ret as $value){
echo $value . '<br>';
}

Thank you much!

-Bryan

我使用simple_html_dom.php创建了一个td元素值数组。 我想将这个数组重新组织成我可以工作的东西,而不仅仅是一个简单的列表。 p>

数组看起来像这样(我缩短了要简化的属性数量): p>

  ret [0] = 1 //玩家位置
ret [1] =老虎伍兹//球员姓名
ret [3] = 68 //得分
ret [4  ] = 2 //开始第二个记录,第2个玩家位置
ret [5] = Phil Mickleson //玩家#2名称
ret [6] = 69 //玩家#2得分
  code>  pre  > 
 
 

我想把它变成另一个数组: p>

  $ ret ['position'] = 1,2 
 $ ret ['  name'] = Tiger Woods,Phil Mickleson 
 $ ret ['score'] = 68,69 
  code>  pre> 
 
 

我如何处理:元素0,4和位置 将它们放入位置 elements 1,5并将它们放入名称 elements 6,3并将它们放入分数 p>

每条记录包含相同数量的属性。 请考虑我的完整属性列表是10而不是3如上所示。 p>

这是我到目前为止的实际代码,它可以工作并将给出我正在使用的数组。 我愿意接受任何更好地利用simple_html_dom.php的解决方案 p>

  $ html = file_get_html('http://sports.yahoo.com/golf/pga/leaderboard  '); 
 
 //使用simple_html_dom查找所有td元素作为数组
 
 $ ret = $ html-&gt; find('td'); 
 
 //删除所有不需要的td的代码 数组中的元素
 
unset($ ret [0]); 
unset($ ret [1]); 
 $ rem = count($ ret)-1; 
unset($ ret [$ rem]);  
 
 //代码显示数组中的所有值
 
foreach($ ret as $ value){
echo $ value。  '&lt; br&gt;'; 
} 
  code>  pre> 
 
 

非常感谢! p>

-Bryan p> DIV>

Iterate through the rows of the table, not all the TDs.

$ret = array('position' => array(),
             'name' => array(),
             'score' => array());
$rows = $html->find('table.sportsTable tbody tr');
foreach ($rows as $row) {
    $cells = $row->find('td');
    $ret['position'][] = $cells[0]->plaintext;
    $ret['name'][] = $cells[1]->plaintext;
    $ret['score'][] = $cells[2]->plaintext;
}

$ret['position'] = implode(', ', $ret['position']);
$ret['name'] = implode(', ', $ret['name']);
$ret['score'] = implode(', ', $ret['score']);

$newret = array_combine (
    array('position', 'name', 'score'),
    call_user_func_array(
        'array_map',
        array_merge(array(NULL),array_chunk($ret, 3))
    )
);
var_dump($newret);

EDIT

If you want a comma separated list instead of a nested array:

$newret = array_map(
    function ($value) { return implode(', ', $value); },
    array_combine (
        array('position', 'name', 'score'),
        call_user_func_array(
            'array_map',
            array_merge(array(NULL),array_chunk($ret, 3))
        )
    )
);

var_dump($newret);

Final code:

<!DOCTYPE html>
<html>
<body>

<?php include 'simple_html_dom.php'; ?>

<?php

// Create DOM from Yahoo Sports URL
$html = file_get_html('http://sports.yahoo.com/golf/pga/leaderboard');

$ret = array('position' => array(),
             'name' => array(),
             'rd_one_score' => array());
$rows = $html->find('table.sportsTable tbody tr');

$count = 0;
foreach ($rows as $row) {
    $cells = $row->find('td');
    $ret['position'][] = $cells[0]->plaintext;
    $ret['name'][] = $cells[1]->plaintext;
    $ret['rd_one_score'][] = $cells[2]->plaintext;
    $count = $count+1;

}

// Build table form array

echo "<table>";
echo "<tr><td>Position</td><td>Name</td><td>Score</td></tr>";
for ($x=1; $x<=$count; $x++) {
echo "<tr><td>",  $ret['position'][$x], "</td>";
echo "<td>",  $ret['name'][$x],  "</td>";
echo "<td>",  $ret['rd_one_score'][$x],  "</td></tr>";
} 
echo "</table>";


?>


</body>
</html>

Let's imagine that simple_html_dom were the library that it should have been:

$dom = file_get_html('http://sports.yahoo.com/golf/pga/leaderboard');
$ret = array();
$ret['position'] = implode(',', $dom->find('.sportsTable tbody tr td:eq(1)')->trim_texts);
$ret['name'] = implode(',', $dom->find('.sportsTable tbody tr td:eq(2)')->trim_texts);
$ret['score'] = implode(',', $dom->find('.sportsTable tbody tr td:eq(3)')->trim_texts);

var_dump($ret);