Comparing Associative array and standard array PHP - php

I have two arrays
Array1:
Array ( [0] => Array ( [0] => 3 [1] => 1 [2] => 4 ) [1] => Array ( [0] => 1 [1] => 6 ) )
Array2:
Array ( [0] => 1 [1] => 3 [2] => 2 )
I used array_diff for comparing and getting the difference values, but the same key is coming ie.,
array_diff(Array1,Array2)
returns Array([0] =>3 [2] => 4)
but is there any other way to get difference and having result like
Array([0] =>3 [1] => 4)..

Assuming you've got array_diff working on the multidimensional array somehow, but from the docs:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.
Use array_values around it.
array_values(array_diff($array1, $array2));

Related

Multidimensional Array - Get unique values, but count all duplicates

I have a multi-dimensional array that I would like to get unique sub-values from, but also have a count of how many times those unique sub-values occurred.
For instance, this would be my starting array:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 3333333333333333
)
)
[1] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 5555555555555555
)
)
[2] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 77777777777777777
)
)
In the end, I'd like to have an array that looks like this:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
[count] => 3
)
[1] => Array
(
[id] => 3333333333333333
[count] => 1
)
[2] => Array
(
[id] => 5555555555555555
[count] => 1
)
[3] => Array
(
[id] => 77777777777777777
[count] => 1
)
)
Is there any general/easy way to do this without iterating through the first array for each value, comparing/storing the values in a temporary array, checking them, and adding to the count?
To get this exact format you may need to iterate thought your current array and do the counting manually, however php has the array_count_values() and array_unique() functions for this kind of thing:
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.array-unique.php
Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. Note that a reference to the output array $counted is used in the callback.
array_walk_recursive($ids, function($id, $k) use (&$counted) {
$counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});
Using the id as the key in the $counted array will simplify the counting. The result of this will be somewhat different from your suggested output, but in my opinion it would actually be simpler to use. (e.g. foreach ($counted as $id => $count) {...).
$counted = array(
"1533438473619168" => 3
"3333333333333333" => 1
"5555555555555555" => 1
"77777777777777777" => 1);

Deleting a number from an array in PHP

I want to remove a number from an array, for example this array:
Array ( [0] => [1] => 2 [2] => 3 )
I want to remove the number 2 so i will get this:
Array ( [0] => [1] => 3 )
but I get this:
Array ( [0] => [2] => 3 )
What I did is to check if the number 2 is in the array and removing it with unset. Is there a better way to do this?
You can reset the numeric indices of your array after you removed the element with array_values.
unset($myArray[1]);
$myArray = array_values($myArray);

convert multiple single dimensional array to single multi dimensional array in php

I have 'N' single dimensional arrays(where N represents number of records retrieved by a sql command)
How do I convert it to multi dimensional array with keys as index?
At first I thought of creating a separate key array and then combining it with those N arrays
My output
Array ( [0] => PONDICHERRY [1] => 31-Jan-2018 [2] => [3] => distance and height are not proportional. )
Array ( [0] => PONDICHERRY [1] => 03-Feb-2020 [2] => [3] => helloooooooooooooo )
My expected output
Array ( [0] => Array ( [0] => PONDICHERRY [1] => 31-Jan-2018 [2] => [3] => distance and height are not proportional. ) [1] =>Array ( [0] => PONDICHERRY [1] => 03-Feb-2020 [2] => [3] => helloooooooooooooo ) )
It's difficult for me to combine the arrays, but how do i create like this?
Thank you
Rather than creating N separate arrays and then combining them later, you can combine them already while reading the rows.
$results = array();
while($row = $query->fetch_row()){
$results[] = $row;
}
// here you have $results in the expected output format

Merge Arrays, Sort Value, Remove Duplicates and Reindex

I have two separate Arrays
I want to merge these arrays, however I can't seem to do this.
Result from F1
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
Result from F2
Array
(
[id] => Array
(
[0] => 2
[1] => 7
[2] => 9
)
)
FINAL RESULT DESIRED
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 7
[4] => 9
)
)
Note I am reindexing the Values by numerical order.
Many thanks
Here's a way to do it without removing the 'id' keys:
$newArray = array_merge_recursive($joinedIDs, $committeeIDs);
$newArray = array_map(function($e){return array_unique($e);}, $newArray);
Try this,
array_unique(array_merge($array1,$array2), SORT_REGULAR);
http://se2.php.net/manual/en/function.array-unique.php
The issue was that F1 & F2 were assigning ['ID'].
Removing this, allows the array_merge functions to work as expected.
$newArray = array_unique(array_merge_recursive($joinedIDs, $committeeIDs));

What does [1] => 0 mean in this array?

I know this must be a fairly simple question, but I haven't managed to stumble across an answer yet.
I have the following array
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
When I use print_r($qid) I get the following
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
I don't understand [1] => 0
in
[0] => Array ( [0] => 1 [1] => 0 )
If someone could explain what [1] => 0 means in this array, I'd greatly appreciate it. Thanks.
EDIT: It turns out that my array was indeed different to what I had written above, because it had been modified later in the code. Thanks everyone for the great answers. I'm still reading over them all and trying to make my mind understand them (Arrays turn my mind to jello).
[1] => 0 denotes an array element with the value 0.
The numbers in [] are array keys. So [1] is the second element of a numerically indexed array, (which starts with [0]), and the value of the second element ([1]) is 0.
PHP uses => as an operator to relate array keys/indices to their values.
So an overall explanation of this structure:
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
The outer array is a numerically indexed array, and each of its elements is a sub-array. The first of them ([0]) is an array containing 2 elements, while the rest of them ([1] through [3]) are arrays containing only one single element.
That two-dimensional array is actually a one-dimensional array of arrays, which is why you're getting the nesting. The [x] => y bit simply means that index x of the array has the value y.
Now your output in this case doesn't actually match your code, since
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);
produces:
Array (
[0] => Array ( [0] => 1 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
If you wanted to get:
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
(with the first array having two elements), you'd actually need:
$qid[0][0]=1;
$qid[0][1]=0;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);
You probably added a second item to $qid[0] somewhere ($qid[0][1] = 0). This code
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
outputs the the correct values for me (without [1] => 0:
Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 3 ) [3] => Array ( [0] => 4 ) )
It means that your index 0 in the original Array contains another Array of 2 items.
Specifically [1] => 0 means that the 2nd item of the "child" Array contains the number 0.
[1] => 0
in this simple way we can say that 1 is your array key and 0 is value for the 1 key
0 is store at the 1 key of the array
thanks
Simply put, you have a numerically indexed multidimensional array. http://php.net/manual/en/language.types.array.php should have all the information you need to read up on this.
As to why you have the [1] => 0, you'll need to look a little deeper into your code to see where it gets assigned.
I got the following result after printing out the array using print_r:
Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 3
)
[3] => Array
(
[0] => 4
)
)
I guess, you might have set a value for $gid[0][1] somewhere in your code.

Categories