Kelly
Kelly

Reputation: 23

Accessing array values in PHP

Please can someone tell me what I am doing wrong? I am trying to retrieve all the data in the array below in PHP and I am having problems doing this. Here is the example:

<?php

$url = 'http://www.example.com/';

if (!$fp = fopen($url, 'r')) {
    trigger_error("Unable to open URL ($url)", E_USER_ERROR);
}

$meta = stream_get_meta_data($fp);

print_r($meta);

foreach($meta as $key=>$value) {
    echo '<br>'.$value;

}

fclose($fp);

?>

The foreach loop above permit me to loop through and retrieve all the data in the array, but my questions are:

  1. How can I retrieve only the value http of [wrapper_type] and other values independently?
  2. How can I retrieve only one value of [wrapper_data] => Array without retrieving all the values together?
  3. What is the index number or value to access these data?

Upvotes: 0

Views: 90

Answers (3)

Gagan Upadhyay
Gagan Upadhyay

Reputation: 255

just use 'wrapper_type' index in your array($meta).

$type = $meta["wrapper_type"];
echo $type;

Upvotes: -1

Federkun
Federkun

Reputation: 36924

How can I retrieve only the value http of [wrapper_type]

Just with $meta['wrapper_data']

How can I retrieve only one value of [wrapper_data]

The same, Array elements can be accessed using the array[key] syntax: $meta['wrapper_data'][0], $meta['wrapper_data'][1], ...

Upvotes: 2

miken32
miken32

Reputation: 42676

This is pretty basic stuff; you may want to do some reading up on PHP basics before progressing further. Nevertheless...

How can I retrieve only the value http of [wrapper_type] and other values independently?

$type = $meta["wrapper_type"];
echo $type;

How can I retrieve only one value of [wrapper_data] => Array without retrieving all the values together?

$the_fourth_header = $meta["wrapper_data"][4];
echo $the_fourth_header;

Upvotes: 2

Related Questions