Ben*_*der 6 php simple-html-dom
我有点'生锈与PHP,因为它发生有时我使用它几个星期有时它会发生你几个月没用.无论哪种方式,我试图传递另一个数组的值是"数组",在另一个数组上有序地...我想要做的主要是创建一个键,允许我每行组织增量值,特别是;
数组内容
Array
(
[key] => value
[2] => 1
[3] => Inter
[4] => 4
[5] => 4
[6] => 0
[7] => 0
[8] => 5
[9] => 1
[10] => +4
[11] => 12
[12] => Chievo Verona - Inter 0 - 1
[13] => Inter - Milan 1 - 0
[14] => Carpi - Inter 1 - 2
[15] => Inter - Atalanta 1 - 0
[16] => ;
[17] => 2
[18] => Torino
[19] => 4
[20] => 3
[21] => 1
[22] => 0
[23] => 9
[24] => 4
[25] => +5
[26] => 10
[27] => Torino - Sampdoria 2 - 0
[28] => Hellas Verona - Torino 2 - 2
[29] => Torino - Fiorentina 3 - 1
[30] => Frosinone - Torino 1 - 2
[31] => ;
[32] => 3
[33] => Fiorentina
[34] => 4
[35] => 3
[36] => 0
[37] => 1
[38] => 5
[39] => 3
[40] => +2
[41] => 9
[42] => Carpi - Fiorentina 0 - 1
[43] => Fiorentina - Genoa 1 - 0
[44] => Torino - Fiorentina 3 - 1
[45] => Fiorentina - Milan 2 - 0
[46] => ;
[47] => 4
[48] => Roma
[49] => 4
[50] => 2
Run Code Online (Sandbox Code Playgroud)
";" 它需要能够识别你在哪里断线,我不记得是否有任何方法允许我访问下一个键.
目前我的代码是:
$classifica = array("key" => "value");
function buildArrayClassifica()
{
global $array;
global $classifica;
$i = 0;
foreach(array_slice($array,1) as $key => $value)
{
if($value != ";")
{
array_push($classifica[$i], $value); //there is a problem
echo $value . " ";
}
else if($value == "value ")
{
continue;
}
else
{
$i++;
echo "<br/>";
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码我将返回此错误:
警告:array_push()将参数1视为数组,在...中给出null
特别是在array_push上,它似乎不接受增量键,或者我做错了.谁能告诉我怎么解决?
UPDATING
正如你所看到的那样,这个问题并不简单,很难解释这个问题,但我会尽量让自己更清楚.如上所示,您是数组"数组"的结构,但是是一个无序结构,需要在另一个数组中进行排序.概括一下数组"array"的结构是:
1 , Inter , 4 , 4 , 0 , 0 , 5 , 1 , +4 , 12 , Chievo Verona - Inter 0 - 1 , Inter - Milan 1 - 0 , Carpi - Inter 1 - 2 , Inter - Atalanta 1 - 0 , ;
Run Code Online (Sandbox Code Playgroud)
";" 该行已完成.所以接近";"的下一个值 意味着新的生产线即将到来.我需要的是移动"array"数组中的所有值classifica,但我想组织它们:
ROW1 => 1 , Inter , 4 , 4 , 0 , 0 , 5 , 1 , +4 , 12 , Chievo Verona - Inter 0 - 1 , Inter - Milan 1 - 0 , Carpi - Inter 1 - 2 , Inter - Atalanta 1 - 0
ROW2 => Other values...
Run Code Online (Sandbox Code Playgroud)
所以ROW1,2 ..代表了数组的关键classifica.我试图将值推入一行,然后增加$ i,index但代码不会添加值,因为索引会在循环中替换实际的键,例如:
actual foreach content:
$i = 0
value = "Inter"
content of array=> [0] => Inter
now the $i is ever 0 because the row isn't finished yet, the ";"
it has not yet been reached, so the next content of foreach is:
"1" but replace the "Inter" value, so this is a problem.
Run Code Online (Sandbox Code Playgroud)
你不能用array_push()这种方式。请尝试:
$classifica = array();
function buildArrayClassifica()
{
global $array;
global $classifica;
$i = 0;
foreach(array_slice($array,1) as $key => $value)
{
if($value != ";")
{
$classifica[$i] = $value;
echo $value . " ";
}
else if($value == "value ")
{
continue;
}
else
{
$i++;
echo "<br/>";
}
}
}
Run Code Online (Sandbox Code Playgroud)
当添加到数组时,这将创建索引( 的值$i) 。会将 放置在下一个数字索引处,但从外观来看可能不是您想要的。如果您希望索引匹配,也可以使用。$valuearray_push()$value$key
编辑
经过更多讨论,您有了一个特定的格式,其中第一个 Item 是 Key,后面的索引是值,当遇到值“;”时,它会重新开始序列。所以当我们读到:
[2] => 1
[3] => Inter
[4] => 4
[5] => 4
[6] => 0
[7] => 0
[8] => 5
[9] => 1
[10] => +4
[11] => 12
[12] => Chievo Verona - Inter 0 - 1
[13] => Inter - Milan 1 - 0
[14] => Carpi - Inter 1 - 2
[15] => Inter - Atalanta 1 - 0
[16] => ;
Run Code Online (Sandbox Code Playgroud)
第一个值“1”是我们的索引,后面的值成为该索引的值,当我们找到“;”时我们停止阅读。那看起来像:
<?php
function buildArrayClassifica($dataArray){
$resultArray = array();
$t = array_values($dataArray);
print_r($t);
$lsc = 0;
foreach($t as $k => $v){
if((string)$v == ';'){
echo "<p>Found ';' at [$k] => {$v}</p>";
// Found end of data
// Save position
$scp = $k;
echo "<p>Recorded [$scp] position for ';'.</p>";
// Reset to find the Index, first int in this series
$c=$lsc; // First pass this should be 0
// Set the index
if($lsc ==0){
// First pass
$index = intval($t[$c]);
echo "<p>Getting Index from position [" . ($c) ."] => $index for Result Array.</p>";
$c++;
} else {
$c++;
$index = intval($t[$c]);
echo "<p>Getting Index from position [" . ($c) ."] => $index for Result Array.</p>";
$c++;
}
echo "<p>Starting to read data from [$c] until [$scp].</p>";
// Init implode variable
$data = "";
for($c;$c<$scp;$c++){
//Populate variable with the series up to semicolon, skipping first element (index)
$data .= $t[$c] . ", ";
}
echo "<p>Data collected for this round: '" . htmlentities(substr($data,0,-2)) . "'</p>";
// populate result array
$resultArray[$index] = substr($data,0,-2);
echo "<p>resultArray[$index] => " . htmlentities($resultArray[$index]) . "</p><br />";
$lsc = $scp;
}
}
return $resultArray;
}
$oldArray = array(1, "Inter", 4 , 4 , 0 , 0 , 5 , 1 , "+4" , 12 , "Chievo Verona - Inter 0 - 1", "Inter - Milan 1 - 0", "Carpi - Inter 1 - 2", "Inter - Atalanta 1 - 0", ";", 2, "Torino", 4, 3, 1, 0, 9, 4, '+5', 10, "Torino - Sampdoria 2 - 0", "Hellas Verona - Torino 2 - 2", "Torino - Fiorentina 3 - 1", "Frosinone - Torino 1 - 2", ";", 3, "apple", 0, 4, 6, "apple", ";");
$classifica = buildArrayClassifica($oldArray);
print_r($classifica);
?>
Run Code Online (Sandbox Code Playgroud)
我的初步测试似乎适用于您所描述的情况。数组的第一个元素成为索引,接下来的几个值会内爆,直到达到分号 ( ;) 值。
我看到的结果是:
Array ( [1] => Inter, 4, 4, 0, 0, 5, 1, +4, 12, Chievo Verona - Inter 0 - 1, Inter - Milan 1 - 0, Carpi - Inter 1 - 2, Inter - Atalanta 1 - 0 [2] => Torino, 4, 3, 1, 0, 9, 4, +5, 10, Torino - Sampdoria 2 - 0, Hellas Verona - Torino 2 - 2, Torino - Fiorentina 3 - 1, Frosinone - Torino 1 - 2 [3] => apple, 0, 4, 6, apple )
Run Code Online (Sandbox Code Playgroud)
在旁边
如果是我,我会把它全部推入一个数组,如下所示:
$data = array();
for($c;$c<$scp;$c++){
$data[] = $t[$c];
}
$resultArray[$index] = $data;
Run Code Online (Sandbox Code Playgroud)
或者如果你真的想要一个字符串:
$resultArray[$index] = implode(", ", $data);
Run Code Online (Sandbox Code Playgroud)
希望有帮助。
| 归档时间: |
|
| 查看次数: |
1923 次 |
| 最近记录: |