Why does add_post_meta create a nested array? - php

I can't figure out why that when trying to add a custom post meta value from a set of checkboxes, by default a nested array set is being created, where I would just have a single array of the values I've saved.
So when I perform add_post_meta($post_id, 'Procedure', $_POST['bv_procedures']); and then do a var_dump of the value, I am returned with this:
array (size=1)
0 =>
array (size=2)
0 => string '13419' (length=5)
1 => string '13416' (length=5)
Why is the first index there, and how can I just have a single array containing the two values?
-- UPDATE --
I have since noticed somewhat strange behaviour with this now:
var_dump(get_post_meta($post->ID, 'Procedure', true)); will result in this being displayed on the screen when editing this post:
array (size=1)
0 => string '13419' (length=5)
However, I'm looking in the 'Custom Fields' section, and Procedure is not listed in there at all, whilst other post meta pairs are. So I have no idea what's going on here, why is this not being listed? And also, I don't understand why stating get_post_meta to return a string value, returns an array (albeit not nested). Surely, this is a bug?

Related

Array to String conversion while login

I've tried to update my site from SF 3.0 to SF 3.1. Since then, this is impossible to login correctly.
"Notice: Array to string conversion in
vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php
at line 377"
I checked that file, and it appears that $controller is an array instead of a string. There is the dump of the array :
array (size=2)
0 => string 'Vehixel\UserBundle\Controller\SecurityController' (length=48)
1 => string 'login_checkAction' (length=17)
I have no idea of what could cause the issue. The login function was fully working before.
Thanks to #symfony IRC channel, especially to "bloodline". It seems to be a regression. We corrected it by adding the loginCheckAction & the logoutAction in the controller, even empty.

Target array element that has a blank key/name

I have an array being generated by data which I have no control over and it returns the below array. Is there a way to target specifically the element that has a blank key/name?
I've tried $array[''], $array[' '] and $array[0] but none of them seem to work. Is there another way without looping through the array, the empty key/name can be anywhere in the array, it's not necessarily the last array element?
var_dump(array_keys($array)); gives me [0]=> string(2) " " for the element in question, I've tried targeting the element with a single and double space as the key/name but still gives an undefined index error.
I've also run serialize()on the keys from the array and the element with the blank key/name is returning s:2:" ";.
Array
Array
(
[desc] => Information etc.
[ref] => 2
[ ] => 123
)
It i probably not '' but something that shows up as '' (null maybe ?). Try var_dump(array_keys($array)) to get actual key values and types. this should show you the proper key to use

Accessing a 3d array without using foreach loop

Let's say I have a 3D array and I don't want to access it through the key names.
Is there a way to access to the value (2) without having to use a foreach loop?
array (size=1)
'type' =>
array (size=1)
'registered' => string '2' (length=1)
It is not possible without a loop.
If you don't want to use keys and all that matters is the position within the array then use a numerical array.
If you really want to use an associative array and you need to find the key at a specified index, then at some point you will need to loop through your array.
You could try serializing your array and working with the serialized structure. Not saying this is good or even viable, but it might do what you want.
You can find out more about serialize here:
http://php.net/manual/en/function.serialize.php

Failing preg_match pattern over entirely valid value

I used regexpal.com to test my regexp against the data Wordpress is trying to compare to and it fails, look at this and tell me if you see the problem?
The regexp
"#^json/(.+?)/?([a-zA-Z0-9]*)?$#"
The content to match
json/trips
These works, the previous one doesn't
json/trips/0
json/trips/13
json/fullticket/9805048001130122361809
If I try all these in regexpal they all work, but in wordpress, only the one that doesn't contain the id of the element I want to fetch fails the others work fine.
Interrestingly enough, the $matches return this:
array
0 => string 'json/trips' (length=10)
1 => string 't' (length=1)
2 => string 'rips' (length=4)
Try this regexp instead :
#^json/([^/]+)/?([a-zA-Z0-9]*)?$#
Output :
Array
(
[0] => json/trips
[1] => trips
[2] =>
)
The answer after tweaking the wordpress rewrite rule a bit more ends up being:
data/([^/]+)(/([a-zA-Z0-9\-]*))?$
Note: i changed json to data in the new scenario so i don't mess up the custom post type rules

Better data structure or algorithm than iterative searching on multidimensional arrays to find corresponding values?

For a site I am working on I use a library to get a list of states. It returns a numerically indexed array of states, each with three keys: stateCode, stateName, and stateSeg. It looks like this:
array
0 => &
array
'stateCode' => string 'AL' (length=2)
'stateName' => string 'Alabama' (length=7)
'stateSeg' => string 'alabama-al' (length=10)
1 => &
array
'stateCode' => string 'AK' (length=2)
'stateName' => string 'Alaska' (length=6)
'stateSeg' => string 'alaska-ak' (length=9)
2 => &
array
'stateCode' => string 'AZ' (length=2)
'stateName' => string 'Arizona' (length=7)
'stateSeg' => string 'arizona-az' (length=10)
I often find myself with one of the three values and needing to look up its corresponding value. To do this I find myself constantly having to iterate through the array of states to find the data I need. Like this:
foreach ($this->data['stateList'] as $state)
{
if ($state['stateCode'] == $searchParams['state'])
{
$stateSeg = $state['stateSeg'];
break;
}
}
$url = BASEURL . '/' . $stateSeg . ".html";
This seems inefficient to me. I think the most efficient solution I’ve been able to come up with is to turn states into objects and put them in array with multiple keys for stateCode, stateSeg, and stateName each pointing to the same state object, so they can be referenced like this:
stateList[‘CA’]->getStateSeg();
or
stateList[‘Arizona’]->getStateCode();
or
stateList[‘alaska-ak’]->getStateName();
etc…
This also seems like kind of a hack which would result in a rather large array (150 keys pointing to 50 objects) with replicated data (keys replicating data stored within objects).
Anyway, just thought I’d see if there is some kind of pattern for this type of problem. This array of states isn't the only thing I’ve come across where I’ve had to do this sort of iterative searching on multidimensional arrays to find corresponding values.
Question is tagged PHP and the code above is in PHP, but I am interested in elegant solutions in any language.
If php supports references and I know the state, I'd just pass a reference to the appropriate array element and extract from it the necessary field.
Alternatively, if you never know in advance what state you can get, create and use a map (associative container/array), let its efficient implementation take care of quickly finding whatever you need. Seems like you may need several of them.
Also, I wonder if you could get rid of everything except the "alaska-ak" strings. The data appears highly redundant.
I think your basic idea with the object and the arrays is not that bad, but instead of creating actually objects, I would just refer to the existing objects (better: array data). Let's see your original list again:
array
0 => &
array
'stateCode' => string 'AL' (length=2)
'stateName' => string 'Alabama' (length=7)
'stateSeg' => string 'alabama-al' (length=10)
1 => &
array
'stateCode' => string 'AK' (length=2)
'stateName' => string 'Alaska' (length=6)
'stateSeg' => string 'alaska-ak' (length=9)
2 => &
...
Each state object has an identifier, the array key: 0, 1, 2, ... .
All you need to do is to create three indexes based on key. You use the value as key (e.g. "AL" for "stateCode" index) and as value you take the array index, 0:
$indexStateCode['AL'] = 0;
You can then already look this up quickly:
$states[$indexStateCode['AL']];
Encapsulate this into a class with ArrayAccess and then on request instantiate the state object. You don't need it earlier.
Could you store the states in a mysql/sqlite table and use the database engine to do the lookup?
This seems inefficient to me
It isn't. Even worse-case, iterating through 50 items is probably an order of magnitude faster than querying a db.
a library to get a list of states
Not sure why you'd need a library to do this. But I'd either change the library to return the array how you need it, or wrap it in another module.
The data is somewhat redundant... All you need is two items: the state code and the state name. You can construct the "state seg" from those two. So keep a state code map and a state name map.

Categories