I have the following result
Array (
[0] => stdClass Object ( [name] => Identification )
[1] => stdClass Object ( [name] => Assay )
[2] => stdClass Object ( [name] => pH(Acidity/Alkalinity))
[3] => stdClass Object ( [name] => Sterility )
)
What i want is to separate the object array values using a comma and return as a string,
so as to have this result:
Identification, Assay, ph(Acid/Alkalinity), Sterility
I have tried the following
$data=(array)$result;
$answer=implode(",",$data);
This return :
Message: Object of class stdClass could not be converted to string
How best can this be achieved?
You are missing the fact that you are dealing with an array of objects.
Looks like you can achieve that by doing:
$output = array_map(function ($object) { return $object->name; }, $input);
echo implode(', ', $output);
To use this feature in the 'title' attribute I did:
echo implode('
', $output);
It will convert the array of objects to a string, and you can store it in the database, use
serialize(array_of_object)
Might help someone. I used it in my case. Thank you!
Related
Having real issues with this. I want to be able to get a value from this data which is returned via an API.
ie get value by
$CM_user_customfields['Organisation'],
$CM_user_customfields->Organisation.
is that even possible? I have tried loops and rebuilding the array but i always end up with a similar results and perhaps overthinking it.
I can't use the [int] => as the number of custom fields will be changing a lot.
$CM_user_customfields = $CM_details->response->CustomFields ;
echo '<pre>' . print_r( $CM_user_customfields, true ) . '</pre>';
// returns
Array
(
[0] => stdClass Object
(
[Key] => Job Title
[Value] => Designer / developer
)
[1] => stdClass Object
(
[Key] => Organisation
[Value] => Jynk
)
[2] => stdClass Object
(
[Key] => liasoncontact
[Value] => Yes
)
[3] => stdClass Object
...
many thanks, D.
I recommend convert to associative array first:
foreach($CM_user_customfields as $e) {
$arr[$e->Key] = $e->Value;
}
Now you can access it as:
echo $arr['Organisation'];
You can also achieve it by: (PHP 7 can convert stdClass and will do the trick)
$arr = array_combine(array_column($CM_user_customfields, "Key"), array_column($CM_user_customfields, "Value")));
I have an array looking something like this:
Array
(
[0] => my_val_one
[1] => my_val_two
)
I then have an object looking something like this:
stdClass Object
(
[id] => 123123
[name] => my_name
[my_val_one] => stdClass Object
(
[my_val_two] => 1
[my_val_three] => 2323
[my_val_four] => 546567
)
)
I want to reference the following object value:
$ob->my_val_one->my_val-two
I'm not sure how to reference this class property from array values that I have.
array_reduce helps here:
$path = ['my_val_one', 'my_val_two'];
$value = array_reduce($path, function ($o, $p) { return $o->$p; }, $ob);
If I have understood correctly, you want to use the string values of the first array to access the StdClass object. You can do this by accessing the attributes dynamically. Here $obj is the StdClass and $arr is your array.
$obj->{$arr[0]}->{$arr[1]}
I have a object as shown below and i want to extract data from
stdClass Object
(
[day1] => stdClass Object
(
[0] => 12.06.2015
[part1] => Array
(
[0] => 19.00
[1] => 22.00
)
[part2] => Array
(
[0] =>
[1] =>
)
)
)
How will i get date as shown above with key 0. I can get others as
$string->day1->part1[0]
How can i get date "12.06.2015" ? This seems to be complicated.
JSON string for reference
{"day1":{"0":"12.06.2015","part1":["19.00","22.00"],"part2":["",""]},"day2":{"0":"13.06.2015","part1":["09.00","12.00"],"part2":["13.00","17.00"]}}
used json_decode to decode it.
You can use this:
echo $string->day1->{0};
Or my preference, decode as an array with the second argument set to true in json_decode() to use this:
echo $string['day1'][0];
I have a stdclass object as shown below:
stdClass Object
(
[text] => Parent
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Laurence W. Lane Jr.
[url] => http://www.freebase.com/view/m/0c02911
)
)
)
I iterate over multiple such objects, some of which have
stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
I was wondering how I would access the "values" object if it comes after a "text" that has "Parent" as its value?
there are serveral ways to turn it to array:
First Solution:
$value = get_object_vars($object);
Second Solution:
$value = (array) $object;
Third Solution
$value = json_decode(json_encode($object), true);
to get value of converted array
echo $value['values']['0']['id'];
The alternate way to access objects var without convert the object, try
$object->values->{'0'}->id
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
echo get_object_vars($object)['values']['0']['id'];
I had the same issue, still not so sure why but I was able to get it working using this workaround:
$k2 ="1";
$elements = json_decode('{"id":"1","name":"User1"}');
//$elements['id'] == $k2; //****Not Working
$tmp = (object)$elements;
$tmp = $tmp ->id; //****Working
//$tmp =$elements['id'] ; //****Not Working
return $tmp == $k2;
I have to say that sometimes accessing the element as array works and some times not,(On PHP7 it worked for me but on PHP5.6 it didn't).
$elements can be Array to but I chose to demonstrate with json string.
I hope this helps somehow !!!
$Obj=stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
$Values= $result->values;
$Item = $Values[0];
$id=$Item->id;
$text = $Item->text;
$url=$Item->url;
I'm doing the same thing and all I did was this;
<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
this can help you accessing subarrays in php using codeigniter framework
foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
echo $novo_puto_ultimos_30_dias;
What you are looking for is the Object['values'][0]: 'values' is the keymap just like 'text', and [0] is the index inside that array you wish to access. so if you would like to get the id deep in the nest, you'd have to do something like
Object['values'][0]['id']
or
Object['values'][0]->id
which should give you /m/0c02911. But I have no idea how you are doing your loop, so you will have to adjust it to your needs and place proper variables where they need to go in that code in your loop. Not exactly sure which language you are working with.
I'm still new at PHP and I can't seem to count the number of Objects within another object. The stdClass object looks like this:
stdClass Object (
[data] => Array (
[0] => stdClass Object (
[Code] => ABC
[Title] => Alphabet
[sections] => Array (
[0] => stdClass Object (
[Name] => Sounds
[sections] => Vowels
)
)
)
)
I must count the number of elements in this object so i can echo it properly. For the data, I was able to do it:
$number = count($hanap->data);
I don't know how to do it for the sections.
$number = count($hanap->data->sections); // does not work.
Thanks. Any help will be greatly appreciated. :)
this will solve your problem, just cast the object to array and count it
$total = count((array)$obj);
PHP: Count an stdClass object
count($hanap->data[0]->sections)
You are missing the first member of the array where they are...
$number = count($hanap->data[0]->sections)