Can anyone help me with this little problem please? I need to get (echo) the name, id and link from an array but after hours trying I have not been able, see the array below... Thank you in advanced.
Array
(
[campaigns] => Array
(
[0] => Campaign Object
(
[name] => My name 1
[id] => 123456789012
[link] => 123456789012
)
[1] => Campaign Object
(
[name] => My name 2
[id] => 123456789012
[link] => 123456789012
)
[2] => Campaign Object
(
[name] => My name 3
[id] => 123456789012
[link] => 123456789012
)
)
)
If you know which campaign you want, you can get it like this:
echo($data['campaigns'][0]->name);
echo($data['campaigns'][0]->id);
echo($data['campaigns'][0]->link);
If you want to loop through all of them, you could do something like this:
foreach ($data['campaigns'] as $item) {
echo($item->name . "\n");
echo($item->id . "\n");
echo($item->link . "\n");
}
This is all a bit of a guess because we don't know what the Campaign class actually looks like - there may be a getName() method that you should be using instead of just accessing the name value directly, for example.
foreach ($array['campaigns'] as $key => $value){
echo "Name: ".$value->name." ID: ".$value->id." Link: ".$value->link."\n";
}
Question is a little light on details to debug but have you tried this?
<?php echo $campaigns[0]->name; ?>
Related
I stored some data to an array using this code ($rval['arr']):
$id = $this->input->get("id");
$rval['arr'] = $this->General_model->details($id);
When I print my result by using print_r($rval['arr']); it shows like this:
Array ( [0] => Array ( [id] => 54 [name] => Rajib [address] => DumDum [mobile] => 9865321245 [doj] => 21-2-2010 [fare] => 1245 [img_name] => Penguins.jpg ) )
Now I want to store the address value to a variable like add. How can I do this?
Try this....
$address=$rval['arr'][0]['address'];
demo
You can also do it in a foreach() loop:
foreach($rval['arr'] as $array){
echo $array['name'];
echo $array['address'];
echo $array['mobile'];
}
I have a JSON file which iam using ( file_get_contents ) from a url, to extract the values to a good formatted way. The problem is that i need to echo out a specific value content but failed to do that. So here is the piece of code iam providing here may be if someone would like to help me out to do it.
Array
(
[type] => result
[data] => Array
(
[0] => Array
(
[title] => My Radio Name
[song] => Artist Name - Song Name
[track] => Array
(
[artist] => Artist Name
[title] => Song Name
[album] => Unknown
[royaltytrackid] => 0
[id] => 542
[playlist] => Array
(
[id] => 45
[title] => ALL SONGS
)
.....
SO i need to echo out [artist] AND [title] Which will be echoed as:
Artist Name | Song Name
Thanks in advance any help would be appreciated :)
Depends on format of your json but below code should work:
foreach ( $json['data'] as $entry ){
foreach ( $entry['track'] as $track ){
echo $track['artist'] ." | ". $track['title'] ."<br/>";
}
}
I have an issue and trying to solve in php. I need ideas of yours for solving this issue.
I'm gonna try to explain, hope I can.
I have an array like this:
$mainArr = array(array("a","b","c"),array("1","2","3"),array("x","y","z"));
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => x
[1] => y
[2] => z
)
)
What I'm trying to do is printing all 3 possibilities of leaf elements. Like that:
a1x
a1y
a1z
a2x
a2y
a2z
a3x
a3y
a3z
b1x
b1y
b1z
.
.
.
I try to form an algorithm to make this, but I got stuck.
Can you please help me for this issue?
Thanks in advance
You should check RecursiveIteratorIterator
I believe this is solution for your problem.
foreach ($mainArr[0] as $level1)
foreach ($mainArr[1] as $level2)
foreach ($mainArr[2] as $level3)
echo $level1 . $level2 . $level3 . "<br />";
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 have a little problem with array. Im using codeigniter. What i want to do, is something like that:
$studentSchool = $students->schoolId;
echo $shools->id[$studentSchool]->schoolName;
Its in foreach $students loop, and array with schools looks like that:
Array ( [0] => stdClass Object ( [id] => 1 [schoolName] => Akademia Ekonomiczna ) [1] => stdClass Object ( [id] => 2 [schoolName] => Politechnika ) )
Those are my first steps in php and codeigniter, so please have mercy :)
If $schools is the array, you have to access the it as an array. It won't have an id property;
You should build your $schools array such that the index of element corresponds to the ID of the school. I.e. you should have:
Array (
[1] => stdClass Object ( [id] => 1 [schoolName] => ... )
[2] => stdClass Object ( [id] => 2 [schoolName] => ... )
)
Then you can do:
echo $schools[$studentSchool]->schoolName;
Or, if the schools are sorted by ID and the IDs are continuous, you can also do:
echo $schools[$studentSchool - 1]->schoolName;
Otherwise you have to loop over the array to find the right entry for the given ID which is expensive and unnecessary.
Learn more about arrays.
Is this what you're looking for?
foreach ($students as $student):
// Prints the School name for this student
echo $student->schoolName;
endforeach;
Or maybe this?:
// Prints the School name for the first student
echo $students[0]->schoolName
EDIT: This is what you want?
$studentSchool = $students->schoolId;
echo $shools[$studentSchool]->schoolName;