0

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.

2
  • 1
    Why don't you flip the array around? Commented May 31, 2014 at 12:35
  • Good idea, I will look into that. Once it's organized by key, it would definitely be easier to make it work like I want. Thank you for your answer Commented May 31, 2014 at 12:56

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you I'll try that, but I think I will try to flip it around. Everybody seems to agree this is the best way.
I just tested it, copy-pasted it as is and only the Name value is shown. The output is the following : code<a href=''>Name 1</a><p></p><p></p> <a href=''>Name 2</a><p></p><p></p>code
it seems to be the case that $array['URL'][$k] is not set! Is URL really the index of your array (case sensitive)? please do a print_r($array) first and post the result.
It's case sensitive. I've put everything to lower-case just to check (Name became name, URL became url,...) and it didn't work either. Here is the array once printed out : codeArray ( [name] => Array ( [0] => Name 1 [1] => Name 2 ) [url] => Array ( [0] => url-1.com [1] => url-2.com ) [date] => Array ( [0] => 2014-05-31 11:10 [1] => 2014-05-26 11:16 ) [online] => Array ( [0] => 1 [1] => ) )code It's all lower case now, but I changed your code to lowercase too.
I found a solution by getting a slightly different array. I added the solution to my question. Thanks for your help @steven
0
<?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
}
?>

1 Comment

sizeof() is nothing but alias of count(). Hence both will work. php.net/manual/en/function.sizeof.php
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.