I've got a bit of a complicated array that I'm working with and I'm not sure how to retrieve one of the values in the array. The array structure is as below and I'm trying to get the 'value'.
array (
0 => Drupal\search_api\Query\Condition::__set_state(
array(
'field' => 'title',
'value' => 'hello',
'operator' => 'CONTAINS',
)
),
)
I tried using $array[0]['value'] but it returns NULL.
Any ideas on how to go about this one?
The Drupal\search_api\Query\Condition::__set_state there is how var_export tells you that there is an object in $array[0]. The array shown inside that is a view of the internal data of that object, which may not be directly accessible at all.
You need to find the documentation or source code for the Drupal\search_api\Query\Condition class and see how it provides access to its data. For instance, the access might look like this (it probably doesn't, this is an example not a suggestion):
echo $array[0]->tellMeTheValue();
Related
I am working with multidimensional, associative arrays a lot, mainly for configuration data and introduced "breadcrumbs" into the use of multidimensional arrays.
The basic idea: my classes are designed to handle one-dimensional (flat) breadcrumb-arrays as a reference-guide for certain values inside a multidimensional, associative array. Every incremented index of a breadcrumb is basically a level deeper inside the array until the last level and associative key are found and reached in recursion. E.g.:
$myArray = array(
'firstLevel' => array(
'secondLevel' => array(
'myValue' => 42
),
'anotherLevel' => array(
'anotherValue' => 13
)
)
);
$myBreadcrumb = array('firstLevel', 'secondLevel', 'myValue');
$fancyClass = new \someNamespace\fancyArrayProcessingClass($myArray);
$myValue = $fancyClass->getValueForBreadcrumb($myBreadcrumb);
If it's requested i'll post an example for the processing of the breadcrumbs, too, but since i'm targeting on custom data types i found it being off-topic.
It is getting tiresome and is overhead to always code "Wrapper" classes, implemented classes or another kind of construct to make arrays navigable via breadcrumb. I wondered if there is a way to introduce real new DataTypes into PHP that can be handled like actual DataTypes. My idea of a good syntax for this concept:
$myArray = navigableArray(
'firstLevel' => array(
'secondLevel' => array(
'myValue' => 42
),
'anotherLevel' => array(
'anotherValue' => 13
)
)
);
$myBreadcrumb = array('firstLevel', 'secondLevel', 'myValue');
$myValue = $myArray[$myBreadcrumb];
Or even more intuitive to use with just xpath-style strings:
$myArray = navigableArray(
'firstLevel' => array(
'secondLevel' => array(
'myValue' => 42
),
'anotherLevel' => array(
'anotherValue' => 13
)
)
);
$myValue = $myArray['firstLevel/secondLevel/myValue'];
I know that there is a sentence in the PHP documentation that says something like "developers will never need to introduce their own DataTypes into PHP", but AFAIK there is no reason given why it is like that and why a developer is - unlike with almost every other language - unable to introduce fully custom DataTypes.
Edit:
For anyone curious: i found an alternative route, with the standard php class "ArrayAccess" you can make your PHP Object behave like an actual array. The famous "Judy" class incorporates "ArrayAccess" and "Iterator" and fits exactly what i was looking for in this question-thread.
http://php.net/manual/de/class.arrayaccess.php
http://php.net/manual/de/class.judy.php
Based on your use-case, I'd recommend you look into PHP's SPL Iterators. Specifically the Recursive Iterator interface or the RecursiveArrayIterator implementation. These Iterators are built natively into PHP, are very fast, and many allow you to access them using native PHP functions such as foreach(), for(), count(), etc.
You can then extend one of the SPL Iterator interfaces to create your own custom class that gives you functionality specific to your Breadcrumb needs. FWIW, I suppose it's true that developers should not have to create their own native data types in PHP (because you can't) but creating custom types with PHP classes is highly encouraged over using native arrays.
I am currently trying to figure out how to save a indexed array to a field in my database. That said, I know an array can't be saved to a database, but you can serialize it or implode it and then save. Im not sure which one I should be using though. I don't want a collection of items to be saved in just one cell. I need the list of items to be saved one by one in the column. So my question is do I need to be using the serialize method, implode or something else? Here is a glimpse of my code and the array I am trying to save.
public function findPolicyIds($coverageId = null) {
$policyid = $this->Policy->find('all', array(
'recursive' => -1,
'conditions' => array('Policy.coverage_id' => $coverageId),
'fields' => array('Policy.id')));
foreach($policyid as $id) {
$all[] = $id['Policy']['id'];
}
return $all;
}
Array
(
[0] => 5202834f-111c-4a76-8b33-1ed8ae78509d
[1] => 5202834f-2ba8-4957-91db-1ed8ae78509d
[2] => 5202834f-356c-49a1-beeb-1ed8ae78509d
[3] => 5202834f-3b40-453f-a491-1ed8ae78509d
It depends.
This is probably the best answer to give you.
Do whatever you like, as long as it doesn't magically corrupt the data between write and read.
Lemme take a moment to explore a few options on your behalf.
var_export() - might not be the best idea, securitywise
serialize() - seems straightforward
implode() - seems straightforward
json_encode() - seems straightforward
There are probably other, more obscure, options available. You could even build up complex data sets with XML, if you like.
Alternatively, why not normalize the schema and add a new table to present that collections of array-data ? Normalization tends to save your bacon in the future.
This might be a simple question, but I am dumping an object ($this) which is absolutely huge and I need to get to a specific point in the array
$this->varA->varB->varC->varD->what_I_need
I know that the variable that I need is in there and I can use ctrl+f to find it, but the array is so nested that I don't know how I should get to it in PHP. Any ideas on what the best way is to do this?
Do not hesitate to look at libraries from frameworks.
[CakePHP] made an awesome class which is able to navigate into arrays using a string in dot syntax notation. This library is known as Hash, just look at it.
If you have this :
$var = array(
'Element1' => array(
'First_rule' => true,
'Second_rule' => false,
),
'Element2' => array(
'First_rule' => 'none',
'Other_rule' => 'otherone',
),
);
You can extract datas from this array simply with a string.
You can take only one information from a specific element :
$extracted_other_rule = Hash::extract($var, 'Element2.Other_rule');
Returns :
Array
(
[0] => otherone
)
Or you can even extract all "First_rule" indexes from any element in the array containing it :
$extracted_rules = Hash::extract($var, '{s}.First_rule');
Returns this :
Array
(
[0] => 1
[1] => none
)
If you need to navigate through a huge array with undefined depth, just make a recursive function, transferring a string named "$path" to recursive actions. I made a big function of this kind to parse a whole XML stream into a JSON string with all keys parsed with my own rules, with an array of parameters.
Ok, I have a multidimensional array that is populated dynamically via an API. Users can enter in arrays of information that are transformed into a multidimensional array, which would look something like this:
$object = array(
array(
'key1' => 'value 1',
'key2' => 'value2'
),
array(
'key1' => 'value1',
'key2' => 'value2'
)
);
Next, I've been running a foreach loop to grab all the information I need, and everything works great. But here's the issue I'm running into - each array on information will output a block of info within a tag. If 5 arrays are inputted, that's way too many blocks of information. Each array has many different if/else checks which is what is throwing me off. I can't simply break after the first foreach loop because I need to grab all the information from each array.
Basically what I am asking is how can I loop through an entire multidimensional array and only output one bit of information?
I hope what I am asking makes sense. If not, let me know and I will try to explain more. This is more of a conceptual question for me.
If I am going to use the code:
extract(
array(
'key.name' => 'value',
'somekey' => 'somevalue'
)
)
Is there some way for me to retrieve the first value? E.g. ${'key.name'} or similar.
I know I can retrieve the second value with $somekey but I am curious if its possible with . in the name.
http://nl.php.net/manual/en/function.extract.php
Checks each key to see whether it has a valid variable name.
Nope, it won't extract 'key.name'. You can inspect the $GLOBALS array to see it isn't there.
The answer is no. Give this post a read.