I have two arrays, I would like to compare.
array1:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "dog"
["name"]=>
string(4) "alan"
}
["order"]=>
string(2) "12"
}
array2:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "fox"
["name"]=>
string(4) "tom"
}
["order"]=>
string(2) "12"
}
I compare them with array_diff:
$result = array_diff($array1, $array2);
But if I var_dump $result, I get the following output:
array(0) {
}
Does anyone have an idea why?
For associative arrays you should use array_diff_assoc. Also see the user contributed notes for how to do this recursively, if you need to.
With the help of sinaza I found out that no difference was displayed, because array_diff works different with multidimensional arrays.
Here is the code, that worked for me:
foreach ($array1 as $k1 => $v1) {
if (array_diff($array2[$k1], $array1[$k1])){
$result[$k1] = array_diff($array2[$k1], $array1[$k1]);
}
}
Related
I'm trying to loop through a multidimensional array with foreach but sometimes there's 5 dimensions and sometimes there's 2, but I need to foreach every array. Here is an example:
array(16) {
["id"]=>
string(2) "1"
["name"]=>
string(1) "Bob"
["job"]=>
array(2) {
[0]=>
string(8) "software"
[1]=>
string(7) "plumber"
}
["kids"]=>
array(2) {
[1]=>
array(2) {
[0]=>
string(4) "Jane"
[1]=>
string(4) "girl"
}
[2]=>
array(2) {
[0]=>
string(3) "Sam"
[1]=>
string(4) "boy"
[2] => array(2) {
[0]=>
string(3) "123"
[1]=>
string(11) "Main Street"
}
}
}
}
you get the point.... but imagine if I had a dimension of 10 in the array. How can I dynamically loop through them and do trim() to each value in the whole array?
Here is what I have so far:
foreach ($array as $key => $value) {
$array[$key] = trim($value);
}
but I need it to go deeper into an array if there is an array and perform trim to all values in my $array.
I have these 2 arrays $fonts['google'] and $data['value'] with the following content:
var_dump ($fonts['google']) outputs
array(4) {
[0]=> array(3) { ["family"]=> string(7) "ABeeZee" ["variants"]=> array(2) { [0]=> string(7) "regular" [1]=> string(6) "italic" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
[1]=> array(3) { ["family"]=> string(4) "Abel" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
[2]=> array(3) { ["family"]=> string(13) "Abril Fatface" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(2) { [0]=> string(5) "latin" [1]=> string(9) "latin-ext" } }
[3]=> array(3) { ["family"]=> string(8) "Aclonica" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
}
var_dump ($data['value']) outputs
array(4) {
["size"]=> int(17)
["family"]=> string(3) "Exo"
["style"]=> string(3) "200"
["subsets"]=> string(5) "latin"
}
Now I get the $data['value']['family'] = 'Abel' from my database.
Questions:
How can I get the ['variants'] for the given $data['value']['family'] value?
How can I get the index in $fonts['google'] for the sub-array where the $data['value']['family'] value is?
PHP supports Associative Arrays which let you use a (string) key rather than a numeric index for each element. These arrays are akin to javascript objects, Objective-C dictionaries, java HashMaps, etc.
That makes scenarios like this easy. Do you have control over building the original data array? If you can refactor your storage, set up the arrays like this:
$fonts['google'] = [
["ABeeZee"] => [
["variants"]=>["regular", "italic"],
["subsets"]=>["latin"]
],
["Abel"] => [
["variants"]=>["regular"],
["subsets"]=>["latin"]
],
["Abril Fatface"] => [
["variants"]=>["regular"],
["subsets"]=>["latin", "latin-ext"]
],
["Aclonica"] => [
["variants"]=>["regular"],
["subsets"]=>["latin"]
]
]
extra credit: if you have the original data as in the post, you could convert it:
$newArray = array(); // or just [] in PHP >= 5.3 I believe
foreach($fonts['google'] as $index=>$fontArray) {
$newArray[$fontArray['family']] = $fontArray;
// this leaves a redundant copy of the family name in the subarray
unset $newArray[$fontArray['family']]['family']; // if you want to remove the extra copy
}
Then it becomes trivial. Given a font family name, you just access $fonts['google'][$fontFamilyName] (or $newArray[$fontFamilyName]) using the family name as the array index.
I have this array called $items. My CMS created this array with three products and I just did a var_dump with the results below.
array(3) { [0]=> array(11) { ["item_id"]=> string(4) "1320" ["name"]=> string(5) "ITEM_A" ["price"]=> string(6) "$5.00" }
[1]=> array(11) { ["item_id"]=> string(4) "1321" ["name"]=> string(5) "ITEM_B" ["price"]=> string(6) "$5.00" }
[2]=> array(11) { ["item_id"]=> string(4) "1323" ["name"]=> string(5) "ITEM_D" ["price"]=> string(6) "$5.00" }
}
Is there a way I can inject another item, "ITEM_C" into this array so that it's now
array(3) { [0]=> array(11) { ["item_id"]=> string(4) "1320" ["name"]=> string(5) "ITEM_A" ["price"]=> string(6) "$5.00" }
[1]=> array(11) { ["item_id"]=> string(4) "1321" ["name"]=> string(5) "ITEM_B" ["price"]=> string(6) "$5.00" }
[2]=> array(11) { ["item_id"]=> string(4) "1323" ["name"]=> string(5) "ITEM_D" ["price"]=> string(6) "$5.00" }
[3]=> array(11) { ["item_id"]=> string(4) "1322" ["name"]=> string(5) "ITEM_C" ["price"]=> string(6) "$5.00" }
}
and then sort it by "name"? I basically want to always add one new row to this array which will always have different names and then sort it by the name. When I do the foreach php command I don't want it to be in the order of ITEM_A, ITEM_B, ITEM_D, ITEM_C. It needs to be ITEM_A, ITEM_B, ITEM_C, ITEM_D.
Appending to an array in PHP is easy as $array[] :
$myArray = array(1,2,3);
$myArray[] = 4; //now array(1,2,3,4)
So to add to your existing array, first create your new array element
$element = array("item_id" => 1322, "name" => "ITEM_C", /*etc.*/);
Then add it your array
$myArray[] = $element;
As for sorting, there are various sort functions depending on your exact needs. Since you're sorting according to a given array key, you'll probably need to call usort with a given function.
function nameCompare($a, $b)
{
$a = $a["name"];
$b = $b["name"];
return strcmp($a, $b);
}
usort($myArray, 'nameCompare');
Good day.
Code:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
I get array unique elements:
$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
if(!in_array($elms[0], $arr_uniq)) {
$arr_uniq[] = $elms[0];
}
}
Tell me pleasse how to get a count each unique element in the general array?
result should been next:
art_7880 - 3
art_7883 - 1
Assuming $all_array is subarray of your main array in your var_dump snipett, the general idea is
$result = array();
foreach ($all_array as $elms)
$result[$elms[0]]++;
array_count_values()
http://php.net/array_count_values
You should be able to easily apply this function.
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed last month.
I'm chasing my tail trying to combine the results of two different queries to output in a template.
I'm trying to merge the corresponding sub-arrays in model_data and entry_data to get desired_result. I will then iterate over desired_result and print values into the template.
Any assistance is greatly appreciated.
model_data
array(2) {
[0]=>
array(2) {
["entry_id"]=> string(3) "192"
["field_id_49"]=> string(10) "Model Name"
}
[1]=>
array(2) {
["entry_id"]=> string(3) "193"
["field_id_49"]=> string(5) "MN123"
}
}
entry_data
array(2) {
[0]=>
array(2) {
["uri"]=> string(24) "/products/product-title/"
["title"]=> string(13) "Product Title"
}
[1]=>
array(2) {
["uri"]=> string(22) "/products/lorem-ipsum/"
["title"]=> string(11) "Lorem Ipsum"
}
}
desired_result
array(2) {
[0]=>
array(4) {
["entry_id"]=> string(3) "192"
["field_id_49"]=> string(10) "Model Name"
["uri"]=> string(24) "/products/product-title/"
["title"]=> string(13) "Product Title"
}
[1]=>
array(4) {
["entry_id"]=> string(3) "193"
["field_id_49"]=> string(5) "MN123"
["uri"]=> string(22) "/products/lorem-ipsum/"
["title"]=> string(11) "Lorem Ipsum"
}
}
foreach($model_data as $key => $value){
$result[$key] = array_merge($entry_data[$key], $model_data[$key]);
}
You can use array_replace_recursive function to merge these arrays.
Below is a example:
$desired_result = array_replace_recursive($model_data, $entry_data);
var_dump($desired_result);