How to read this attributes in this xml using PHP? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
http://idragonlk.com/channels.xml
This is the xml.
What wanna be is read this channel and programs in a loop.
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("channels.xml");
$xmlObject = $xmlDoc->getElementsByTagName('channel');
$itemCount = $xmlObject->length;
for ($i=0; $i < $itemCount; $i++){
$title = $xmlObject->item($i)->getElementsByTagName('display-name')->item(0)->childNodes->item(0)->nodeValue;
print "Finished Item $title n<br/>";
}
?>
I have tried this. But it prints the txt values. I'm unable to get attribute values.
Wanna get value of
channel id=' '
lang='' of display-name
icon src='' value
program start ='' value
program stop = '' value
program channel = '' value
in program rating value
Plz can anyone help me?

Try this, Maybe Helpful to you.
<?php
/*CURL PHP library to get file contents*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://idragonlk.com/channels.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
/*End*/
/*Takes a well-formed XML string and returns it as an object.*/
$XMLOutPut = simplexml_load_string($output);
//$channel_list = getChannel_list($XMLOutPut);
//$programme_list = getProgramme_list($XMLOutPut);
echo "<pre>";print_r(getChannel_list($XMLOutPut));
echo "<pre>";print_r(getProgramme_list($XMLOutPut));
exit;
/*Function to fetch channel and programme*/
//channel
function getChannel_list($mainObj){$Result_List = array();
for($i=0;$i<count($mainObj->channel);$i++){
$Result_List[] = objectToArray($mainObj->channel[$i]);
}
return $Result_List;
}
//programme
function getProgramme_list($mainObj){$Result_List = array();
for($i=0;$i<count($mainObj->programme);$i++){
$Result_List[] = objectToArray($mainObj->programme[$i]);
}
return $Result_List;
}
//Convert Object to array.
function objectToArray($obj) {
if (is_object($obj)) {$obj = get_object_vars($obj);}
if (is_array($obj)) {return array_map(__FUNCTION__, $obj);}
else {return $obj;}
}
?>
OUTPUT
Array
(
[0] => Array
(
[#attributes] => Array
(
[id] => 4seven UK
)
[display-name] => 4seven UK
[icon] => Array
(
[#attributes] => Array
(
[src] => 4seven UK.png
)
)
[url] => http://www.tvguide.co.uk
)
[1] => Array
(
[#attributes] => Array
(
[id] => 5STAR UK
)
[display-name] => 5STAR UK
[icon] => Array
......
......

Related

How to add specific key to array values? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I stored links to $fotolar variable and I converted to array with explode.
My code:
$ex = explode('<br>', $fotolar);
echo "<pre>";
print_r($ex);
echo "</pre>";
Output:
Array
(
[0] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg
[1] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg
[2] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
)
How can I add "url" key to this array?
For example:
Array
(
[0] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg
)
[1] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg
)
[2] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
)
)
After exploded string to array, you can loop through this array to create a new array like this:
<?php
$str = <<<STR
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
STR;
$arr = explode('<br>', $str);
$result = [];
foreach ($arr as $v) {
$result[]['url'] = $v;
}
var_dump($result);
Here is a one liner for you using array_map
$str = <<<STR
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
STR;
$array = array_map(function($i){return['url'=>$i];},explode('<br>',$str));
Sandbox

Add one more element to array in loop with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have array like below:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg"]];
i want to add one more element to array $arr by "link"=>"uploads/hello.jpg"
My expected result:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg","link"=>"uploads/hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg","link"=>"uploads/abc.jpg"]];
Any solution for this thank.
You can iterate over the array using foreach, passing a reference into the loop to allow the values to be modified directly:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg"]];
foreach ($arr as &$a) {
$a['link'] = 'uploads/' . $a['pict'];
}
print_r($arr);
Output:
Array
(
[0] => Array
(
[id] => 001
[name] => Hello
[pict] => hello.jpg
[link] => uploads/hello.jpg
)
[1] => Array
(
[id] => 002
[name] => Abc
[pict] => abc.jpg
[link] => uploads/abc.jpg
)
)
Demo on 3v4l.org
You can iterate over each element in the array and set it that way.
for ($i = 0; $i < count($arr); i++) {
$arr[$i]['link'] = 'uploads/'.$arr[$i]['pict'];
}
foreach($arr as $key => $value){
$arr[$key]['link'] = "uploads/".$value['pict'];
}
Use the foreach loop to modify the original array. The $key value is used to refer to each index in the array.

How to read from multi-dimensional arrays in PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to do something I've never tried before: reading from APIs, and using the returned values in my website. To do this I need to read from multi-dimensional arrays in PHP.
See below the result I am getting from the API callback, when I run the following lines of code:
$tempContents = json_decode($data, true);
echo '<pre'; print_r($tempContents); echo '</pre';
Results:
Array (
[data] = Array
(
[boards] = Array
(
[0] = Array
(
[columns] = Array
(
[0] = Array
(
[title] = Name
)
[1] = Array
(
[title] = note
)
[2] = Array
(
[title] = notes2
)
[3] = Array
(
[title] = Status
)
)
)
)
)
[account_id] = xxxxxxx
)
So that's great, I can see the information there; however, I'm not sure about how to construct a foreach function to retrieve individual values. For example; the name of the second title value.
You can do this in 2 way
using foreach() directly:-
foreach($tempContents['data']['boards'][0]['columns'] as $title){
echo $title['title'].PHP_EOL;
}
Or using array_column() along with foreach():
$finalArray = array_column($tempContents['data']['boards'][0]['columns'],'title');
foreach($finalArray as $title){
echo $title.PHP_EOL;
}
Sample Output:- https://3v4l.org/4ERqd

Placing data into an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the following code:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = $attributeSelected['id'];
}
?>
This saves the data as:
Array (
[0] => SimpleXMLElement Object ( [0] => 23914175_laptop )
[1] => SimpleXMLElement Object ( [0] => 23914175_laptop )
[2] => SimpleXMLElement Object ( [0] => price_range_10_50004 )
)
However I just want the array data and not to include the "SimpleXMLElement Object". So I added the following code:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = json_decode(json_encode($attributeSelected['id']), true);
}
?>
Which now gives me the data as:
Array (
[0] => Array ( [0] => 23914175_laptop )
[1] => Array ( [0] => 23914175_laptop )
[2] => Array ( [0] => price_range_10_50004 )
)
I'm not sure what I'm doing wrong. I just want to save the data to an array and then use an If statement to check if the array contains specific data.
How about:
$attr_data[] = $attributeSelected['id'][0];
You are trying to store an object, if you need the first value of object you should change the third line to:
$attr_data[] = $attributeSelected['id']->0;
Please add a snippet of your XML, it makes things a lot clearer.
typecast the values to string:
$xml = simplexml_load_string($x); // assume XML in $x
$att = array();
foreach ($xml->search as $a) {
$att[] = (string)$a['id'];
}
see it working: http://codepad.viper-7.com/P0DI0S
The class SimpleXMLElement has a __toString method, so you could call it directly (which is kinda ugly IMO) or just typecast it to a string:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = (string) $attributeSelected['id'];
// Or
$attr_data[] = $attributeSelected['id']->__toString();
}
?>
Read more at http://php.net/manual/en/simplexmlelement.tostring.php

how to loop over this array to obtain only the number 6 and 2? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to loop over the array called $res, but I want to obtain only the values 6 and 2, for each name it is an id, is its with a foreach o just a normal for? I am confused because the array has two arrays insiden and it increments if I add a new name like 'Joe'
$arrayDirectory = array('Nick', 'Alex');
$res = array();
foreach($arrayDirectory as $user) {
$res[] = $obj->obtainID($user);
}
echo print_r($res);
Array ( [0] => Array ( [0] => Array ( [id_usuario] => 6 [0] => 6 ) ) [1] => Array ( [0] => Array ( [id_usuario] => 2 [0] => 2 ) ) ) 1
foreach ($res as $item) {
echo $item[0][0];
}
Or
foreach ($res as $item) {
echo $item[0]['id_usuario'];
}
Depending on what you are looking for
With PHP5.3 you can also use array_map() with a lambda
$idUsarios = array_map(function ($item) {
return $item[0]['id_usario'];
}, $res);
Change
$res[] = $obj->obtainID($user);
to
$user = $obj->obtainID($user);
$res[] = $user[0]['id_usuario'];

Categories