简体   繁体   中英

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) :

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 :

  • [URL][0] is the url of [Name][0]

I want to do a foreach displaying the value by key in order to echo something like this :

<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.

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.

Once I got this new array, it's way easier to manage. I got the loop to work with a 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)

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.

<?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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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