So I have two arrays which looks like this when I do a var_dump :
array(4) {
["DatabinFieldName_1"]=> string(7) "Heading"
["DatabinFieldType_1"]=> string(13) "VARCHAR (255)"
["DatabinFieldName_3"]=> string(11) "DateCreated"
["DatabinFieldType_3"]=> string(8) "DATETIME"
}
array(8) {
["DatabinFieldName_1"]=> string(7) "Heading"
["DatabinFieldType_1"]=> string(13) "VARCHAR (255)"
["DatabinFieldName_2"]=> string(4) "Copy"
["DatabinFieldType_2"]=> string(4) "TEXT"
["DatabinFieldName_3"]=> string(11) "DateCreated"
["DatabinFieldType_3"]=> string(8) "DATETIME"
["DatabinFieldName_4"]=> string(8) "Comments"
["DatabinFieldType_4"]=> string(4) "TEXT"
}
I need to get the difference in a result. Which I have tried using this code.
// Get POST Array
$databinPostArray = $_POST;
// Get Databin Array
$databinObject =json_decode($nbase->getwhere("Databins","ID='".$databinID."' LIMIT 1;",$_SESSION["UserDB"]));
$databinArray= unserialize($databinObject[0]->DatabinArray);
var_dump($databinPostArray);
var_dump($databinArray);
$result = array_diff($databinPostArray, $databinArray);
print_r($result);
Problem is I keep getting Array() back which means its not finding any differences even though there is.
array_diff() returns the elements of the second array which are not in the first one.So the answer to your question is:
$result = array_diff($databinPostArray, $databinArray);
if (couunt($result) == 0) {
$result = array_diff($databinArray, $databinPostArray);
}
This way, the difference will be returned, whether there is more keys in $databinPostArray or in $databinArray.
If what you want is only to check which elements are in $databinArray, but not in $databinPostArray, please do:
$result = array_diff($databinArray, $databinPostArray);
You need to reverse the arguments:
$result = array_diff($databinArray, $databinPostArray);
array_diff returns an array with everything in the first array that isn't in the second array.
If you want to get all the elements that are unique to either array, you can use:
$result = array_diff(array_unique(array_merge($databinArray, $databinPostArray)),
array_intersect($databinArray, $databinPostArray));
Related
i need to unserialize a string to an array.Here is the stringwhich needs to be unserialized in php to assosiative array.
a:1:{i:0;s:158:"a:6:{s:5:"rowid";s:32:"94ca9ee0c4e3184b50e89e82f80332fb";s:2:"id";
s:2:"68";s:3:"qty";s:1:"1";s:5:"price";
s:2:"20";s:4:"name";
s:5:"Bread";s:8:"subtotal";i:20;}";
}
It looks like this has been doubly serialized. It expands to an array with a single element, and that element is a serialized associative array. So you need to do:
$temp = unserialize($data);
$result = unserialize($temp[0]);
var_dump($result);
Result:
array(6) {
["rowid"]=>
string(32) "94ca9ee0c4e3184b50e89e82f80332fb"
["id"]=>
string(2) "68"
["qty"]=>
string(1) "1"
["price"]=>
string(2) "20"
["name"]=>
string(5) "Bread"
["subtotal"]=>
int(20)
}
If there can be more that one element in the top-level serialized array, use array_map to unserialize all of them:
$result = array_map('unserialize', $temp);
$result will now be a 2-dimensional array.
I'm not sure why you stored your data this way. Why not just serialize the original 2-d array all at once, instead of nesting them?
I have a nested array of arrays, and I want to shuffle the inner arrays. My code looks like this (simplified):
$a = array(array('banana', 'peach'), array('ding', 'dong'), array('oh snow'));
foreach ($a as &$arr) {
shuffle($arr);
}
var_dump($a);
The var_dump outputs this:
array(3) { [0]=> array(2) { [0]=> string(5) "peach" [1]=> string(6) "banana" } [1]=> array(2) { [0]=> string(4) "ding" [1]=> string(4) "dong" } [2]=> &array(1) { [0]=> string(7) "oh snow" } }
As you can see in the output, the first two subarrays work, but the third subarray is linked by reference in the output...
In my full app, this last array-link causes problems, but rather than working around the issue, I want to fix this shuffle thing...
Cheers!
This has to do with how PHP stores references to array elements. It cannot reference an element of an array, only values. Therefore it has to store the value array('oh snow') in a "slot" of the symbol table, then make $arr and $a[2] a reference to that value.
To fix this, unset($arr) after the loop. That way only a single variable is referencing the value, which will then be made a regular array index again. Unsetting references after a foreach is good practice anyway, since there are many such gotchas.
I'm trying to make advanced search filters in an application that holds resources (people). I've got all the results in 1 multidimensional array. A user of the application can search for the persons Job title, skills, work field and country.
I've already made the part where I look up the people that meet the criteria given by the user. These results are stored in a multidimensional array. If the user is looking for someone with a specific resource with a job title and a specific skill the return value is this:
$realfilters = array(2) {
["resourcesWithJobtitle"]=> array(6) {
[0]=> string(1) "1"
[1]=> string(2) "48"
[2]=> string(2) "88"
}
["resourcesWithSkill"]=> array(9) {
[0]=> string(1) "4"
[1]=> string(1) "8"
[2]=> string(1) "48"
[3]=> string(2) "50"
}
When the user also looks for a work field this is added to the result:
["resourcesWithWorkfield"]=> array(3) {
[0]=> string(2) "48"
[1]=> string(2) "96"
[2]=> string(2) "97"
}
I need to know which resources meet all dimensions of the array so I can display them. (So in this example I need an array with just 1 value: 48). I think I need to use array_intersect but can't seem to get it right.
One of the possible solutions: you may first extract() the $realfilters array values to variables, and then apply the array_intersect() to them. But this solution is applicable only if there are not many possible filters.
Another one and probably the best solution would be to intersect in a loop, something like:
$res_arr = array_shift($realfilters);
foreach($realfilters as $filter){
$res_arr = array_intersect($res_arr, $filter);
}
$intersection = call_user_func_array('array_intersect', $array);
That will give you the elements present in all the sub arrays of $array.
edit-
This above is like a shortcut for writing:
$intersection = array_intersect($array['a'], $array['b'], ...and so on for all elements...);
A loop could be used as well
$intersection = reset($array);
foreach ($array as $subarr) {
$intersection = array_intersect($intersection, $subarr);
}
print_r($intersection);
Im trying to search a array and navigate to the next and previous values
$ids=$res->result_array();
returns
array(3) {
[0]=>
array(1) {
["qid"]=>
string(5) "63697"
}
[1]=>
array(1) {
["qid"]=>
string(5) "63706"
}
[2]=>
array(1) {
["qid"]=>
string(5) "63709"
}
}
but when i try to search for the index it returns false
$curr_index = array_search($this->uri->segment(4), $q);
returns
bool(false)
$this->uri->segment(4) is the qid.
i want to navigate with the array by increasing and decreasing by one so i can get the next and previous values.
can someone please tell what am i doing wrong here?
You have an array of arrays, you could search it like this:
$curr_index = array_search(array('qid' => $this->uri->segment(4)), $q);
Where you are actually searching for an array instead of a string.
Working example: http://codepad.viper-7.com/Ff0sAq
array(14) {
[0]=>
string(1) "1"
["id"]=>
string(1) "1"
[1]=>
string(7) "myUserName"
["UserID"]=>
string(7) "myUserName"
[2]=>
string(10) "myPassword"
["passwordID"]=>
string(10) "myPassword"
[3]=>
string(24) "myEmail#domain.com"
["emailAddress"]=>
string(24) "myEmail#domain.com"
[4]=>
string(7) "myFirstName"
["firstName"]=>
string(7) "myFirstName"
[5]=>
string(8) "myLastName"
["lastName"]=>
string(8) "myLastName"
[6]=>
string(1) "1"
["active"]=>
string(1) "1"
}
how do i access the contents of this array using PHP?
the above was a var_dump($info)
It depends on which part of the array you are trying to access.
If you are trying to access a specific item, you can access it by its index ; for instance :
echo $info['passwordID'];
Should give you :
myPassword
(edit after the comment)
For the email address, there is this portion in your var_dump's output :
["emailAddress"]=>
string(24) "myEmail#domain.com"
This indicates that the e-mail address is stored in the array as an element that has the key "emailAddress".
Which means you should be able to get that e-mail address like this :
echo $info['emailAddress'];
And as you also have this portion of text in the var_dump's output :
(About that duplication of data, you should read Pekka's answer, who provides an idea of why your data is in your array twice, with both integers and strings as keys)
[3]=>
string(24) "myEmail#domain.com"
You could also use :
echo $info[3];
(of course, in each of those cases, you could also store this to a variable for futures re-use)
Another solution, if you want to access each item, would be to use some foreach loop ; for instance :
foreach ($info as $key => value) {
echo "Value for key $key is $value <br />";
}
You might want to go through the arrays section of the PHP manual, for more informations.
And, also, the section about array functions.
You can use either the numeric or the associative key:
echo $array[0]; // outputs 1
echo $array["id"]; // outputs 1
I'm guessing this is the result of a mysql_fetch_array() operation, isn't it? You may want to specify whether you want a numeric or associative array using the second parameter to that function.
Example:
$record = mysql_fetch_array($query, MYSQL_ASSOC); // for associative keys only
$record = mysql_fetch_array($query, MYSQL_NUM); // for numeric keys only
The array appears to have both string and numeric keys. You can access the fields using the array index operator []. Supply either the numeric key or the column name:
echo $info['UserID']; // output "myUserName"
echo $info['emailAddress']; // output "myEmail#domain.com"
if $info is the array, then you can echo $info[6] for example.
If you want it as a string then $s=print_r($info,true);
It seems you are doing some thing wrong, as there shouldn't be a need to access array like that, and there other ways to access objects as arrays.