Convert String To An Array Or Object If It Represents One?


Feb 16, 2011

I have a function that prints information to the page, it does this because it checks if that value exists every time and outputs an error if it doesn't. Due to the unknown nature of what is being sent to this function it always arrives as a string, I can't change this.Is it possible for this function to interpret strings such as "array[0]" and "object.something" and return that value instead of looking for the value as an index in $this

E.g.

private array = array("stuff");
$this->printValue("string");
$this->printValue("array[0]");[code]....

View 2 Replies


ADVERTISEMENT

How Many Bits Each Character Of A String Represents

I remember there was a function in PHP where you can choose how many bits each character of a string represents (I dont remember if it was for converting string or for generating them). For example if you chose 4 bits per caracter then the output string would be formed by hexadecimal characters only (0-9 A-F).

If you chose 5 bits per caracter then the output string will include more characters (not just hexadecimal ones) since the alphabet needs to have 32 characters (2^5); and if you chose 6 bits per charaters then an alphabet of 2^6=64 characters was used. Of course, the more bits per character you choose, the shorter the output string will be. Does anyone know what I'm talking about?? I'm quite sure there was a built-in function for this in PHP, but I cant find it.

View 1 Replies

Multidimensional Array Where Each Sub-array Represents Quantity[0] And Item #[1]?

I have the following multidimensional array where each sub-array represents quantity[0] and item #[1]:

Array
(
=> Array
[code]....

View 8 Replies

Sort A MultiDimensional Array Which Represents Filesystem Structure?

I need to sort a multi-dimensional array which represents filesystem structure:

Array
(
[dir1] => Array
(
[dir2] => Array

[Code]....

View 1 Replies

Convert DTO Object To XML String?

I need to convert a data transfer object which in turn may contain another DTO into XML, is there any function which does this?

View 1 Replies

Convert Object To String?

I'm having problems using an object in a comparison; every type of comparison generates an error: Code: [error] PHP Catchable fatal error: Object of class stdClass could not be converted to string in I'm building a theme in WordPress, and I'm trying to use the $post->post_name variable. I've tried if and ternary, I've tried converting the object with strval() and settype(), I've tried passing its value into an array and then imploding the array to get at the string ... nothing! I can echo the object out, but I cannot use it any comparison inside the Loop. But, bizarrely, even if I perform the conversion outside of the Loop which works the moment I use the new string variable no matter what its name is back inside the Loop, I get the same error.

View 4 Replies

Convert An Object To A String?

Is it possible to convert an object to a string? I'm using the imap functions for php and I'd like to get the subject out of what I have and use a preg_match to get the url that's in the subject. But it seems I can't use an object in the preg_match. Any easy way to convert the object to a string? It seems a string (when I manually enter one in) works

Oh I'm sing php 4, but we hope to upgrade to 5.3 in a month or so. So a solution that works for both would be great if possible.

View 7 Replies

Convert Object To String

I have an object which gets a String. I want to compare it with a String and I don't know how to do it. My source is something like it:

$field = $this->form->getFieldset('profile');
$website = "Web site";
if ($field->label == $website){
echo "good";
}else{
echo "bad";
}

View 2 Replies

Convert Object Into String ?

how to convert object into string in php.Actually i am dealing with web service APIs.i want to use output of one API as a input for another API. when i am trying to do this i got error like this:Catchable fatal error: Object of class std could not be converted to string in C: ...

this is the output of first API::stdClass Object ( [document_number] => 10ba60 ) now i want only that number to use as input for 2nd AP

print_r and _string() both are not working in my case

View 5 Replies

Convert Object To String Within Class

I am trying to convert some old library functions into a class. (i'm trying to force myself to learn OOP) The Class:

[Code]....

Catchable fatal error: Object of class timestamp could not be converted to string I've worked out its the strpos() function that's causing the error and I understand that you can use __toString() to convert an object to a string but am not sure how to implement it in this set-up.

View 5 Replies

Convert String Into Object Property?

I am working on the back end php for a JQuery inline field editing feature for a web app.

I am passing the field name (id) and value (val) to the mysql/update method of my class.

Since this update can be for literally any field in the table (and therefore any property of the object), I need to be able to do this:

PHP Code:
function update($id, $val) {
$this->$id = $val;
}

But I get "cannot access empty property" with this approach. Using $id, which corresponds to one of the existing object properties, how can I assign $val to the correct property?

View 2 Replies

Convert Object (SimpleXMLElement) To String?

I am using xpath to parse text from a webpage but it returns it as an object how can i return this as a string.

libxml_use_internal_errors(TRUE);
$dom = new DOMDocument();
$dom->loadHTML($source);
$xml = simplexml_import_dom($dom);
libxml_use_internal_errors(FALSE);
$username = $xml->xpath("//span[@class='user']");

var_dump of the $username array:

object(SimpleXMLElement)#3 (2) { ["@attributes"]=> array(1) { ["class"]=> string(4) "user" } [0]=> string(11) "bubblebubble1210" }

View 1 Replies

Equivalent Of Delphi - Convert The Object To An Array And Then Refer To The Associative Elements In The Array?

Is there a PHP way to do the equivalent of the following Delphi snippet

with someObject do
begin
x:=objPropA;//some stuff with objPropA
end;

Apart from reducing the verbosity of code it also reduces the possibility of typo errors. The best I have managed is to convert the object to an array and then refer to the associative elements in the array. This works for me because I ensure that I am only handling simple PHP objects - i.e. ones which do not have objects as properties in turn. However, given just how neat PHP is I am sure that there is a better way.

View 2 Replies

Code To Convert Javascript Object To String?

I have php file from which i am trying to run firefox and getting yslow results. But yslow results are sent in object form some thing like [object Object] results are sent using post method. how can convert it into string in php code and display to web page??

View 3 Replies

C - Extension: Convert An Object To String With __toString()?

Writing a PHP extension in C, I want to convert a userland object (IS_OBJECT) to a string through __toString() if it has one, and fail otherwise. What should I use?
I don't need another zval on output, just a char *.

zval *zo;
switch (Z_TYPE_P(zo)) {
case IS_STRING:

[code]...

View 1 Replies

Method As ToString() To Convert A Object To String?

is there any method as toString() to convert a object to string?

View 6 Replies

Sql Server - Convert Mssql Datetime Object To String

I'm grabbing some information from a database and the record is in an MSSQL DateTime format, when I return it, it shows in my array as follows:

[code]...

When I try to extract this as an array (ie $array[arrayItem][date]) I get an error: Fatal error: Cannot use object of type DateTime as array. I have also tried formatting the data in SQL before it is passed to the PHP, and the strtotime functions and had no joy.

View 3 Replies

Convert XML Object To HTML Friendly Text String?

I'm using PHP5 and it's inbuilt SOAP functionality. I'm catching the SOAP Fault errors, and email myself when one is triggered. What I really need to do is include the __getLastRequest() and __getLastResponse(), however as these are XML objects, when I try to include them by echoing them into the body of my HTML email, for obvious reasons they don't appear in full.

Is there a function or class I can use to convert these objects into a HTML friendly string? I've googled this but without any joy. If possible i'd like to avoid using a class external to PHPs own functionality but if needs must i'll have to. EDITED: How can I format the following XML so that it gives me an indented browser friendly version?

[Code]...

View 2 Replies

Convert Object To Array?

<?php
print_r($response->response->docs);
?>

display the following

Array ( [0] => Object ( [_fields:private] => Array ( [id]=>9093 [name]=>zahir) Object ( [_fields:private] => Array ( [id]=>9094 [name]=>hussain)..)

how to change the object to array... i want the following

Array([0]=>([id]=>9093 [name]=>zahir) [1]=>([id]=>9094 [name]=>hussain)...)

View 2 Replies

Convert Array Into Object?

I have a own Array class. Like this:

myArray::fetch('site.meta.keywords'); // return Array(...)

At the same time, How can I do like this?

myArray::fetch('site.meta.keywords'); // return Array(...)
myArray::fetch('site.meta.keywords')->as_object(); // return Object{...}

Is it possible in PHP?

View 3 Replies

Convert An Object To An Array?

using the below code for decoding json

$categories = json_decode($data);
$categories = $categories->data;

where i get this

{"categories":[{"id":1,"name":"Utilities","apps":897,"iconurl":"http://static.apptrackr.org/caticons/uti.jpg"},{"id":2,"name":"Productivity","apps":477,"iconurl":"http://static.apptrackr.org/caticons/pro.jpg"},{"id":3,"name":"Music","apps":466,"iconurl":"http://static.apptrackr.org/caticons/mus.jpg"},

[code].........

and i would like to convert to in an array like this

Array[0]
{
id => 1
name => Utilities

[code].....

View 3 Replies

Convert Array To XML Object?

How would I convert an array to XML? I am using PHP to provide the files in a directory as an array. I will then take this info and use it as XML in flex to display all files within a specific directory.I want to call this file as an HTTPRequest and return the XML.

PHP Code:

<?php
$dir = 'test';
$files = scandir($dir);

[code]....

View 1 Replies

Convert Particular Array To Object?

I'm looking for an elegant approach to convert $array to $object.

If $array contains Array ( [0] => en=english [1] => fr=french ) the resulting $object should contain stdClass Object ( [en] => english [fr] => french )

The most elegant approach would be the one that uses the least variable names.

View 4 Replies

Convert An Object Into An Array?

Is there a simple way to convert a mysql query result object into an array (rather than an object?)

ie. say I run a query against a mysql database in order to show the results in a table:

PHP Code:

$result= mysqli_query($link,$sql);
//...then run the result through the code that displays the table...that all works fine

BUT when I try to print_r($result) at the end of the process, all I see is the statement "mysqli_result Object ( ) " is there a way to simply convert the object into something (ie an array) that can be used again for other purposes without re-running the query?

View 3 Replies

Function To Convert An Object To An Array?

I'm integrating an API to my website which works with data stored in objects while my code is written using arrays. I'd like a quick and dirty function to convert an object to an array.

View 3 Replies

Convert Indexed Array Into Object?

I find it peculiar converting an indexed array to an object. Consider the following:

PHP Code:
$arr = array('John', 'Jenny');
$obj = (object) $arr;
echo $obj->{0}; // Notice: Undefined property: stdClass::$0

The goal here is to be able to simply tap into and echo out the value 'John' via $obj while making use of stdClass. Since arrays being converted to objects assign array keys to object properties while the array values become those properties' values, this means that the property 0 will have the value 'John' while the property 1 will have 'Jenny'. Simply using print_r in place of that last line in the snippet demonstrates this:

[code]...

View 13 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved