I have a SimpleXMLElement Object that I want to import with PHP.
But it only loop through the first row in the XML file.
I also want to show the specific columm with $item->columm1 instead of show the whole row whith all it's values .
$url = 'file.xml';
$sxml = simplexml_load_file($url);
foreach($sxml->Departure->attributes() as $item)
{
echo $item;
}
EDIT: Here is the output, notice that I have edit the orginal values with text1, text2.
SimpleXMLElement Object (
[Departure] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => text1 [type] => text2 [stop] => text3 [time] => text4 [date] => text4 [direction] => text5 )
EDIT2: Output->
object(SimpleXMLElement)#4 (2) { ["#attributes"]=> array(6) { ["name"]=> string(6) "text1" ["type"]=> string(3) "text2" ["stop"]=> string(12) "text3" ["time"]=> string(5) "text4" ["date"]=> string(8) "text5" ["direction"]=> string(10) "text6" } ["JourneyDetailRef"]=> object(SimpleXMLElement)#5 (1) { ["#attributes"]=> array(1) { ["ref"]=> string(125) "text7" } } }
EDIT3: Output->
object(SimpleXMLElement)#7 (1) { [0]=> string(6) "text1" } object(SimpleXMLElement)#8 (1) { [0]=> string(3) "text2" } object(SimpleXMLElement)#7 (1) { [0]=> string(12) "text3" } object(SimpleXMLElement)#8 (1) { [0]=> string(5) "text4" } object(SimpleXMLElement)#7 (1) { [0]=> string(8) "text5" } object(SimpleXMLElement)#8 (1) { [0]=> string(10) "text6" }
$item has become the object. When you want to loop over the item, make sure you tell what part of the item you want to echo out to the screen.
For example:
$url = 'file.xml';
$sxml = simplexml_load_file($url);
//foreach loop
foreach($sxml->Departure as $item){
//vardump
var_dump($item);
//construct next piece of code
foreach($item->attributes() as $key => $piece){
echo $key.' = '.$piece;
}
}
Related
RESOLVED: I WAS ADDING EXTRA INDEX TO $firms LIKE $firms['type'] = 'ABC' AS $firms Already Contains CHILD ARRAYS. My BAD, Sorry.
This might be duplicate But i couldn't figure it out T _ T Sorry
As i am printing array results coming right from database, its continuously annoying me with error
Illegal string offset 'id'
When i simply print_r result array it gives me following result.
echo "<pre>";
foreach ($firms as $firm) {
print_r($firm[0]);
}
echo "</pre>";
Result:
Array
(
[id] => 7923
[rank] => 0
[name] => CBRE
)
Array
(
[id] => 7919
[rank] => 0
[name] => Aecom
)
But if i try to print 'id' it gives me error:
echo "<pre>";
foreach ($firms as $firm) {
echo $firm[0]['id'];
}
echo "</pre>";
Error:
Illegal string offset 'id'
VAR_DUMP($firms) , Result:
array(3) {
[0]=>
array(1) {
[0]=>
array(3) {
["id"]=>
int(7923)
["rank"]=>
int(0)
["name"]=>
string(4) "CBRE"
}
}
[1]=>
array(1) {
[0]=>
array(3) {
["id"]=>
int(7919)
["rank"]=>
int(0)
["name"]=>
string(5) "Aecom"
}
}
[2]=>
array(1) {
[0]=>
array(3) {
["id"]=>
int(6793)
["rank"]=>
int(1)
["name"]=>
string(5) "AECOM"
}
}
}
Im trying to remove a session['cart_items] array from a multi array and I can't seem to get the delete part to work.
I'm getting the id through: $_POST['product] however I've tried the following and getting a bit stuck:
$temp = array_flip($_SESSION['cart_items']);
unset($_SESSION['cart_items'][$temp[$_POST['product']]]);
I've also tried:
unset($_SESSION['cart_items'][$key]);
My output is:
Array
(
[0] => Array
(
[0] => Array
(
[item_id] => 407
[item_name] => Am I Bothered? About Homophobia
[item_qty] => 22
)
)
)
An help would be great
Multiple array result:
array(1) {
["cart_items"]=>
array(2) {
[1]=>
array(1) {
[0]=>
array(3) {
["item_id"]=>
string(3) "407"
["item_name"]=>
string(31) "Am I Bothered? About Homophobia"
["item_qty"]=>
string(2) "50"
}
}
[2]=>
array(1) {
[0]=>
array(3) {
["item_id"]=>
string(4) "1131"
["item_name"]=>
string(50) "10 Ways A Condom Can’t Protect You – Postcards"
["item_qty"]=>
string(2) "14"
}
}
}
}
for(i=0;i<sizeof($_SESSION['cart_items'][0]);i++){
if($_SESSION['cart_items'][0][i]['item_id'] == $key);
unset($_SESSION['cart_items'][0][i]['item_id']);
}
I need to sort the array by the value of the given key 0 in this case.
So here is my array:
array(11) {
[0]=> array(3) {
[0]=> string(3) "5"
[1]=> string(1) "3"
[2]=> string(2) "21"
}
[1]=> array(3) {
[0]=> string(3) "0.5"
[1]=> string(1) "3"
[2]=> string(3) "may"
}
[2]=> array(3) {
[0]=> string(3) "2.2"
[1]=> string(1) "3"
[2]=> string(16) "sport"
}
}
Result must be sorted by the value of key 0:
array(11) {
[0]=> array(3) {
[0]=> string(3) "0.5"
[1]=> string(1) "3"
[2]=> string(2) "may"
}
[1]=> array(3) {
[0]=> string(3) "2.2"
[1]=> string(1) "3"
[2]=> string(3) "sport"
}
[2]=> array(3) {
[0]=> string(3) "5"
[1]=> string(1) "3"
[2]=> string(16) "21"
}
}
I attempted it with this code:
function sort_by_second($i,$j){return $i[0]-$j[0];};
usort($mas,'sort_by_second');
I don't understand why it doesn't work.
Try below code:
$array = array(
array("5","3","21"),
array("0.5","3","may"),
array("2.2","3","sport"),
);
//print_r($array);
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
$result = subval_sort($array, 0);
print_r($result);
Result:
Array
(
[0] => Array
(
[0] => 0.5
[1] => 3
[2] => may
)
[1] => Array
(
[0] => 2.2
[1] => 3
[2] => sport
)
[2] => Array
(
[0] => 5
[1] => 3
[2] => 21
)
)
Demo:
http://3v4l.org/q4ZtL#v430
PHP >= 5.5.0:
array_multisort(array_column($array, 0), SORT_ASC, $array);
in fear of duplicating content, i have looked through so many similar SO questions, but i think i need a bit more than code, to tell me how solve my problem- would be lovely with some explaination too.
How do i turn $list:
array(4) {
[0]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[1]=>
array(2) {
["title"]=>
string(6) "Zarafa"
["id"]=>
int(34)
}
[2]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[3]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
}
Into $list:
array(2) {
[0]=>
array(2) {
["title"]=>
string(8) "Zambezia"
["id"]=>
int(31)
}
[1]=>
array(2) {
["title"]=>
string(6) "Zarafa"
["id"]=>
int(34)
}
}
By removing duplicate entries?
Use array_unique() with SORT_REGULAR flag.
$new_array = array_unique($array, SORT_REGULAR);
Output should be:
Array
(
[0] => Array
(
[title] => Zambezia
[id] => 31
)
[1] => Array
(
[title] => Zarafa
[id] => 34
)
)
Demo.
Okay here is my recursion algo:
public function getCategoryTree($tree,$return = array()) {
foreach ($tree->children as $child) {
if (count($child->children) > 0 )
$return[$tree->name] = $this->getCategoryTree($child, $return);
else
$return[] = $child->name;
}
return $return;
}
Here is a snippet of the data structure I'm trying to traverse
Object(stdClass)#290 (6) {
["category_id"]=>
int(1)
["parent_id"]=>
int(0)
["name"]=>
string(4) "Root"
["position"]=>
int(0)
["level"]=>
int(0)
["children"]=>
array(2) {
[0]=>
object(stdClass)#571 (7) {
["category_id"]=>
int(2)
["parent_id"]=>
int(1)
["name"]=>
string(18) "Root MySite.com"
["is_active"]=>
int(0)
["position"]=>
int(0)
["level"]=>
int(1)
["children"]=>
array(11) {
[0]=>
object(stdClass)#570 (7) {
["category_id"]=>
int(15)
["parent_id"]=>
int(2)
["name"]=>
string(9) "Widgets"
["is_active"]=>
int(1)
["position"]=>
int(68)
["level"]=>
int(2)
["children"]=>
array(19) {
[0]=>
object(stdClass)#566 (7) {
["category_id"]=>
int(24)
["parent_id"]=>
int(15)
["name"]=>
string(16) "Blue widgets"
["is_active"]=>
int(1)
["position"]=>
int(68)
["level"]=>
int(3)
["children"]=>
array(0) {
}
}
<snip....>
I'm trying to get a php data structure like such
categories = array( "Root" =>
array("Root MySite.com" =>
array( "Widgets" =>
// final element is NOT an array
array ("Blue Widgets", "Purple Widgets" ...)
)
)
)
I can't quite seem to get the data structure i'm looking for using my recursive algo. Any help
would be great.
Eventually I'll need to parse it again on the frontend and display it, but another problem for another day...
Have a look at this phpFiddle for a full working example. The only error I found was the $this->getCategoryTree which gave me an Fatal Error Using $this when not in object context. So are you sure the function is within the correct scope?
Updated
I hope this one works. :)
function traverse($root, $return = array()) {
$return[$root->name] = array();
foreach ($root->children as $child) {
if (count($child->children) > 0) {
traverse($child, &$return[$root->name]);
}else {
array_push(&$return[$root->name], $child->name);
}
}
return $return;
}
The output from this is:
Array ( [Root] =>
Array (
[Root MySite.com] =>
Array (
[Widgets] => Array ( [0] => Blue Widget [1] => Purple Widget)
[Gizmos] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos)
)
[FooBar.com] =>
Array (
[Widgets] => Array ( [0] => Blue Widget [1] => Purple Widget)
[Gizmos] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos)
)
)
)
Again, full working example