How to get the values of an array from a key

1

I need to get the values ($ src), within this foreach, of each key separately in that 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 complete array in 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 wanted to be able to mount 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" 
        }
    } 
    ?>
    
asked by anonymous 06.11.2015 / 19:05

2 answers

2

You can print the key and value of the array by checking if any of the keys is an array with is_array() , in the case src and then make a implode() to display all the 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);

Output:

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
        )
)
    
06.11.2015 / 19:41
0

It could be this 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 as follows

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++;
    }
}
    
06.11.2015 / 19:20