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?
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.
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>";
}
$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>';
}
<?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']. "
";
}
you can play with it here http://sandbox.onlinephpfunctions.com/code/1c7838e25045263de03e23c60b19c86d5640407d
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>";
}