May 7, 2013

Accessing Object within Array

Viablepath’s Questions:

I have the following output:

Array (
  [0] => stdClass Object (
        [id] => 20
        [news_title] => Startup finance docs in GitHub
        [news_url] => http://venturebeat.com/2013/03/06/fenwick-west-github/
        [news_root_domain] => venturebeat.com
        [news_category] =>
        [news_submitter] => 4
        [news_time] => 2013-03-06 11:20:03
        [news_points] => 0
    )
    [1] => stdClass Object (
        [id] => 21
        [news_title] => The problems with righteous investing
        [news_url] => http://gigaom.com/2013/03/07/the-problems-with-righteous-investing/
        [news_root_domain] => gigaom.com
        [news_category] =>
        [news_submitter] => 4
        [news_time] => 2013-03-08 09:14:17
        [news_points] => 0
    )
)

How would I access something like news_url in these? I’ve tried this, but to no avail:

print_r $this->$record[0]->news_title;

Your code is a little incomplete, and hard to follow, but try this:

 $arr =    Array();

    $obj0 = new stdClass;
    $obj0->id = 123;
    $obj0->news_title = "some title 0";
    //etc...
    $obj1 = new stdClass;
    $obj1->id = 124;
    $obj1->news_title = "some title 1";
    //etc...

   $arr[0] = $obj0;
   $arr[1] = $obj1;

    print_r($arr);

or something like

print_r($arr[0]);

or even

 echo $arr[0]->id;

You are using class property, you might want to check if it is accessible first. While accessing the class property after using $this you dont need the additional $, just use $this - record. Like

echo $this -> record[0] -> title;

If record is a valid class property which is an array and it still does not work. Give this a try too:

echo {$this -> record[0]} -> title;
April 30, 2012

Converting the structure of an array

Question by Joseph

I have the following array:

Array ( 
   [0] => Information.pdf
   [1] => Presentation.pdf
   [2] => Brochure.pdf
) 

I want to convert it into a nested array in the following format using PHP to make it compatible with the CakePHP 2.1.1 email class:

Array (  
   [Information.pdf] => Array ( 
        [file] => /files/Information.pdf 
        [mimetype] => application/pdf  
   ) 
   [Presentation.pdf] => Array ( 
        [file] => /files/Presentation.pdf 
        [mimetype] => application/pdf  
   ) 
   [Brochure.pdf] => Array ( 
        [file] => /files/Brochure.pdf 
        [mimetype] => application/pdf  
   ) 
)

Any ideas on how to do this? The “mimetype” can be hardcoded.

Answer by ThiefMaster

$nested = array();
foreach($flat as $filename) {
    $nested[$filename] = array(
        'file' => '/files/'.$filename,
        'mimetype' => 'application/pdf'
    );
};

For mimetype guessing, the fileinfo extension would be a good choice.

Answer by Starx

Use a foreach to iterate through the array items and use mime_content_type() to get the mimetype.

$temp = array(); //A temporary array to store new array
foreach($array as $filename) { //Supposing $array is your original array
    $file = '/files/'.$filename;
    $temp[$filename] = array(
        'file' => $file,
        'mimetype' => mime_content_type($file)
    );
};
March 23, 2012

Search array key from form post and get sub array

Question by Vince Lowe

I have an array which is storing server addresses like so

$servers = array(
  'UK'=>array(
    'voi' =>  '54.53.23.32',
    'gps' =>  '55.65.34.34',
    'sos' =>  '54.54.34.23'
  ),
  'USA'=>array(
    'voi' =>  '12.23.45.67',
    'gps' =>  '65.443.23.45',
    'sos' =>  '12.23.456.4'
  )
);

I have a form which is listing the server locations

<select name="server" id="server">
<?php foreach($servers as $key => $arr){
echo "<option>" .$key. "</option>"; }
?>
</select>

Once the form is submitted i need to set a session variable with the correct countrys IP’s

$_SESSION['voi']=$submitted_countrys_voi_ip;
$_SESSION['gps']=$submitted_countrys_gps_ip;

etc..

How can i choose the correct key based on the form post and get the values from the sub array into a session variable?

Answer by Starx

First of all, you select does not have a value. So fixed that first

<select name="server" id="server">

    <?php foreach($servers as $key => $arr){
    echo "<option value='$key'>" .$key. "</option>"; }
    ?>

</select>

Then, after the form is submitted. Assuming that form is submitted using POST method

$server = ..; //Being your array of ips
$sub_server = $_POST['server']; //get the submitted server
$_SESSION['voi'] = $server[$sub_server]['voi'];
$_SESSION['gps'] = $server[$sub_server]['gps'];
// and so on

Improvement

In case you have lots of IP’s for a nation, then you might want to create an array of the types first.

$server = ..; 
$types = array('voi','gps', 'sos'); //TYPES in a single array
$sub_server = $_POST['server']; 
foreach($types as $type) {
    $_SESSION[$type] = $server[$sub_server][$type];
}

This is automatically assign every types at once.

...

Please fill the form - I will response as fast as I can!