How to get the values of an array from a key

Asked

Viewed 512 times

1

I need to get the values ( $src ), inside this foreach, of each key separately in this array:

<?php

   foreach( get_post_gallery( $post->id, false ) as $key => $src ) { 

    echo $key; //resultado:  link, size - ids         - src
    echo $src; //resultado:  file, full - 61,59,57,41 - Array
   }

?>  

This is the full array on var_dump();

<?php
array(4) { 
    ["link"]=> string(4) "file" 
    ["size"]=> string(4) "full" 
    ["ids"]=> string(11) "61,59,57,41" 
    ["src"]=> array(4) { 
        [0]=> string(83) "http://localhost/1.jpg" 
        [1]=> string(94) "http://localhost/2.jpg" 
        [2]=> string(62) "http://localhost/3.jpg" 
        [3]=> string(68) "http://localhost/4.jpg" 
    }
} 
?>

UPDATE

I wish I could assemble the result like this:

 <?php
    array(4) { 
        ["link"]=> string(4) "file" 
        ["size"]=> string(4) "full" 

        ["ids"]=> array(4) { 
            [0]=> string(83) "61" 
            [1]=> string(94) "59" 
            [2]=> string(62) "57" 
            [3]=> string(68) "41" 
        }

        ["src"]=> array(4) { 
            [0]=> string(83) "http://localhost/1.jpg" 
            [1]=> string(94) "http://localhost/2.jpg" 
            [2]=> string(62) "http://localhost/3.jpg" 
            [3]=> string(68) "http://localhost/4.jpg" 
        }
    } 
    ?>
  • 1

    Some charitable soul could help?

  • You can make a new foreach() inside src

2 answers

2


You can print the array key and value by checking if one of the keys is an array with is_array(), in the case src and then make a implode() to display all items.

<?php
    $arr = ['link' => 'file', 'size' => 'full', 'ids' => '61,59,57,41',
            'src' => ['http://localhost/1.jpg', 
                      'http://localhost/2.jpg', 
                      'http://localhost/3.jpg',
                      'http://localhost/4.jpg']
            ];

foreach($arr as $key => $value) {
    if($key == 'ids'){
        $arr['id'] = explode(',', $value);
    }

    if(!is_array($value)){
        echo "$key: ".  $value .'<br>';
    }
}

    echo "<pre>";
    print_r($arr);

Exit:

Array
(
    [link] => file
    [size] => full
    [ids] => 61,59,57,41
    [src] => Array
        (
            [0] => http://localhost/1.jpg
            [1] => http://localhost/2.jpg
            [2] => http://localhost/3.jpg
            [3] => http://localhost/4.jpg
        )

    [id] => Array
        (
            [0] => 61
            [1] => 59
            [2] => 57
            [3] => 41
        )
)
  • @Lollipop can do another foreach this time iterating $v in place of implode().

  • @Lollipop, like this? foreach($arr as $k => $v) {&#xA; if(is_array($v)){&#xA; echo 'links:<br>';&#xA; foreach($v as $link){&#xA; echo $link .'<br>';&#xA; }&#xA; }else{&#xA; echo "$k:". $v .'<br>';&#xA; }&#xA;}&#xA;

  • @Lollipop as an array?

  • @Lollipop 61 is linked to http://localhost/1.jpg, 59 to http://localhost/2.jpg that?

  • @Lollipop see if that’s it

  • @Lollipop changed the answer.

  • I’m glad it worked :)

Show 2 more comments

0

It can be that way:

foreach( get_post_gallery( $post->id, false ) as $key => $src ) { 
    echo $key; //resultado:  link, size - ids         - src
    foreach($src as $valor){
        echo $valor->campo;
    }
}

Or if there is no field, do it this way

foreach( get_post_gallery( $post->id, false ) as $key => $src ) { 
    $i = 0;
    echo $key; //resultado:  link, size - ids         - src
    foreach($src as $valor){
        echo $valor[$i];
        $i++;
    }
}
  • : Invalid argument supplied for foreach()

  • Give a print_r() on $src, to see the return... which of the options you set? And I think there on get, you can leave only $src... without the key

  • Array ( &#xA;[0] => link [1] => size &#xA;[2] => ids &#xA;[3] => src &#xA;) &#xA;file full 61,59,57, 41 &#xA;Array ( &#xA; [0] => http://localhost/1.jpg &#xA; [1] => http://localhost/2.jpg &#xA; [2] => http://localhost/3.jpg &#xA; [3] => http://localhost/4.jpg &#xA;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.