8

For visual representation, for simplicity and of course to feed my curiosity, I'm wondering how to convert a PHP array into a valid PHP resource.

See the below example: enter image description here (example image created with dBug component available at http://dbug.ospinto.com/)

I've made 3 examples:

  1. resource: this is the typical representation of a MySQL resource, visualized as a grid
  2. object: a handmade create object from an array
  3. array: a handmade multidimensional array

As you can see, the resource is a visual beauty, while the object and array are constructed by using multidimensional arrays, using poor numeric array indexes to bind them together :(

What I'm looking for, would probably be something like this:

$resource_var = (resource) $array_var;
5
  • 4
    I'm pretty sure what you're asking is not possible. Commented Aug 24, 2012 at 14:27
  • This seems to me it is not a PHP-related question, but rather a dbug-related question. Is your question "How to make dbug display the content of an object or an array the same way it displays the content of a resource?" Commented Aug 24, 2012 at 14:31
  • @Jocelyn I read that too quickly and thought you said "...rather a drug-related question." I don't think you would have been too far off. Commented Aug 24, 2012 at 14:35
  • Only you have a resource does not mean you will get beautiful output. Try with a resource handle of a CSV file and your dbug extension. Commented Aug 24, 2012 at 14:40
  • Thanks for your replies guys! This is giving me new clues to work on. However, I really like the data to be sorted internally into the query grid (with rows and columns) but maybe I'm pushing to hard :) Commented Aug 24, 2012 at 14:48

4 Answers 4

8

What I'm looking for, would probably something like this:

$resource_var = (resource) $array(var)

You will never find that. A resource is an internal data-type in PHP. If (and only if) you write yourself a PHP extension and load it, you could do the following:

$resource = array_resource_create($array);

Your PHP extension then would create that resource (as the mysql extension for example creates its specific resource type) within that array_resource_create function. However, it would be useless, because there is no other function so far that could deal with that resource.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your replies guys! This is giving me new clues to work on. However, I really like the data to be sorted internally into the query grid (with rows and columns) but maybe I'm pushing to hard :)
Write your own a function for that and/or extend the dbug library you use. You would only need to check if it is an array and if so it contains only of arrays that have the same keys -or- objects that have the same properties. That is also a nice tutorial you will get in extending existing code.
2

You can't create resource. but you can use native one.

Try with curl for example.

function makeResourceFromArray($array) {

    $resource = curl_init();

    curl_setopt($resource, CURLOPT_PRIVATE, serialize($array));

    return $resource;
}

function makeArrayFromResource($resource) {
    return unserialize(curl_getinfo($resource, CURLINFO_PRIVATE));
}

$resource = makeResourceFromArray(['name' => 'test']);

$array = makeArrayFromResource($resource);

1 Comment

That will lead to a resource of type curl. Not really what the OP wants.
1

The output you show there is nothing to do with it being a resource as such, but the pretty-print function you're using noticing that the variable you've given it points at a database result set, and fetching and displaying the results.

What PHP means by a resource is that the variable doesn't actually hold data within PHP, but is a pointer or reference usable by some lower-level module of code - in this case, a DB library which can use that reference to retrieve the results of the executed query.

If you just want the pretty-print to look similar for an array with a DB-resultset-like structure, then you should simply modify the pretty-print function to do so - you don't need to do anything to the array itself.

Comments

0

A resource is a special type. And a resource is specific to a source that's external. Therefore going backwards wouldn't be possible.

Theoretically, an interface with an instance of the resource would help manage the type - but this is just nonsense theoretical talk that is impossible in PHP.

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.