2

I currently have an array as follows:

   Array ( 
    [0] => Array ( [id] => 34 [another_id] => 2805 [third_id] => 1 ) 
    [1] => Array ( [id] => 35 [another_id] => 2805 [third_id] => 1 ) 
    [2] => Array ( [id] => 36 [another_id] => 2805 [third_id] => 1 ) 
    [3] => Array ( [id] => 37 [another_id] => 2805 [third_id] => 1 ) 
    [4] => Array ( [id] => 38 [another_id] => 2805 [third_id] => 1 ) 
    [5] => Array ( [id] => 39 [another_id] => 2805 [third_id] => 1 ) 
    [6] => Array ( [id] => 40 [another_id] => 2805 [third_id] => 2 ) 
    [7] => Array ( [id] => 41 [another_id] => 2805 [third_id] => 2 )
    [8] => Array ( [id] => 42 [another_id] => 2805 [third_id] => 2 ) 
    [9] => Array ( [id] => 43 [another_id] => 2805 [third_id] => 2 )
 )

What I need to do is ultimately print out 9 links ( as there are 9 array elements) but based on the keys in the array. For example:

www.samplelink/link/id/another_id/third_id

But I can't seem to get the loop right. What I have so far is:

foreach ($array as $arr) {
  foreach ( $arr as $key => $value ) {
    echo "<a>www.samplelink/link/".$key[$value]."</a>";
  }
}

But thats not exactly what I need as its printing out the keys as well. Anyone know what I could do?

6
  • 5
    "What I need to do is ultimately print out 9 links ( as there are 9 array elements)" noooo there are 10 ! Commented Dec 19, 2017 at 13:18
  • 3
    First rule of programming: arrays start at 0 Commented Dec 19, 2017 at 13:20
  • It took me about 10 minutes to realize I was counting my bills starting from 0, baffled each time Commented Dec 19, 2017 at 13:22
  • 1
    @Phiter no, it depends on the programming language! en.wikipedia.org/wiki/… Commented Dec 19, 2017 at 13:32
  • Oh my god what the hell? People really don't know how to follow a standard, do they? Commented Dec 19, 2017 at 13:39

5 Answers 5

6
 foreach ($array as $innerArray) {
  echo "<a>www.samplelink/link/".$innerArray['id']."/".$innerArray['another_id']."/".$innerArray['third_id']."</a>";
 }

It can give an undefined index error if key doesn't exist so you can do something like this as well:

foreach ($array as $innerArray) {
    $finalLink = array_key_exists('id',$innerArray)?$innerArray['id']:"";
    $finalLink.= "/".array_key_exists('another_id',$innerArray)?$innerArray['another_id']:"";
    $finalLink.= "/".array_key_exists('third_id',$innerArray)?$innerArray['third_id']:"";
    echo "<a>www.samplelink/link/$finalLink</a>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

When I'm not sure if the array key exists, I use this: $arr['key'] ?? "". It does the same as isset(), and doesn't throw an error. Of course this is PHP 7 only.
array key exists checks if the key exists within the array or not. In your case, it will crash if the array doesn't contain the key. For more info, read this : php.net/manual/en/function.array-key-exists.php
5

If elements in subarrays always in same order, you can just implode them:

foreach ($array as $arr) {
    echo "<a>www.samplelink/link/".implode('/', $arr)."</a>";
}

Otherwise you should point what index will be in which position explicitly, as in @Danyal Sandeelo's answer.

Comments

1
<?php

$arrays = array(
    "0" => array('id' => 30, 'another_id' => 2800, 'third_id' => 1),
    "1" => array('id' => 31, 'another_id' => 2801, 'third_id' => 1),
    "2" => array('id' => 32, 'another_id' => 2802, 'third_id' => 1),
    "3" => array('id' => 33, 'another_id' => 2803, 'third_id' => 1),
    "4" => array('id' => 34, 'another_id' => 2804, 'third_id' => 1),
    "5" => array('id' => 35, 'another_id' => 2805, 'third_id' => 2),
    "6" => array('id' => 36, 'another_id' => 2806, 'third_id' => 3),
    "7" => array('id' => 37, 'another_id' => 2807, 'third_id' => 3),
    "8" => array('id' => 38, 'another_id' => 2808, 'third_id' => 2),
    "9" => array('id' => 39, 'another_id' => 2809, 'third_id' => 2),
);

foreach($arrays as $key => $array) {
   echo 'www.samplelink/link/'.$array['id'].'/'.$array['another_id'].'/'.$array['third_id']. "\n";
}

you can play with it here http://sandbox.onlinephpfunctions.com/code/1c7838e25045263de03e23c60b19c86d5640407d

Comments

1

You can do this :

foreach ($array as $arr) {
    echo "<a>www.samplelink/link/" . $arr['id'] . '/' . $arr['another_id'] . '/' . $arr['third_id'] . "</a>";
}

or, if your sub-arrays always holds the ids in the proper order and nothing but the relevant ones :

foreach ($array as $arr) {
    echo "<a>www.samplelink/link/" . implode('/', $arr) . "</a>";
}

Comments

0

$value will hold the inner array on each iteration.

foreach($array as $value) {
  $link = '//www.samplelink/link/'.$value['id'].'/'.$value['another_id'].'/'.$value['third_id']
  echo '<a>'.$link.'</a>';
}

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.