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")));
Hi would love some help on how to perform this. Cause so far what I'm doing now is failing.
this is the sample output of json turned to array.
Array
(
[0] => Array
(
[0] => Array
(
[value] => lit-PR-00-Preparing-Precise-Polymer-Solutions.html
)
)
[1] => Array
(
[0] => Array
(
[value] => 90Plus
)
)
[2] => Array
(
[0] => Array
(
[value] => Particle Size Analyzer
)
)
)
So far this is what I got so far and It's still not outputting the value I need. Would appreciate some help on what I'm doing wrong thanks.
$decode = json_decode($row['elements'], true);
echo '<pre>';
//print_r($decode);
print_r(array_values($decode));
echo '</pre>';
echo ($value['0'][1][value]);
$decode = json_decode($row['elements'], true);
// iterate through array
foreach($decode as $array_row) {
echo $array_row[0]['value'];
}
// display specific row #2
echo $decode[2][0]['value'];
PHP Arrays
I'm having some problems getting the array in these objects. When I print_r(), the following code is printed. $message_object is the name of the object.
SimpleXMLElement Object
(
[header] => SimpleXMLElement Object
(
[responsetime] => 2012-12-22T14:10:09+00:00
)
[data] => SimpleXMLElement Object
(
[id] => Array
(
[0] => 65233
[1] => 65234
)
[account] => Array
(
[0] => 20992
[1] => 20992
)
[shortcode] => Array
(
[0] => 3255
[1] => 3255
)
[received] => Array
(
[0] => 2012-12-22T11:04:30+00:00
[1] => 2012-12-22T11:31:08+00:00
)
[from] => Array
(
[0] => 6121843347
[1] => 6121820166
)
[cnt] => Array
(
[0] => 24
[1] => 25
)
[message] => Array
(
[0] => Go tramping wellington 11-30
[1] => Go drinking Matakana 2pm
)
)
)
I'm trying to get the id arrays out of the objects with a foreach:
foreach($message_object->data->id AS $id) {
print_r($id);
}
The following reply is sent:
SimpleXMLElement Object ( [0] => 65233 ) SimpleXMLElement Object ( [0] => 65234 )
How do I get the value of [0] or am I going about this wrong? and is there a way to loop though the results and get the object keys?
I have tried to echo $id[0] but it returns no result.
When you use print_r on a SimpleXMLElement there comes magic in between. So what you see is not actually what is there. It's informative, but just not the same as with normal objects or arrays.
To answer your question how to iterate:
foreach ($message_object->data->id as $id)
{
echo $id, "\n";
}
to answer how to convert those into an array:
$ids = iterator_to_array($message_object->data->id, 0);
As this would still give you the SimpleXMLElements but you might want to have the values you can either cast each of these elements to string on use, e.g.:
echo (string) $ids[1]; # output second id 65234
or convert the whole array into strings:
$ids = array_map('strval', iterator_to_array($message_object->data->id, 0));
or alternatively into integers:
$ids = array_map('intval', iterator_to_array($message_object->data->id, 0));
You can cast the SimpleXMLElement object like so:
foreach ($message_object->data->id AS $id) {
echo (string)$id, PHP_EOL;
echo (int)$id, PHP_EOL; // should work too
// hakre told me that this will work too ;-)
echo $id, PHP_EOL;
}
Or cast the whole thing:
$ids = array_map('intval', $message_object->data->id);
print_r($ids);
Update
Okay, the array_map code just above doesn't really work because it's not strictly an array, you should apply iterator_to_array($message_object->data_id, false) first:
$ids = array_map('intval', iterator_to_array$message_object->data->id, false));
See also: #hakre's answer.
You just need to update your foreach like this:
foreach($message_object->data->id as $key => $value) {
print_r($value);
}
This question already has answers here:
simplexml_load_file parse [#attributes] => Array
(3 answers)
Closed 9 years ago.
The goal: Using PHP/CodeIgniter, I need to get a list of users and their custom field names and values in a usable array. If I can just get to the items, I can do what I need. Please see OUTPUT with notes for "// <<< " so you can see where I'm stuck. I just can't get past the dang [#attributes].
I'm using PHP to connect to Redmine using ActiveResource.php and their built in REST API. I want to get a list of all the users and am getting the OUTPUT below.
Models/Employees_model.php:
<?php
require_once ('ActiveResource.php');
class Employees_model extends ActiveResource {
var $site = 'http://user:password#localhost:8080/redmine/';
var $element_name = 'user';
var $request_format = 'xml';
function __construct() {
parent::__construct();
}
}
?>
controllers/employees.php:
public function employees () {
$employees = new Employees_model();
$data['employeeList'] = $employees->find('all');
$this->load->view('customers/ajaxEmployees', $data);
}
view/ajaxEmployees.php:
<?php
//I can get the following with no problem
echo $employee->id . "<br/>";
echo $employee->firstname . "<br/>";
echo $employee->lastname . "<br/>";
//This is where I'm stuck (see OUTPUT for [#attributes] and "// <<<" notes)
echo $employee->custom_fields->custom_field;
?>
OUTPUT:
Array
(
[0] => Employees_model Object
(
[id] =>
[site] => http://user:password#localhost:8080/redmine/
[element_name] => user
[request_format] => xml
[extra_params] =>
[user] =>
[password] =>
[element_name_plural] => users
[_data] => Array
(
[id] => 16
[login] => jschmoe
[firstname] => Joe
[lastname] => Schmoe
[mail] => joe#example.com
[created_on] => 2012-08-24T01:58:21Z
[last_login_on] => SimpleXMLElement Object
(
)
[custom_fields] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => array
)
[custom_field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Quality Control //<<< I need this
[id] => 2
)
[value] => 1 //<<< I need this to know that QualityControl = 1
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Technical Contractor // <<< I need this
[id] => 3
)
[value] => 0 // <<< I need this to know that TechnicalContractor = 0
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Content //<<< I need this
[id] => 4
)
[value] => 0 // <<< I need this to know Content = 1
)
)
)
)
)
)
Thank you all so much for your help. After wasting many hours searching around, trying everything I came across and everything I could think of - I figured I'd better wave my little white flag. :(
$employee->custom_fields->custom_field; is an array, and you can foreach over it to get each name attribute and its corresponding value using SimpleXMLElement::attributes().
foreach ($employee->custom_fields->custom_field as $cf) {
// Loop over the custom field nodes and read the name of each
switch ($cf->attributes()->name) {
// Load a variable on each matching name attribute
// or do whatever you need with them.
case "Quality Control":
$quality_control = $cf->value;
break;
case "Technical Contractor":
$technical_contractor = $cf->value;
break;
case "Content":
$content = $cf->value;
break;
}
}
Or if you don't know in advance all the ones you'll need, load them into an array:
$employee_attrs = array();
foreach ($employee->custom_fields->custom_field as $cf) {
// Add an array key of the name, whose value is the value
$attrs[$cf->attributes()->name] = $cf->value;
}
var_dump($employee_attrs);
The attributes() function is what you're looking for I think. For example
foreach ($employee->custom_fields->custom_field as $line) {
echo "[" . $line->attributes()->name . "]" . $line->value . "\n";
}
Also you might have to use this instead:
foreach ($employee->_data->custom_fields->custom_field as $line) {
(not sure, try both)
I need some help. I have a variable containing this string;
[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]
I need to retrieve the id and value for each pair and make it any array in PHP.
You can use json_decode and pass the second param as true so it returns an array like this
$json = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$decoded = json_decode($json,true);
print_r($decoded);
Working Example
Output would be
Array
(
[0] => Array
(
[id] => 17
[value] => 123456789
)
[1] => Array
(
[id] => 18
[value] => 2012-06-13
)
[2] => Array
(
[id] => 19
[value] => Kampala
)
[3] => Array
(
[id] => 20
[value] => 1
)
.......
)
Which you can loop through using foreach like.
foreach($decoded as $de){
// access id with $de['id']
// access value with $de['value']
}
You have got an json string. You can convert it to an array by using function json_decode
Check this code .
$str = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"}, {"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$array = json_decode($str);
foreach($array as $temp){
echo "ID : ".$temp->id."\t Value: ".$temp->value;
echo "<br />";
}