Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP convert array to resource

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;
like image 981
hardcoder Avatar asked Aug 24 '12 14:08

hardcoder


People also ask

How to convert array to string in PHP?

In this article, we are using two methods to convert array to string. Method 1: Using implode () function: The implode () method is an inbuilt function in PHP and is used to join the elements of an array. The implode () method is an alias for PHP | join () function and works exactly same as that of join () function.

How do I create a resource in PHP?

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.

How to reduce an array to a single string in PHP?

The usage of the array_reduce () function may not obvious at the first glance, but the general idea is to reduce an array down to a single string – By using a given custom REDUCTION function – In this one, we can pretty much set any rules, do any “special processing”. Well, this should be pretty self-explanatory.

How to convert array to string in jQuery?

ARRAY TO STRING 1 IMPLODE FUNCTION. This should be pretty easy to understand. ... 2 ARRAY REDUCE. The usage of the array_reduce () function may not obvious at the first glance, but the general idea is to reduce an array down to a single ... 3 MANUAL LOOP. Well, this should be pretty self-explanatory. ... 4 JSON ENCODE. ... 5 SERIALIZE. ...


3 Answers

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.

like image 152
hakre Avatar answered Sep 21 '22 16:09

hakre


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);
like image 45
Gérald PLUSQUELLEC Avatar answered Sep 24 '22 16:09

Gérald PLUSQUELLEC


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.

like image 32
IMSoP Avatar answered Sep 22 '22 16:09

IMSoP