There are three array of the form:
Array (
[0] => NAME
[1] => NAME
[n] => n
)
Array (
[0] => DATE
[1] => DATE
[n] => n
)
Array (
[0] => TEXT
[1] => TEXT
[n] => n
)
As possible to combine these three arrays in one of the next species?
Array(
Array (
[0] => NAME
[1] => DATE
[2] => TEXT
)
Array (
[0] => NAME
[1] => DATE
[2] => TEXT
)
Array (
[0] => NAME
[1] => DATE
[2] => TEXT
)
and etc.
)
Arrays build with the use of library PHP Simple HTML DOM Parser Manual that's the way
// Search for the name
$array_nameNews = array();
foreach($html->find('div.narrow_column div h2 a') as $element) {
$nameNews = $element->innertext;
$array_nameNews[] = $nameNews;
}
// Search the date
$array_dateNews = array();
foreach($html->find('div.narrow_column div div.postdate') as $element) {
$dateNews = $element->innertext;
$array_dateNews[] = $dateNews;
}
// Search text
$array_textNews = array();
foreach($html->find('div.narrow_column div div.postdate span') as $element) {
$textNews = $element->innertext;
$array_textNews[] = $textNews;
}
You can do this with array_merge()
and your end-result arrays as others suggest, but you should do this instead:
$articles = array();
foreach($html->find('div.narrow_column div') as $art_elem) {
$article = array(
'name' => $art_elem->find('h2 a', 0)->plaintext,
'date' => $art_elem->find('div.postdate', 0)->plaintext,
'text' => $art_elem->find('div.postdate span')->innertext,
);
$articles[] = $article;
}
Two reasons:
$art_elem
context, you can degrade gracefully even when some items are missing.you can do this by array_merge
— Merge one or more arrays
try like
$result = array_merge($array1, $array2);
You'd be looking at something like this
<?php
$formatted = array();
foreach ($array_nameNews as $key => $news) {
$one_piece = new array();
$one_piece['NAME'] = $news;
$one_piece['DATE'] = $array_dateNews[$key];
$one_piece['TEXT'] = $array_textNews[$key];
$formatted[] = $one_piece
}
First of all Array
and PHP Simple HTML DOM Parser Manual
are different things.. In case of array you should try this
$first = array('1','2','3');
$second = array('11','22','33');
$third = array('111','222','333');
for($i=0;$i<count($first);$i++)
{
$result[] = array($first[$i],$second[$i],$third[$i]);
}
echo "<pre>";print_r($result);echo "</pre>";exit;
Sorry, if you don't like my wording.
Assuming the arrays always have the same number of elements
$t = count($array_nameNews);
$sorted = array();
for ($i=0; $i<$t, $i++) {
$sorted[] = array(
$array_nameNews[$i],
$array_dateNews[$i],
$array_textNews[$i],
);
}