![](/img/trans.png)
[英]How to intersect an array based on key values while preserving sibling key value pairs
[英]How to iterate child arrays as “rows” of parent array, while (by name) echoing values (of key/value pairs) in each “row”
我花了好幾個小時尋找和嘗試。 根據 array() 和 list() 的文檔,我的一種變體似乎可以工作,但到目前為止還沒有成功。
我的目標是以下代碼(或它的一些更正版本)......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Demo of Issue</title>
</head>
<body>
<?php
$Details_Standard_Ship = Array (
Array ('Book_ID' => 'c7xhGSU', 'Author_Name' => 'John Doe', 'Language' => 'English', 'Title' => 'Some Title 1'),
Array ('Book_ID' => 'rFxW8Bo', 'Author_Name' => 'Jane Doe', 'Language' => 'Spanish', 'Title' => 'Some Title 2')
);
foreach ($Details_Standard_Ship as $Details_ITEM) {
list($_Book_ID, $_Author_Name, $_Language, $_Title) = current($Details_ITEM);
echo "<p>Book_ID = " . $_Book_ID . "</p>";
echo "<p>Author_Name = " . $_Author_Name . "</p>";
echo "<p>Language = " . $_Language . "</p>";
echo "<p>Title = " . $_Title . "</p>";
}
?>
</body>
</html>
...將 output 這個結果:
迭代 1 of 2(第一個子數組) Book_ID: c7xhGSU, Author_Name: John Doe, Language: English, Title: Some Title 1
迭代 2 of 2(第二個子數組) Book_ID: rFxW8Bo, Author_Name: Jane Doe, Language: Spanish, Title: Some Title 2
注意:在遍歷父數組的父級,我需要對每個子數組進行一次迭代,給出與子 arrays 的數量相匹配的主迭代總數。 挑戰在於僅在該子數組的一個“父級”迭代中獲取所有子級的值。 似乎我在進行外循環迭代時需要內循環迭代。 我就是不知道怎么辦!
以下是我嘗試過的偽版本...
foreach $Details_Standard_Ship as $Details_ITEM {
foreach $Details_ITEM as list($_Book_ID, $_Author_Name, $_Language, $_Title) {
// echoing here...
}
}
和
foreach ($Details_Standard_Ship as $Details_ITEM) {
list($_Book_ID, $_Author_Name, $_Language, $_Title) = current($Details_ITEM);
// echoing here...
}
和
foreach ($Details_Standard_Ship as list( $_Book_ID, $_Author_Name, $_Language, $_Title)) {
// echoing here...
}
..等等。
我當然會歡迎並感謝任何幫助。
實現 output 貼出問題
利用
foreach(array as $key => $value)
//do something
整個代碼
$Details_Standard_Ship = Array (
Array ('Book_ID' => 'c7xhGSU', 'Author_Name' => 'John Doe', 'Language' => 'English', 'Title' => 'Some Title 1'),
Array ('Book_ID' => 'rFxW8Bo', 'Author_Name' => 'Jane Doe', 'Language' => 'Spanish', 'Title' => 'Some Title 2')
);
foreach ($Details_Standard_Ship as $Details_ITEM) {
foreach ($Details_ITEM as $key => $value) {
echo "<p> $key = $value </p>";
}
}
從評論中了解問題后
使用$array['keyname']
獲取特定鍵的值
$Details_Standard_Ship = Array (
Array ('Book_ID' => 'c7xhGSU', 'Author_Name' => 'John Doe', 'Language' => 'English', 'Title' => 'Some Title 1'),
Array ('Book_ID' => 'rFxW8Bo', 'Author_Name' => 'Jane Doe', 'Language' => 'Spanish', 'Title' => 'Some Title 2')
);
foreach ($Details_Standard_Ship as $Details_ITEM) {
echo "<p>Book_ID = " . $Details_ITEM['Book_ID'] . "</p>";
echo "<p>Author_Name = " . $Details_ITEM['Author_Name'] . "</p>";
echo "<p>Language = " . $Details_ITEM['Language'] . "</p>";
echo "<p>Title = " . $Details_ITEM['Title'] . "</p>";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.