How to save simplexml node value to string? - php

I am trying to get a value in a node, and save it into a string variable. I haven't used PHP in about 5 years so I have no idea what is going on?
string $errorMessage = (string)$error->message);
print_r($errorMessage);
returns nothing

first do
$errorMessage = (string)$error->message;
and dont do print_r its used for echoing array simply use echo instead so
echo $errorMessage;
Php is not strongly typed language so you dont need to do string $errorMessage; , but still casting is pressent in php , coz objects such as simplexml implements __toString magic function which gets called automaticaly when you cast that object as string .

Related

Serilaizing in PHP returns wrong value

I try to serialize my data with PHP. Unfortunaly, the serialize() function returns a wrong value.
String to be serialized:
{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}
What serialize() returns on my server:
serialize($code);
s:204:"{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}";
What should be returned (https://duzun.me/playground/serialize):
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
You need to json_decode it first to get the wanted result:
When you use the boolean switch as second parameter in json_decode it will be an array instead of an object.
$serialized = serialize(json_decode($inputString, true));
echo $serialized;
// output:
// a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
The site you're using isn't clear about what it's doing, but it appears to be treating the string as JSON and decoding to an array before serializing it as PHP. If you want to replicate this, you can use:
$str = '{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}';
echo serialize(json_decode($str, true));
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
As pointed out in the comments, unless there's a specific reason you need serialized PHP, then just stick with the serialized JSON string you already have - it'll be both more readable and portable.

how to solve Object of class stdClass could not be converted to string PHP

I am using php and decode json format to array as following code
$sub_cats_ids=array();
$sub_cats_ids=json_decode($_POST['sub_cats']);
I want to echo first item in array to test if it works fine as following code
echo current($sub_cats_ids);
but I get this error message
Object of class stdClass could not be converted to string
I tried this code also
echo $sub_cats_ids[0];
but I get the same error message
how I can solve this issue
The error message is crystal clear, isn't it? The current element in the array is an object, not a string. You cannot "echo" an object, but only a string. So php tries to convert the object into a string but fails, since no such conversion is defined for an object of the generic standard class.
You need to use a function to output an object instead of the echo command, or you need to to convert that object into a string which you can output:
<?php
//...
var_export(current($sub_cats_ids));
Or to echo the object:
<?php
//...
echo var_export(current($sub_cats_ids), true);
UPDATE:
Your comment below suggests that unlike what you wrote in the question you do not want to output the object itself, but only a specific property of that object. That means you need to access that property in the object, php cannot somehow magically guess that you want to do that.
Without you posting additional information all I can do here is guess what you actually need to do:
<?php
//...
$object = current($sub_cats_ids);
echo $object->IT;

PHP object losing its data when casting to array

I work on the PHP framework Laravel 5, and here's my question :
I get from my DB an object with several values.
When I echo this, it show all the correct data, however, as soon as I cast it into an array, it becomes an empty array, why ?
$TonnageTotal = Collecte\Produit::pluck('tonnage'); //put everything I need into $TonnageTotal
$arrTonnage = get_object_vars($TonnageTotal); //then cast it
It does the same if I do this :
$arrTonnage=(array)$TonnageTotal;
Values I want to get are integer and are meant to be array_sum();
What am I missing ?
You may convert from collection to array by using toArray() method.
$arrTonnage = $TonnageTotal->toArray();

Convert key-value JSON to object and refer individual values directly

I am making a web app. In one part of it, I have JS send a string(in json format) to PHP.
The value php receives is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
Now, this is saved in a variable $meta. The problem I am facing is, as to how do I convert this string into an object and reference each individual entry separately.
I have tried json_decode and json_encode
and then I have referenced each variable using $meta.["date"] and $meta.date but nothing seams to work. I am getting just { as the output.
What's the correct way to do this?
$str = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$obj = json_decode($str);
echo $obj->date;
// 24-03-2014
Usually a $my_obj = json_decode($_POST['jsonstring'], 1); (true supply means it'll be returned as an assoviative array) should be the way to go. If I were you I'd probably try a var_dump($my_obj); to see what actually comes through. If it doesn't work you'll want to make sure that you correctly submit a valid json string, e.g. JSON.stringify();
You should check out the PHP doc page for json_decode here.
By default, unless you pass true as the second parameter for json_decode, the function call will return an object, which you can access the members of by using:
$meta->date
The arrow operator will allow you to access object values, not the square brackets or a dot.

how to convert object into string in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP ToString() equivalent
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
You can tailor how your object is represented as a string by implementing a __toString() method in your class, so that when your object is type cast as a string (explicit type cast $str = (string) $myObject;, or automatic echo $myObject) you can control what is included and the string format.
If you only want to display your object's data, the method above would work. If you want to store your object in a session or database, you need to serialize it, so PHP knows how to reconstruct your instance.
Some code to demonstrate the difference:
class MyObject {
protected $name = 'JJ';
public function __toString() {
return "My name is: {$this->name}\n";
}
}
$obj = new MyObject;
echo $obj;
echo serialize($obj);
Output:
My name is: JJ
O:8:"MyObject":1:{s:7:"*name";s:2:"JJ";}
Use the casting operator (string)$yourObject;
You have the print_r function, check docs.
There is an object serialization module, with the serialize function you can serialize any object.
In your case, you should simply use
$firstapiOutput->document_number
as the input for the second api.

Categories