0

I am trying to get the data from a multidimensional array key in php. The array structure is like this :

Array


( [status] => 1     [embeds] => Array
    (
        [1] => Array
            (
                [embed] => <IFRAME SRC="XXXXXXXX.ZZZ" FRAMEBORDER="0" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="NO" WIDTH="620" HEIGHT="360"></IFRAME>
                [link] => http://XXXXXXXXXXX.ZZZZ
                [language] => ENG
            )

        [2] => Array
            (
                [embed] => <iframe src="http://www.XXXXXXX.ZZZZ" width="620" height="360" frameborder="0" scrolling="no"></iframe>
                [link] => http://www.XXXXXXX.ZZZZZ
                [language] => ENG
            ) ... ... ... ...


    ))

The $auto_incrementing_value starts from 1 to as many as there are. so if I want to echo only 1 data and $auto_incrementing_value = 1, I can do echo $ret['embeds'][$auto_incrementing_value]['link']; What I want to do is echo all the "link" value from all the arrays.

I tried this code but it does not work :

$codes = 1;
foreach ($ret as $key => $rets){
echo $ret['embeds'][$codes]['link'];
$codes++;
}

4 Answers 4

1

That sure is some whacky syntax you've got going on there. You're using a foreach loop like a while loop that is written like a for loop.

Try:

foreach ($ret['embeds'] as $embed){
  echo $embed['link'];
}

Or:

for( $i=1; $i<=count($ret['embeds']); $i++ ) {
  echo $ret['embeds'][$i]['link'];
}

Or if you want to get saucy:

$i=0;
while($i<=count($ret['embeds'])) {
  echo $ret['embeds'][$i]['link'];
  $i++;
}

edit

@MarkBaker raises a valid point about calling count() [or really any function] in the loop condition. If the function's return will be static during the entire course of the loop [ie: the number of elements in the array doesn't change] then it's best to do:

$count = count($ret['embeds'];
for( $i=1; $i<=$count; $i++ ) {
  echo $ret['embeds'][$i]['link'];
}

Or, alternatively, you can go backwards:

for( $i=count($ret['embeds'])-1; $i>=0; $i-- ) {
  echo $ret['embeds'][$i]['link'];
}
Sign up to request clarification or add additional context in comments.

5 Comments

Was going to add to my answer to show the for loop, but you beat me to it.
thank you! Your solution in working great too. But, witch one is better (speed, errors ...), the foreach loop or the for loop.
@user2650060 these loops are all more or less equivalent, the differences are all too small to worry about even if the loop runs a billion iterations.
Any loop that executes count($ret['embeds']) every iteration isn't a well written loop because that is adding a performance overhead
@MarkBaker true. It's a lazy way to write a loop and should be avoided for arrays larger than what you can count without having to take off your shoes. But I'm super lazy.
1

You could simply iterate over the embeds array:

foreach($ret['embeds'] as $embed) {
    echo $embed['link'];
}

1 Comment

thanks alot! It working! I was complicating things but the solution was simple!
1

You don't need to use an index variable if you're using foreach, that's what foreach does automatically. You just have to give it the correct array that you want to iterate over.

foreach ($ret['embeds'] as $rets) {
    echo $rets['link'];
}

1 Comment

thank you! It working great . Also, thank you for explaining! I was complicating things too much!
0

PHP >= 5.5.0

foreach(array_column($ret['embeds'], 'link') as $link) {
    echo $link, PHP_EOL;
}

just to demonstrate one of the newest features in PHP

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.