Sort multidimentional array of objects by value - php

I have an xml source that brings me some data each time. I parse the xml with php's simpleXML and the generated array looks like the one below. My problem is that i need to sort the array ascending based on the value that carries the time.
Array
(
[0] => SimpleXMLElement Object
(
[dict] => Array
(
[0] => SimpleXMLElement Object
(
[key] => Array
(
[0] => channel
[1] => title
[2] => time
)
[string] => Array
(
[0] => OTE SPORT 3
[1] => ΕΡΓΟΤΕΛΗΣ - ΤΡΙΚΑΛΑ
[2] => 20:00
[3] => Ποδόσφαιρο
)
)
[1] => SimpleXMLElement Object
(
[key] => Array
(
[0] => channel
[1] => title
[2] => time
)
[string] => Array
(
[0] => NOVASPORTS 1
[1] => ΠΛΑΤΑΝΙΑΣ - ΟΛΥΜΠΙΑΚΟΣ
[2] => 19:30
)
)
)
)
[1] => SimpleXMLElement Object
(
[dict] => Array
(
[0] => SimpleXMLElement Object
(
[key] => Array
(
[0] => channel
[1] => title
[2] => time
)
[string] => Array
(
[0] => NOVASPORTS 2
[1] => ΚΟΛΟΣΣΟΣ - ΟΛΥΜΠΙΑΚΟΣ
[2] => 16:00
[3] => Μπάσκετ
)
)
)
)
)

Related

Converting objects in multidimensional array to array

I am trying to get list of branches of the shipping company from their php API.The output from API I get is array which contains objects and I would like to convert these objects to arrays and save whole array to database. Example of the output is below. As I say I would like convert these objects into array and save it to database. Can you help me please? I tried some tips from stackoverflow but I have't fount anything yet. Thank you
Array
(
[email] => info#ulozenka.cz
[phone] => +420777208204
[hours] => UlozenkaLib\APIv3\Model\Branch\OpeningHours\OpeningHours Object
(
[regular:protected] => UlozenkaLib\APIv3\Model\Branch\OpeningHours\RegularOpeningHours Object
(
[monday:protected] => Array
(
[0] => UlozenkaLib\APIv3\Model\Branch\OpeningHours\Hours Object
(
[open:protected] => 10:00
[close:protected] => 16:00
)
)
)
[exceptions:protected] => Array
(
)
)
[links] => Array
(
[0] => UlozenkaLib\APIv3\Model\Link Object
(
[resourceName:protected] => website
[url:protected] => https://www.ulozenka.cz/pobocky/50020/jablonec-nad-nisou-machova-877-4-czc-cz
)
[1] => UlozenkaLib\APIv3\Model\Link Object
(
[resourceName:protected] => picture
[url:protected] => https://www.ulozenka.cz/cdn/images/branches/destination/50020.png
)
[2] => UlozenkaLib\APIv3\Model\Link Object
(
[resourceName:protected] => self
[url:protected] => https://api.ulozenka.cz/v3/branches/50020
)
)
)
So the converted array should be
Array
(
[email] => info#ulozenka.cz
[phone] => +420777208204
[hours] => array
(
[regular] => Array
(
[monday] => Array
(
[0] => Array
(
[open] => 10:00
[close] => 16:00
)
)
)
[exceptions] => Array()
)
[links] => Array
(
[0] => Array
(
[resourceName] => website
[url] => https://www.ulozenka.cz/pobocky/50020/jablonec-nad-nisou-machova-877-4-czc-cz
)
[1] => Array
(
[resourceName] => picture
[url] => https://www.ulozenka.cz/cdn/images/branches/destination/50020.png
)
[2] => Array
(
[resourceName] => self
[url] => https://api.ulozenka.cz/v3/branches/50020
)
)
)

SimpleXMLElement Object showing instead of default key

I'm attempting to build an array for CSV export and can't figure out why my array is building a sub array with just a string.
Any help would be appreciated.
PHP:
foreach($Entry as $list){
$pushArray = array($list['firstEntry'], $list['secondEntry']);
array_push($csv, $pushArray);
}
echo "<pre>";
print_r($csv);
echo "</pre>";
Output:
Array
(
[0] => Array
(
[0] => Title1
[1] => Title2
)
[1] => Array
(
[0] => SimpleXMLElement Object
(
[0] => Test Entry 1
)
[1] => SimpleXMLElement Object
(
[0] => Test Entry 1.1
)
)
[2] => Array
(
[0] => SimpleXMLElement Object
(
[0] => Test Entry 2
)
[1] => SimpleXMLElement Object
(
[0] => Test Entry 2.1
)
)
[3] => Array
(
[0] => SimpleXMLElement Object
(
[0] => Test Entry 3
)
[1] => SimpleXMLElement Object
(
[0] => Test Entry 3.1
)
)
)
I want it to look like this:
Array
(
[0] => Array
(
[0] => Title1
[1] => Title2
)
[1] => Array
(
[0] => Test Entry 1
[1] => Test Entry 1.1
)
[2] => Array
(
[0] => Test Entry 2
[1] => Test Entry 2.1
)
[3] => Array
(
[0] => Test Entry 3
[1] => Test Entry 3.1
)
)
$pushArray = array((string) $list['firstEntry'], (string) $list['secondEntry']);
You've to cast SimpleXML elements to string type if you're using it that way

Retrieve the value from the array and then add it to the new PHP

i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.

Loop through xml array

Below is my code and I want to extract information of[field] => Array of index [1].It means [1] => 1030.670044.
This is xml file,and I want to store in my database,but unable to loop this in php.
Please Help me.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[meta] => SimpleXMLElement Object
(
[type] => resource-list
)
[resources] => SimpleXMLElement Object
(
[#attributes] => Array
(
[start] => 0
[count] => 168
)
[resource] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[classname] => Quote
)
[field] => Array
(
[0] => USD/KRW
[1] => 1030.670044
[2] => KRW=X
[3] => 1398752590
[4] => currency
[5] => 2014-04-29T06:23:10+0000
[6] => 0
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[classname] => Quote
)
[field] => Array
(
[0] => SILVER 1 OZ 999 NY
[1] => 0.051059
[2] => XAG=X
[3] => 1398721323
[4] => currency
[5] => 2014-04-28T21:42:03+0000
[6] => 400
)
)
This is a SimpleXML object. If the variable is called $xml you can access the field[1] like this:
foreach ($xml->resources->resource as $res) {
var_dump( $res->field[1] );
}

XML xpath attribute value result to contain other results

I recently asked a question about how to select a parent's node attribute and its values at the same time using xpath, What i ended up with is :
Parent/#attr|Parent/x
It grab the parent attr's value and all of its x's nodes..
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
)
The thing now.. In case there are several Parent nodes, it will mix them together and i wouldn't know which attr belongs to its x's .. it will result something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
[5] => SimpleXMLElement Object
(
[0] => 1
)
[6] => SimpleXMLElement Object
(
[0] => 2
)
...
)
You see, they are all treated as a normal result, what i am trying to do is to make the attr contain all of it's results ( 1, 2, 3 ) in an array or something and the same thing goes to the other attr's to end up with something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
)
using the xpath and the way given at first

Categories