1

I have a multidimensional array where I am storing a variable as the key and a list of file names within the array.

Below is some test code I am using:

$file["dir1"] = "dir1";
$file["dir2"] = "dir2";
$fileDetails = new FileInformation();
$fileDetails->fileName = "file1";
$fileDetails->fieSize = 100;
$fileDetails->modifiedTime = "111";
$file["dir1"][0] = $fileDetails; 

$fileDetails = new FileInformation();
$fileDetails->fileName = "file2";
$fileDetails->fieSize = 200;
$fileDetails->modifiedTime = "222";

$file["dir1"][1] = $fileDetails;

The FileInformation class is defined as follows:

class FileInformation
    {
        var $fileName;
        var $modifiedTime;
        var $fieSize;
    }

On the line $file["dir1"][0] = $fileDetails; I am getting the following error from PHP:

Catchable fatal error: Object of class FileInformation could not be converted to string in

I'm not sure what the problem is, I'm sure I've done this before in the past or at least something similar.

The idea is if I want to get the fileName out from the array then I could do the following:

$file["dir1"][0]->fileName

At least this is how I am expecting it to work.

4
  • man, don't use var anymore, public, private, protected is much better Commented Sep 14, 2014 at 1:00
  • I've not heard of that, what's the reason behind it. What makes them better than using var? Commented Sep 14, 2014 at 1:01
  • Because var is php 5.2. You're not writing ancient code. Are you? Commented Sep 14, 2014 at 1:05
  • this is to set the visibility of properties of class, is deprecated on php 5.4 Commented Sep 14, 2014 at 1:05

3 Answers 3

1

You shouldn't use $var anymore. For a quick read:

http://php.net/manual/en/language.oop5.visibility.php

Anyway, initialize file['dir'] as array instead.

class FileInformation
{
    public $fileName;
    public $modifiedTime;
    public $fieSize;
}

$file["dir1"] = array();
$file["dir2"] = array();
$fileDetails = new FileInformation();
$fileDetails->fileName = "file1";
$fileDetails->fieSize = 100;
$fileDetails->modifiedTime = "111";
$file["dir1"][0] = $fileDetails; 


$fileDetails = new FileInformation();
$fileDetails->fileName = "file2";
$fileDetails->fieSize = 200;
$fileDetails->modifiedTime = "222";

$file["dir2"][1] = $fileDetails;

echo '<pre>';
print_r($file);

If you want to access individual properties of that object inside the array:

echo $file['dir1'][0]->fileName; // file1
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, quite obvious now. I'm going to blame the fact its getting late here ;).
@Boardy haha sure man, no problem glad it help, maybe needs some rest
1

You assign a string to $file["dir1"]. Calling $file["dir1"][0] gives you the first position of that string which is a string as well (there is no char type in PHP like C has) and which is obviously not an object. What you want is a multidimensional array:

$file["dir1"] = [];
$file["dir1"][0] = $fileDetails
$file["dir1"][0]->fileName;

Comments

1

In short, you have to decide what exactly you want to store in $file["dir1"].

Currently, you're assign a string to it first, then attempt to modify the first character of that string (with $file["dir1"][0] syntax, PHP allows you to do that). That's obviously wrong, but PHP only warns about it as it sees (attempt to convert an object to string).

What you might want to do instead is using an array right from the start here:

$file["dir1"]['dirname'] = "dir1";
...
$file["dir1"][0] = $fileDetails;

... or, even better,

$file["dir1"]['dirname']   = "dir1";
$file["dir1"]['details'][] = $fileDetails; // for the first file
// ... 
$file["dir1"]['details'][] = $fileDetails; // for the second file and so on.

... without mixing 'normal' and 'associative' approaches at the same level (which is possible, but confusing).

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.