简体   繁体   English

使用密钥的Foreach多维数组-PHP

[英]Foreach multidimensional array using the key - PHP

I am posting this message because I couldn't find the answer to my problem anywhere. 我发布此消息是因为我无法在任何地方找到问题的答案。 I might not be looking for the right search query i am just starting with arrays. 我可能不是在寻找正确的搜索查询,而只是从数组开始。

I have the following array with nested arrays organized by information type (Name, URL, Date, Online) : 我有以下按信息类型(名称,URL,日期,在线)组织的嵌套数组的数组:

Array (
[Name] => Array ( [0] => Name 1 [1] => Name 2 )
[URL] => Array ( [0] => http://url-1.com [1] => http://url-2.com )
[Date] => Array ( [0] => 2014-05-31 11:10 [1] => 2014-05-26 11:16 )
[Online] => Array ( [0] => 1 [1] => ) )

The key is showing which items are linked. 关键是显示要链接的项目。 Every [0] belongs together for example : 每个[0]都属于例如:

  • [URL][0] is the url of [Name][0] [URL] [0]是[Name] [0]的网址

I want to do a foreach displaying the value by key in order to echo something like this : 我想做一个foreach通过键显示值,以便回显类似这样的内容:

<a href="[URL][0]">[Name][0]</a><p>[Date][0]</p><p>[Online][0]</p>
<a href="[URL][1]">[Name][1]</a><p>[Date][1]</p><p>[Online][1]</p>

There can be 1 to n keys. 可以有1到n个键。

Found a Solution 找到了解决方案

I manage to get the array differently : 我设法以不同的方式获取数组:

$array = Array (
[0] => Array ( [name] => Name 1 [url] => http://url-1.com [date] => 2014-05-31 11:10 [online] => 1 )
[1] => Array ( [name] => Name 2 [url] => http://url-2.com [date] => 2014-05-26 11:16 [online] => ) ) 

It's organized by arrays containing the values together, not by arrays containing all the names together, all the urls together,... If someone knows how to change the initial to this one I can add it to my solution. 它是由包含值的数组组成的,而不是由包含所有名称,所有URL在一起的数组组成的……如果有人知道如何将首字母更改为该名称,则可以将其添加到解决方案中。

Once I got this new array, it's way easier to manage. 一旦有了这个新阵列,就可以更轻松地进行管理。 I got the loop to work with a for : 我有一个循环可以与for一起使用:

I first set a limit to the for by getting the highest array key value : 我首先通过获取最高的数组键值来为设置一个限制:

$max_stop = max(array_keys($array));

Then i did a for. 然后我做了一个。 The +1 after the $max_stop is needed, otherwise it stops counting at the second to last one (the count starts at 0 and not 1) 需要$ max_stop之后的+1,否则它将停止倒数第二个计数(计数从0而不是1开始)

for ($row = 0; $row < $max_stop+1; $row++){

Then I echo what i wanted to display : 然后我回显我想显示的内容:

echo '<a href="'.$array[$row]["url"].'">'.$array[$row]["name"].'</a><p>'.$array[$row]["date"].'</p><p>'.$array[$row]["online"].'</p>';}

This may not be the best way but it works as I wanted. 这可能不是最好的方法,但是它可以按我的意愿工作。

you may do it like this: 您可以这样做:

$array = array(
    'Name' => array( 0 => "name 1", 1 => "name 2"),
    'URL' => array( 0 => "http://www.example.com", 1 => "http://www.stackoverflow.com"),
    'Date' => array( 0 => "2014-05-31 11:10", 1 => "2014-05-02 12:10"),
    'Online' => array( 0 => 1, 1 => )
);

foreach($array['Name'] as $k => $v) {

    $url = isset($array['URL'][$k])?$array['URL'][$k]:"";
    $date = isset($array['Date'][$k])?$array['Date'][$k]:"";
    $online = isset($array['Online'][$k])?$array['Online'][$k]:"";

    echo "<a href='$url'>$v</a><p>$date</p><p>$online</p>";
}

the output is: 输出为:

<a href='http://www.example.com'>name 1</a><p>2014-05-31 11:10</p><p>1</p>
<a href='http://www.stackoverflow.com'>name 2</a><p>2014-05-02 12:10</p><p>0</p>

but it should be a better idea to flip the array arround like Barmar said. 但是像Barmar所说的那样翻转数组范围应该是一个更好的主意。

<?php
$array = array(
         'name' => array('name1','name2');
         'url' => array('url1','url2');
         'date' => array('date1','date2');
         'online' => array('online1','online2');
      );

for($i=0; $i<sizeof($array['name']); $i++)
{
?>

   <a href="".$array['url'][$i]."">".$array['name'][$i]."</a><p>".$array['date'][$i]."</p><p>".$array['online'][$i]."</p> 

<?php
}
?>

Putting the array in a different order makes live probably easier: 以不同的顺序放置数组可能使操作变得更容易:

//Setup your array   
$array =  array(
    array('Name' => 1, 'URL' => 'http://url-1.com', 'Date' => '2014-05-31 11:10', 'Online' => 1),
    array('Name' => 5, 'URL' => 'http://url-2.com', 'Date' => '2014-05-20 11:10', 'Online' => 0)
);


//Loop the array 
foreach($array as $item){
    echo '<a href="'.$item['URL'].'">'.$item['Name'].'</a><p>'.$item['Date'].'</p><p>'.$item['Online'].'</p>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM