sách gpt4 ăn đã đi

Php - 创建和显示多维数组

In lại 作者:数据小太阳 更新时间:2023-10-29 02:45:24 25 4
mua khóa gpt4 giày nike

我有这个 XML:



11482001
2001 Jun

Adverse Drug React Toxicol Rev

Mantle D
Gok MA
Lennard TW

Lennard TW
Adverse and beneficial effects of plant extracts on skin and skin disorders.
20
2
89-103

English

9109474
0964-198X


Journal Article
Review

PubMed - indexed for MEDLINE
ppublish

11482001
11482001
11482001


2001/08/03 10:00
2002/01/23 10:01
2001/08/03 10:00


1
3
Adverse drug reactions and toxicological reviews

2001 Jun;20(2):89-103


我想创建一个这样的数组:

$results2 = array(
'0' => array(
'id' => 'xxxxxxxx',
'authors' => array(
'0' => 'xxxxxxxxxx',
'1' => 'xxxxxxxxxx',
)
),
'1' => array(
'id' => 'xxxxxxxx',
'authors' => array(
'0' => 'xxxxxxxxxx',
'1' => 'xxxxxxxxxx',
)
),
);

我已经执行此功能以将数据从 XML 获取到 PHP,我试图将数据放入此结构中,然后显示。我对 PHP 没有太多经验,我在语法方面有问题。这是函数:

function esummary_query($db, $id) { 
$context = stream_context_create(array(
'http'=>array(
'user_agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'
)
));

$xml = file_get_contents('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=' . $db . '&id=' . $id, FALSE, $context);

$xml_file = simplexml_load_string($xml);
$results2 = array();
foreach ( $xml_file->DocSum as $items ) {
$results2[]['id'] = $items->Id;
$authors = $xml_file->xpath("//Item[@Name='Author']");
foreach ($authors as $author) {
$results2[]['authors'][] = $author;
}
}
return $results2;
}


echo'

id:

' . $results2[0]['id'] . "
author:" . $results2[0]['authors'];

但是当我显示它时,显示了 id 但没有显示作者的名字。 print_r 显示:

Array ( [0] => Array ( [id] => SimpleXMLElement Object ( [0] => 11850928 ) ) [1] => Array ( [authors] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Author [Type] => String ) [0] => LoPresti PJ ) ) ) [2] => Array ( [authors] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Author [Type] => String ) [0] => Hambrick GW Jr ) ) ) )

Cảm ơn tất cả mọi người!

câu trả lời hay nhất

添加计数器并将其用作数组键

function esummary_query($db, $id) { 
$context = stream_context_create(array(
'http'=>array(
'user_agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'
)
));

$xml = file_get_contents('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=' . $db . '&id=' . $id, FALSE, $context);

$xml_file = simplexml_load_string($xml);
$results2 = array();
$c=0;
foreach ( $xml_file->DocSum as $items ) {
$results2[$c]['id'] = (string)$items->Id;
$authors = $xml_file->xpath("//Item[@Name='Author']");
foreach ($authors as $author) {
$results2[$c]['authors'][] = (string)$author;
}
$c++;
}
return $results2;
}

示例输出

Mảng
(
[0] => Mảng
(
[id] => 11482001
[authors] => Array
(
[0] => Mantle D
[1] => Gok MA
[2] => Lennard TW
[3] => Mantle D
[4] => Gok MA
[5] => Lennard TW
)

)

[1] => Mảng
(
[id] => 11482001
[authors] => Array
(
[0] => Mantle D
[1] => Gok MA
[2] => Lennard TW
[3] => Mantle D
[4] => Gok MA
[5] => Lennard TW
)

)

)

关于Php - 创建和显示多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19924959/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress