xpath get node from inside value - xpath

Current xpath: Product/ProductMultimediaObject/MultimediaObject
Returns:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ContentType] => application/pdf
[Date] => 2014-11-01 01:20:35
[Description] => Leaflet
)
)
)
But I'm currently just hoping that the found MultimediaObject is the correct one - which is not. I need to get the MultimediaObject where Description has a specific value.
What I've tried:
Product/ProductMultimediaObject/MultimediaObject[Description/text() = 'WhatIWant']
Product/ProductMultimediaObject/MultimediaObject/Description[text() = 'WhatIWant']

Your Desciption is an attribute. Try
Product/ProductMultimediaObject/MultimediaObject[#Description = 'WhatIWant']
or, for short,
//MultimediaObject[#Description = 'WhatIWant']

Related

laravel 5.4 validation array with keys

In laravel 5.4 when validation is failed I do like:
if ($validator->fails()) {
$errors_list = $validator->messages()->all();
and I got array like :
[errors_list] => Array
(
[0] => The image has already been taken.
[1] => The is main has already been taken.
)
What I dislike in this output that actuall name of error field is ommitted.
Code
echo '<pre>$validator->messages()::'.print_r($validator->messages(),true).'</pre>';
has output:
$validator->messages()::Illuminate\Support\MessageBag Object
(
[messages:protected] => Array
(
[image] => Array
(
[0] => The image has already been taken.
)
[is_main] => Array
(
[0] => The is main has already been taken.
)
)
[format:protected] => :message
)
And I did not find how access to messages data.
I would like to get array like:
[errors_list] => Array
(
[image] => The image has already been taken.
[is_main] => The is main has already been taken.
)
Is there is a way to make it ?
Thanks!
I hope this will work for you.
$errors->has('image');
$errors->get(image);
$errors->has('is_main');
$errors->get(is_main);
It's a tricky one. The reason it's like that is because there may be multiple validation errors happening at once. However if you only have a single rule per entry:
array_combine($validator->messages()->keys(),$validator->messages()->all())

laravel array_get first of multiple nested elements

i have an array, something like
[0] => Array
(
[id] => 1
[name] => Jimmy
[address] => Array
(
[number] => 1
[street] => Astreet
)
)
I need to access [street] using something like helper array_get dot notation:
array.address.street
However, as it can have multiple elements, I need something like
array*
where it can just get the first one.
Coming from cakephp they have a helper so i can do
array.{*}.address.street
Is there something similar in laravel, i cannot find such
If you want to get street from the first element you can do:
array_get(head($array), 'address.street');
// or
array_get($array, '0.address.street')
And if you want to get list of elements that contains street you can use array_pluck:
array_filter(array_pluck($array, 'address.street'));
$addresses = array_map(function($obj) {
if ( array_key_exists('address', $obj) )
if ( count($obj['address']) > 0 )
return $obj['address'][0];
}, $yourArray);

how to get the values of multidimensional array

how to get the values of multidimensional array.
code//
Array
(
[0] => Array
(
[City] => Array
(
[title] => Nagpur
[id] => 20299
)
[Branch] => Array
(
[0] => Array
(
[id] => 8
[country_id] => 41
[state_id] => 102
[city_id] => 20299
[title] => Geotech Services Ltd.
)
)
)
[1] => Array
(
[City] => Array
(
[title] => kolapur
[id] => 20300
)
)
)
Here I want to get the only values if cities(title) so plz tel me how to fetch in cakephp..
Thanks in advance
Thus you finding as recursive so all related data is fetched with City.
To disable this recursion, you need to set $recursive = -1;
You can do this by many ways--
place $this->City->recursive = -1; before find operation
place recursive => -1 on find overation
Globally set public $recursive = -1; on AppModel.php
And if you read from this array its simple and basic programming operation...
foreach($cities as $city){
echo $city['City']['title']
}
using cakePHP what you're trying to do is usually achieved using Hash
Assuming $your_array contains the data, then you have to do:
$cities = Hash::extract($your_array, '{n}.City.title');

Magento - get all attribute value

It is necessary for me to get a list of all the meanings an attribute of “color”. when i use this code
$name='color';
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem();
$attributeId = $attributeInfo->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions(false);
In that case I get that kind of list:
(
[0] => Array
(
[value] => 6
[label] => blueAdmin
)
[1] => Array
(
[value] => 5
[label] => coralAdmin
)
[2] => Array
(
[value] => 3
[label] => redAdmin
)
[3] => Array
(
[value] => 4
[label] => limeAdmin
)
)
It is the list of all meanings which are displayed in administration's part of website. How can I get a list of all meanings of attributes which are displayed in the shop not in administration's part of website?
Thank you.
You can get the attribute option values for a particular store by setting the store ID on the attribute prior to calling getAllOptions(), e.g.,
$attributeOptions = $attribute->setStoreId(1)->getSource()->getAllOptions(false);
gets the option values for the store with ID 1. You can get the ID of the current store with
Mage::app()->getStore()->getId();
So something like this should get you what you want:
$storeId = Mage::app()->getStore()->getId();
$attributeOptions = $attribute->setStoreId($storeId)->getSource()->getAllOptions(false);

Simplexml How to access all element of the same node

[Villa] => Array
(
[0] => SimpleXMLElement Object
(
[VillaID] => 6
[VillaName] => Mary
[Distances] => SimpleXMLElement Object
(
[Distance] => Array
(
[0] => SimpleXMLElement Object
(
[Destination] => Sea
[Value] => 1000 m
)
[1] => SimpleXMLElement Object
(
[Destination] => Market
[Value] => 800 m
)
)
)
)
[1] => SimpleXMLElement Object
(
[VillaID] => 21
[VillaName] => Marion
[Distances] => SimpleXMLElement Object
(
[Distance] => Array
(
[0] => SimpleXMLElement Object
(
[Destination] => Beach
[Value] => 5 min
)
)
)
)
)
I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach
I can obtain it with:
echo 'Name of Villa: '.$xml->Villa[0]->VillaName.'';
etc. etc ... but in this way have to change manually for every villa (too much) the value in the brackets.
i've tried with $xml->Villa[$value]->VillaName; ($value comes from another page) but it's not working...
Tanks for help!
First of all, your question starts with "i have this xml:" followed by something that is not XML. I'm not saying this to be a smartass, rather because it's important for XML beginners to understand that print_r() is not the right way to inspect SimpleXMLElements. Sometimes it will show you things that aren't in your XML, other times it will not show things that are actually in your XML. In short: do not use print_r() on SimpleXMLElement. Just use ->asXML() and look at the actual XML.
From what I understand, you want to locate and select a node based on some criteria. XML just happens to have a language for that: XPath. The official specs aren't terribly user-friendly but w3schools.com has a pretty good XPath tutorial.
I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach
Anywhere in your document, you want to select all Villa nodes with an attribute VillaID whose value is "6". In XPath:
//Villa[#VillaID="6"]
Via SimpleXML:
$xml->xpath('//Villa[#VillaID="6"]');
Attention, xpath() always return an array.

Resources