I have a routine in my code that computes the differences between two arrays in order create an SQL UPDATE statement.
As soon the routine starts I create a copy of the input array in order to manipulate it while the input one is kept untouched. When I'm ready to create the UPDATE statement I compute the difference between them, using the modified array as leading one.
In every test I ran both arrays were filled with key=value pairs and all values were strings, even when they're integers in the database (PDO behavior) and until now everything worked flawlessly.
But right now I've found something strange. Two different actions, in two different Controllers, but with the same logic are producing different differences:
This one works as expected:
$input = array(
'tid' => '3',
'enabled' => '1'
);
$modified = array(
'tid' => '3',
'enabled' => 0,
'modified' => '2014-11-26 15:17:55'
};
$diff = array(
'enabled' => 0,
'modified' => '2014-11-26 15:17:55'
);
$input is the result of a query. $modified is a copy of the first array manipulated through class methods. When I'm ready to create the statement, $diff is computed in order to send to PDO (or other driver) the correct statement.
Here, array_diff() works. the tid index is present in both array and it's ignored. The enabled, a simple on/off flag, is different and it's included. The datetime representation too.
But look the variables of this other case:
$input2 = array(
'sid' => '1',
'finished' => '0'
);
$modified2 = array(
'sid' => '1',
'finished' => 1,
'modified' => '2014-11-26 15:21:58'
);
$diff2 = array(
'modified' => '2014-11-26 15:21:58'
);
The same as before but with different field names. The sid is ignored but the finished is ignored too while it shouldn't because it is not present in the $input.
By replacing array_diff() with array_diff_assoc(), as expected, everything works, but why?
From the docs:
array array_diff ( array $array1 , array $array2 [, array $... ] )
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
In your example, $modified2 has an entry 'finished' which has value 1. But $input2 also has value 1 for key 'sid'. Thus, using array_diff($modified2, $input2) will result in the removal of every entry with value 1, no matter what the key is.
Using array_diff_assoc, it will only check to see if $input2 has the entry 'finished' => 1, which it does not, so the entry will not be removed.
Related
I have to pass a query parameters with different values but absolutely equal keys. I found this \GuzzleHttp\Psr7\build_query(); But it returns only last value. For instance:
$array = [
'test' => '1',
'test' => '2'
]
\GuzzleHttp\Psr7\build_query($array);
//OR
http_query_builder($array);
It returns every time string with the last item.
Does it possible to do that with PHP? Because the side which will take these parameters just can not do anything in their side so I have to pass parameters with the equal keys.
The problem was not with the specific method used, but with how you filled your array to begin with:
$array = [
'test' => '1',
'test' => '2'
]
You can not use the same array key twice; your array only contains one element now, because the second one has overwritten the existing first one under that same key.
Make the test element itself an array, that contains your two values:
$array = [
'test' => ['1', '2']
];
I am inserting items and attributes via SQL. Everything works fine, but i cannot figure it out how the _product_attributes in wp_postmeta
I understand the logic, except the s:7:"pa_hrup" <-- where do i get 7
and s:31:"pa_kapaciteta-rezervoarja-za-go" <-- where do i get 31..
a:2:{s:7:"pa_hrup";a:6:{s:4:"name";s:7:"pa_hrup";s:5:"value";s:0:"";s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:31:"pa_kapaciteta-rezervoarja-za-go";a:6:{s:4:"name";s:31:"pa_kapaciteta-rezervoarja-za-go";s:5:"value";s:0:"";s:8:"position";i:1;s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}}
It is a meta data string serialized array and s:31 is the length of pa_kapaciteta-rezervoarja-za-go in this case. a:6 is the number ok key/value pairs items for each array (or sub array).
So a seriallized array:
always start with a: for array, plus the number of items key/value pairs in it and :.
then { for items start
then s: for string, plus the lenght of the string item (key or value) and :, plus the key or value string.
then ; to separate each key or value component
then } for items end
Serialized arrays, can be unserialized using WordPress maybe_unserialize() (or unserialize() in PHP).
A normal array can be serialized using using WordPress maybe_serialize() (or serialize() in PHP).
Wordpress functions like add_post_meta() or update_post_meta() will always serialize an arrays before saving a meta_value in wp_postmeta table.
Same thing for WooCommerce with some related WC_Data method as save() on CRUD Objects and all related data stores classes.
using maybe_unserialize() on your serialized string array will give:
$values = array(
'pa_hrup' => array(
'name' => 'pa_hrup',
'value' => '',
'position' => '0',
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '1'
),
'pa_kapaciteta-rezervoarja-za-go' => array(
'name' => 'pa_kapaciteta-rezervoarja-za-go',
'value' => '',
'position' => '1',
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '1'
)
);
I'm currently attempting to return all records that match an array of conditions that I have. Currently I can get my code to work, but instead of returning all records that match an array of conditions that I've passed, it just returns the first one and then stops, instead of the four that I know exist in the table I'm accessing. This is with the all parameter set for find.
Here's the code snippet for a better view:
$array = implode(',', array('1','2','3','4'));
$a = $this->Assets->find('all', array(
'conditions' => array(
'id' => $array
)
)
);
var_dump($a);
var_dumping $a will just provide the record that has id 1, when there's records that exist for 2, 3, and 4 as well.
That is the expected result.
You are working against the ORMs auto-magic. Passing a string will result in an equality comparison, ie WHERE x = y, and since id is most probably an integer, the casting will turn the string 1,2,3,4 into 1, so ultimately the condition will be WHERE id = 1.
You should pass the array instead
'conditions' => array(
'id' => array(1, 2, 3, 4)
)
that way the ORM will generate an IN condition, ie WHERE id IN (1,2,3,4).
This is also documented in the cookbook:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
I'm trying to use array_merge_recursive to merge two data structures.
<?php
$testSite = array(
'name' => 'test site',
'modules' => array(
'foo' => 'true',
'bar' => 'true'
)
);
$testData = array(
'modules' => array(
'bar' => 'false'
)
);
$testSite = array_merge_recursive($testSite, $testData);
Note that I'm using strings instead of booleans for debug printing purposes
I would expect $testSite to be the exact same after this code has ran, except for the modules.bar property, which I'd expect to see being changed to false. What happens instead, as seen in this live example, is that bar is turned into an array containing it's old value and the value false is appended to that.
The documentation page reads that this is what will happen for numeric keys, but these are all strings keys. Can anyone shed some light on this?
I think you want array_replace_recursive.
array_merge_recursive() vs. array_replace_recursive()
Can an array key in PHP be a string with embedded zero-bytes?
I wanted to implode a multi-part key with embedded zero-bytes as the delimiter and use it as the key in an associative array, but it doesn't seem to be working. Not sure whether this is a problem with the array access or with array_keys_exists().
Does anybody have any idea? Am I going about this the wrong way? Should I be creating a multi-part key in another way?
To clarify, I am trying to eliminated duplicates from user-entered data. The data consists of a product ID, a variation ID, and N fields of textual data. Each of the N fields has a label and a value. To be considered a duplicate, everything must match exactly (product ID, variation ID, all the labels and all the values).
I thought that if a create a string key by concatenating the information with null bytes, I could keep an associative array to check for the presence of duplicates.
From the PHP string documentation:
There are no limitations on the values the string can be composed of;
in particular, bytes with value 0 (“NUL bytes”) are allowed anywhere
in the string (however, a few functions, said in this manual not to be
“binary safe”, may hand off the strings to libraries that ignore data
after a NUL byte.)
From the PHP arrays documentation:
A key may be either an integer or a string.
No mention is made of any special case for strings that are array keys.
So, yes.
Like I already said in the comments
print_r(array("\00foo\00bar" => 'works'));
works. However, there is no reason for any of the gymnastics you are doing with implode or serialize or null byte keys.
If you want to see whether arrays are identical, then you can just compare them:
$input1 = array('product_id' => 1, 'variation_id' => 2, 'foo' => 'bar');
$input2 = array('product_id' => 1, 'variation_id' => 2, 'foo' => 'bar');
var_dump($input1 === $input2);
will output true whereas
$input3 = array('product_id' => 1, 'variation_id' => 2, 'foo' => 'foobarbaz');
var_dump($input1 === $input3);
will give false.
Quoting the PHP Manual on Array Operators:
$a == $b Equality TRUE if $a and $b have the same key/value pairs.
$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
Also, PHP has a function for deleting duplicate values from arrays:
array_unique — Removes duplicate values from an array
And when you set the second argument to SORT_REGULAR, PHP will compare the arrays for equality, e.g.
$data = array(
array('product_id' => 1, 'variation_id' => 2, 'foo' => 'bar'),
array('product_id' => 1, 'variation_id' => 2, 'foo' => 'bar'),
array('product_id' => 2, 'variation_id' => 2, 'foo' => 'bar')
);
print_r(array_unique($data, SORT_REGULAR));
will reduce the array to only the first and the third element.