This is a result array from a php pdo query.
How can I get e.g. only the sitetitle which has langCode 'en'?
Array
( [0] => Array ( [id] => 3 [alias] => home [sitetitle] => LALALA [langCode] => de [langName] => Deutsch ) [1] => Array ( [id] => 3 [alias] => home [sitetitle] => LOLOLO [langCode] => en [langName] => English ) [2] => Array ( [id] => 3 [alias] => home [sitetitle] => LELELE [langCode] => es [langName] => Español )
)
This will iterate the array, find the first element with langCode =='en' and return the sitetitle of it. If no elements have langCode =='en', it will return an empty string.
function getEngSiteTitle($arr){
for each($arr as $v)
if($v['langCode']=='en')return($v['siteTitle']);
return('');
}
Related
I've that xml structure retrieving from device
<packet>
<info action="fiscalmemory" fiscalmemorysize="1048576" recordsize="464" fiscal="1" uniqueno="ABC12345678" nip="123-456-78-90" maxrecordscount="2144" recordscount="7" maxreportscount="1830" reportscount="4" resetmaxcount="200" resetcount="0" taxratesprglimit="30" taxratesprg="1" currencychangeprglimit="4" currencychangeprg="0" fiscalstartdate="dd-mm-yyyy hh:dd:ss" fiscalstopdate="dd-mm-yyyy hh:dd:ss" currencyname="PLN" />
<ptu name="A" bres="Nobi">123.23</ptu>
<ptu name="B">123.23</ptu>
<ptu name="D">8</ptu>
<sale>999.23</sale>
</packet>
simpleXml does't see ptu's attributes
$array = simplexml_load_string($xml);
print_r($array);
It prints
SimpleXMLElement Object
(
[info] => SimpleXMLElement Object
(
[#attributes] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[ptu] => Array
(
[0] => 123.23
[1] => 123.23
[2] => 8
)
[sale] => 999.23
)
As we can see ptu doesn't contain attributes :/
I also tried parse it with recursive function because children also may contain chilren but without success :/
Could anybody point to me why SimpleXML doesn't take ptu's attributes and also share any parsing function?
Thanks in advance.
edited
As regards Nigel Ren I made that function
function parseXMLtoArray($xml){
$x = simplexml_load_string($xml);
$result = [];
function parse($xml, &$res){
$res['name'] = $xml->getName();
$res['value'] = $xml->__toString();
foreach ($xml->attributes() as $k => $v){
$res['attr'][$k] = $v->__toString();
}
foreach($xml->children() as $child){
parse($child, $res['children'][]);
}
}
parse($x, $result);
return $result;
}
$resArray = parseXMLtoArray($rawXml);
print_r($resArray);
this returns such array
Array
(
[name] => packet
[value] =>
[attr] => Array
(
[crc] => BKJFKHKD54
)
[children] => Array
(
[0] => Array
(
[name] => info
[value] =>
[attr] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[1] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => B
)
)
[3] => Array
(
[name] => ptu
[value] => 8
[attr] => Array
(
[name] => D
)
)
[4] => Array
(
[name] => sale
[value] => 999.23
)
)
)
Is this correct?
Thanks again Nigel
When loading with SimpleXML, using print_r() gives only an idea of the content and doesn't have the full content. If you want to see the full content then use ->asXML()...
$array = simplexml_load_string($xml);
echo $array->asXML();
To check the attributes of <ptu> element, (this just gives the first one)...
echo $array->ptu[0]['name'];
This uses ->ptu to get the <ptu> element and takes the first one [0] and then takes the name attribute using ['name'].
I've managed to decode and echo a JSON feed. After running this command
print_r(json_decode($data,true));
this is what I see on the screen:
Array
(
[sportId] => 29
[last] => 96466864
[league] => Array
(
[0] => Array
(
[id] => 1980
[events] => Array
(
[0] => Array
(
[id] => 667177156
[starts] => 2016-11-26T15:00:00Z
[home] => Hull City
[away] => W.B.A
[rotNum] => 2504
[liveStatus] => 1
[status] => O
[parlayRestriction] => 2
)
[1] => Array
(
[id] => 672139467
[starts] => 2016-12-10T15:00:00Z
[home] => Hull City
[away] => Crystal Palace
[rotNum] => 2510
[liveStatus] => 1
[status] => O
[parlayRestriction] => 2
)
[2] => Array
(
[id] => 676973849
[starts] => 2016-12-26T15:00:00Z
[home] => Burnley
[away] => Middlesbrough
[rotNum] => 2519
[liveStatus] => 1
[status] => O
[parlayRestriction] => 2
)
)
)
)
)
I need to be able to use foreach to go through each [events] in this associative array, and to be able to get the result such as this:
Hull City v W.B.A.
Hull City v Crystal Palace
Burnley v Middlesbrough
I think everything is already parsed correctly and now it's just a matter of using the correct syntax to echo the result from the associative array, which I cannot do myself.
You can try this:
$data=json_decode($data,true);//converts in array
foreach($data['league'] as $key=>$val){// this can be ommited if only 0 index is there after
//league and $data['league'][0]['events'] can be used in below foreach instead of $val['events'].
foreach($val['events'] as $keys=>$value){
echo $value['home'].' v '.$value['away'].'<br>;
}
}
Try like this..
$data=json_decode($data,true);//convert your json into array
$events = $data['leage'][0]['events'];//events array
foreach($events as $key=>$value)//loop inside your events array
{
echo $value['home'].' v '.$value['away'].'<br>;
}
I am using php with codeigniter. When I did the print_r, I got the following:
Array
(
[0] => stdClass Object
(
[UserName] => fun
[UserCode] => 6
[GroupCode] => 1
[GroupName] => Master
[MenuCode] => 0
[MenuName] =>
[Admin] => 0
)
[1] => stdClass Object
(
[UserName] => fun
[UserCode] => 6
[GroupCode] => 1
[GroupName] => Master
[MenuCode] => 1
[MenuName] => Item Master
[Admin] => 0
)
)
In another hands I have another set of array,
Array
(
[0] => Billing Report
[1] => Waiterwise Report
[2] => Admin
[3] => User Master
[4] => User Rights
[5] => Close Session
[6] => Close Day
)
Now i want to compare Group name in the above two array and want the key as result.
Try this
$result_array = array();
foreach($codeigniter_array as $k=>$r)
{
foreach($r as $k1=>$r1)
{
if(in_array($r1, $group_array))
{
$result_array[$k1] =$r1;
}
}
}
print_r($result_array);die;
I am using the following cakephp query to retrieve data from mysql:
$tops = $this->PageBanner->find('all', array(
'conditions' => array(
'PageBanner.status' => 1
),
'fields' => array(
'PageBanner.page_url',
'PageBanner.image',
'PageBanner.logo',
'PageBanner.logo_text',
'PageBanner.content'
)
));
This query returns me the following results:
[0] => Array
(
[PageBanner] => Array
(
[page_url] => index
[image] => home_banner.png
[logo] => home_logo.png
[logo_text] => abc
[content] => abc.
)
)
[1] => Array
(
[PageBanner] => Array
(
[page_url] => write_review
[image] => kids2.png
[logo] => home_logo.png
[logo_text] => abc
[content] => abc.
)
)
But I want the data to be returned in the following format:
[index] => Array
(
[page_url] => index
[image] => home_banner.png
[logo] => home_logo.png
[logo_text] => abc
[content] => abc.
)
[write_review] => Array
(
[page_url] => write_review
[image] => kids2.png
[logo] => home_logo.png
[logo_text] => abc
[content] => abc.
)
I need page_url field content in place of Array index (e.i. 0, 1). Is that possible to get data in this format or I need to manually configure the arrays?
$result = Set::combine($tops, '{n}.PageBanner.page_url', '{n}.PageBanner');
pr($result);
I have managed to get to the stage where I have an array that looks like this. Used (zend_json to decode a json response)
Array
(
[response] => Array
(
[status] => ok
[userTier] => free
[total] => 10
[startIndex] => 1
[pageSize] => 10
[currentPage] => 1
[pages] => 1
[results] => Array
(
[0] => Array
(
[id] => lifeandstyle/series/cycling
[type] => series
[webTitle] => Cycling
[webUrl] => http://www.guardian.co.uk/lifeandstyle/series/cycling
[apiUrl] => http://content.guardianapis.com/lifeandstyle/series/cycling
[sectionId] => lifeandstyle
[sectionName] => Life and style
)
[1] => Array
(
[id] => sport/cycling
[type] => keyword
[webTitle] => Cycling
[webUrl] => http://www.guardian.co.uk/sport/cycling
[apiUrl] => http://content.guardianapis.com/sport/cycling
[sectionId] => sport
[sectionName] => Sport
)
How would I go about parsing only the elements that are [webTitle] and [webUrl]
Thanks!
You can't specifically parse only those parts, but you can iterate over the results and access them.
foreach ($val['response']['results'] as $result) {
$title = $result['webTitle'];
$url = $result['webUrl'];
// ...
}