I have an array like below. I want to extract the values . Help me out please. But this doesn't print anything. Please help me.Any help would be appreciated.May you all find this question similar.But I am unable to find any answer,because that's the way we do to find the array values.
Array
(
[0] => stdClass Object
(
[bHeader] => stdClass Object
(
[ei] => NSE
[seg] => I
)
[cNetChangeIndicator] =>
[fClosingIndex] => 10558.5
[fHighIndexValue] => 10532
[fIndexValue] => 10469
[fLowIndexValue] => 10438.5
[fOpeningIndex] => 10499.5
[fPercentChange] => -0.85
[sIndexName] => 962450
[fChange] => -89.5
[iIdxId] => 311
)
)
Thanks in advance
convert your object in to array using
$array = (array) $yourObject;
if you use json_decode than give second parameter true e.g
$array = json_decode($jsonStr,TRUE);
It will return array so no need to typecast(conveting) obj to array
also used operator '->' which help to fetch data from object
You are accessing the object in the array as if it is also an array.
You need to access the object's properties using ->
echo $arr[0]->fIndexValue;
echo $arr[0]->fChange;
echo $arr[0]->fPercentChange';
For example:
$obj = new stdClass;
$obj->fIndexValue = 10469;
$arr = array();
$arr[0] = $obj;
echo $arr[0]->fIndexValue;
Prints "10469".
Try this to print the whole thing, assuming your var is $arr:
print_r($arr);
Or for variables
print($arr[0]-->fIndexValue);
Related
The table filed name is called metadata, and within the metadata contained an array of objects such as
[{"title":"Type","value":"Hard Drive (HDD)"},{"title":"Condition","value":"Used"}]
How do I access it using PHP/Laravel. Any help is appreciated. Thanks
You need to decode it, with json_decode() php function :
$x = '[{"title":"Type","value":"Hard Drive (HDD)"},{"title":"Condition","value":"Used"}]';
$y = json_decode($x, true);
print_r($y);
Output :
Array
(
[0] => Array
(
[title] => Type
[value] => Hard Drive (HDD)
)
[1] => Array
(
[title] => Condition
[value] => Used
)
)
Now you can access the value with foreach loop as :
foreach($y as $key => $val) {
echo "Title : " . $val['title'] . $val['value'] ;
}
Above code tested here
The code you have posted is a JSON string. So you first have to decode it to a PHP array with the function json_decode. After that you can access it easily.
Try this out:
$json = '[{"title":"Type","value":"Hard Drive (HDD)"},{"title":"Condition","value":"Used"}]';
$assoc_array = json_decode($json, true); // second parameter is true to get an associative array
echo $assoc_array[0]['title'];
I want to echo an array_chunk as string, how do I do that ?
here is the code
$rt = $this->db->query("SELECT id_reg_pd FROM 043104_kuliahmhs_20152_2a0dc380_temp");
$_datao = array_chunk($rt->result(), 3);
foreach($_datao as $batman => $robin) {
print_r($robin);
}
I want echo id_reg_pd as string.
I have tried tried :
echo $robin->id_reg_pd;
but get php error like this
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Here the array from print_r($robin);
Array
(
[0] => stdClass Object
(
[id_reg_pd] => 001be76b-4e58-4cea-96cf-fee2d8e0abdc
)
[1] => stdClass Object
(
[id_reg_pd] => 001d4fe5-73f5-4bae-b126-1f787ea0104e
)
[2] => stdClass Object
(
[id_reg_pd] => 002ab28b-e0b9-464a-89fb-12552512a5d0
)
)
Loop over $robin and then check
foreach($robin as $value)
{
echo $value->id_reg_pd;
}
try like this
for($i=0;$i<count($_datao);$i++){
$newarr = (array) $robin[$i];
echo $newarr['id_reg_pd'];
}
Sahil is incorrect. It is not true that you must use a for / foreach loop to achieve your desired result. array_column() works on an array of objects. If you can use a simple implode() call to convert your array to a string, then here is a simple one-liner:
Code (Demo):
$robin=[
(object)['id_reg_pd'=>'001be76b-4e58-4cea-96cf-fee2d8e0abdc'],
(object)['id_reg_pd'=>'001d4fe5-73f5-4bae-b126-1f787ea0104e'],
(object)['id_reg_pd'=>'002ab28b-e0b9-464a-89fb-12552512a5d0']
];
//print_r($robin); // uncomment to see for yourself
//var_export(array_column($robin,'id_reg_pd')); // uncomment to see for yourself
echo implode(', ',array_column($robin,'id_reg_pd')); // implode with whatever glue you wish
Output:
001be76b-4e58-4cea-96cf-fee2d8e0abdc, 001d4fe5-73f5-4bae-b126-1f787ea0104e, 002ab28b-e0b9-464a-89fb-12552512a5d0
I'm playing with an API that's giving me data back in JSON format that I then json_decode() to the following format:
[stockData] => stdClass Object
(
[data] => stdClass Object
(
[PS3] => stdClass Object
(
[2015-01-26T20:45:01Z] => stdClass Object
(
[AMU] => 999.76
[ZIT] => 3.63
)
)
)
[status] => stdClass Object
(
[code] => 200
[text] => ok
)
)
I need some way of getting the 2015-01-26T20:45:01Z (which changes all the time).
I've tried get_Class() on the object, eg:
get_Class($bawsaq->stockData->data->PS3) (actually in a foreach loop)
But all that's returned is: "stdClass" and not the name. How can I get the object's name?
It isn't actually the object's class: it's the name of the property that contains the stdClass object. So you'd need to get the first object property name from $bawsaq->stockData->data->PS3. Which is a bit tricky, actually.
It's nicer to work with arrays. If you use the $assoc parameter of json_decode, you can get an associative array instead of an object whenever a JSON object appears. This is much easier to deal with in PHP.
$bawsaq = json_decode($jsonData, true);
You can get the key name with key:
$dateTime = key($bawsaq['stockData']['data']['PS3']);
When you decode the JSON, use
$bawsaq = json_decode($json, true);
This will return associative arrays instead of stdClass objects for all the JSON objects. Then you can use
$keys = array_keys($bawsaq['stockData']['data'];
$date = $keys[0];
You can use get_object_vars method.
$obj = new stdClass();
$obj->field1 = 'value1';
print_r(get_object_vars($obj));
Result:
Array
(
[field1] => value1
)
You can use the second argument to json_decode. This will return the data as an associative array instead of an object list, so you could simply use
$input = json_decode($jsonInput, true);
$key = key($input['stockData']['data']['PS3']);
$data = $input['stockData']['data']['PS3'][$key];
or a foreach-loop. See also key on php.net.
I am using $this->db->get_where() to get data from database in codeigniter.
Its returning following which I got using print_r()
Its looks like array of stdClass object. Anyone who how to access values inside this array.
Array ( [0] =>
stdClass Object (
[id] => 1
[password321] => qwerty
[email123] => example#gmail.com
[username123] => xyz
)
)
It shows an array of objects. There is only one object in it.
If:
$var = $this->db->get_where();
Then:
echo $var[0]->id;
Access it like any other object.
echo $array[0]->id //1
echo $array[0]->username123 //xyz
And so on. If you have multiple objects inside the array, run it through a for loop to iterate the array.
For example:
for ($i=0;$i<sizeof($array);$i++) {
echo $array[$i]->[object property];
}
This question already has answers here:
Convert a PHP object to an associative array
(33 answers)
Closed 1 year ago.
My array is like:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => demo1
)
[1] => stdClass Object
(
[id] => 2
[name] => demo2
)
[2] => stdClass Object
(
[id] => 6
[name] => otherdemo
)
)
How can I convert the whole array (including objects) to a pure multi-dimensional array?
Have you tried typecasting?
$array = (array) $object;
There is another trick actually
$json = json_encode($object);
$array = json_decode($json, true);
You can have more info here json_decode in the PHP manual, the second parameter is called assoc:
assoc
When TRUE, returned objects will be converted into associative arrays.
Which is exactly what you're looking for.
You may want to try this, too : Convert Object To Array With PHP (phpro.org)
Just use this :
json_decode(json_encode($yourArray), true);
You can use array_walk to convert every item from object to array:
function convert(&$item , $key)
{
$item = (array) $item ;
}
array_walk($array, 'convert');
Assuming you want to get to this pure array format:
Array
(
[1] => "demo1",
[2] => "demo2",
[6] => "otherdemo",
)
Then I would do:
$result = array();
foreach ($array as $object)
{
$result[$object->id] = $object->name
}
(edit) Actually that's what I was looking for possibly not what the OP was looking for. May be useful to other searchers.
You should cast all objets, something like :
$result = array();
foreach ($array as $object)
{
$result[] = (array) $object
}
As you are using OOP, the simplest method would be to pull the code to convert itself into an array to the class itself, you then simply call this method and have the returned array populate your original array.
class MyObject {
private $myVar;
private $myInt;
public function getVarsAsArray() {
// Return the objects variables in any structure you need
return array($this->myVar,$this->myInt);
}
public function getAnonVars() {
// If you don't know the variables
return get_object_vars($this);
}
}
See: http://www.php.net/manual/en/function.get-object-vars.php for info on get_object_vars()
it you have object and you want to set a value as array
use
$this->object->pluck('name');
then you get the value as array of names like
["name1", "name2", "name3"];