I have two arrays:
$userBuildingIds
print_r():
Array
(
[0] => 4
)
and $allRequiredIds
print_r:
Array
(
[0] => 4
[1] => 1
)
now I want check if one element of $userBuildingIds exists in
the $allRequiredIds array. And if so, I want get a new array of all elements they are NOT in the first array like this:
Array
(
[0] => 1
)
(because 1 isn't in $userBuildingIds)
I try this with array_diff but it gives me this result (with the key of array 2):
Array
(
[1] => 1
)
Is it possible to get an array in which are all the elements of array $allRequiredIds where are not in $userBuildingIds, but without copy the keys from $allRequiredIds?
If you don't care about the keys of the returned array then you can just use array_values() on it to get a new array having the keys starting with 0.
The code will be:
$diffIds = array_values(array_diff($allRequiredIds, $userBuildingIds));
It produces a list of values from $allRequiredIds that does not exist in $userBuildingIds. The returned list has numeric keys starting with 0 (no association with the original keys from $allRequiredIds is $userBuildingIds is kept, on purpose).
Related
How to extract only value from the below array
Array ( [COUNT(department_name)] => 3 )
this is a response from a db query the actual value came like this
Array ( [0] => Array ( [COUNT(department_name)] => 3 ) )
if we select the 0th index $department[0] it is giving this
Array ( [COUNT(department_name)] => 3 )
but now i need only the value from this i.e 3 how can i extract the value from this.
and i tried like this $department[0]['department_name'] and $department[0]['COUNT(department_name)'] but no result.
You can use this simply and it's working -
$department = var_export($department,true);
print_R($department[0]['COUNT(department_name)'])
var_export helps you get the structured information of a variable.
For more see this -
var_export
NOTE - I will suggest you to to get a more readable key from database.
I am trying to compare two different arrays and get the values that do not exist in 1 of the arrays. Here are my 2 arrays:
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52)
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52 [3] => 583cee91-1913-4e9d-b51d-e27083420001)
As you can see the second array has an additional value. I am trying to user array_diff like this:
$result = array_diff($array1,$array2);
print_r($result);
However the out of the array_diff is:
array()
Any ideas what is going on?
As people have suggested and i have already tested switching the arrays around, this is the output:
Array ( [0] => [1] => )
array_diff gives you the values from $array1 that are not in the other arrays. All the values of your first array are in the second. Sou change the order of your arrays and you should be fine.
See also here: http://php.net/manual/de/function.array-diff.php
The order of arguments in array_diff() is important
Returns an array containing all the entries from array1 that are not
present in any of the other arrays2
Read array_diff
$result = array_diff($array2,$array1);
Try like this
I'm trying to figure out why it is that I cannot access the follow array with this statement:
var_dump($thevar[0]['product_id']);
Array
(
[d142d425a5487967a914b6579428d64b] => Array
(
[product_id] => 253
[variation_id] =>
[variation] =>
[quantity] => 1
[data] => WC_Product Object
(
[id] => 253
[product_custom_fields] => Array
(
[_edit_last] => Array
(
[0] => 1
)
[_edit_lock] => Array
(
[0] => 1345655854:1
)
[_thumbnail_id] => Array
(
[0] => 102
)
I can, however, access the 'product_id' using the dynamically created array name:
print_r($thevar['d142d425a5487967a914b6579428d64b']['product_id']);
The issue is, I don't know what that dynamic name is going to be on the fly...
There are several options for such scenarios.
Manually iterate over the array
You can use reset, next, key and/or each to iterate over the array (perhaps partially).
For example, to grab the first item regardless of key:
$item = reset($thevar);
Reindex the array
Sometimes it's just convenient to be able to index into the array numerically, and a small performance hit is not a problem. In that case you can reindex using array_values:
$values = array_values($thevar);
$item = $values[0]; // because $values is numerically indexed
Iterate with foreach
This would work for a single value as well as it works for more, but it might give the wrong impression to readers of the code.
foreach($thevar as $item) {
// do something with $item
}
If the array key is dynamic you might find the PHP function array_keys() useful.
It will return an array of the keys used in an array. You can then use this to access a particular element in the array.
See here for more:
http://php.net/manual/en/function.array-keys.php
Because PHP array are associative therefor you have to access them by key.
But you may use reset($thevar) to get first item.
Or array_values():
array_values($thevar)[0]
Or if you feel like overkill you may also use array_keys() and use the [0] element to address element like this:
$thevar[ array_keys($thevar)[0]]
I have some data for my sites statistics in a SQL DB:
date: visits: pageviews:
12-12-12 34 21
12-12-13 31 22
12-12-14 33 2445
12-12-15 35 2422
12-12-16 36 232
//ect ect
I'm trying to create a Multidimensional array that contains all the dates info from the DB and where the date will be the key (selector,name of the array inside the multi array), so as the end result, I should be able to just do this:
print_r $my_multi_array[12-05-12];
And I should see the statistics for that date on the screen.
Now I know how to do all the loops and stuff, and I even have a good idea about how to do multidimensional arrays, it's just that I think I'm doing something wrong:
//first things first, define the array:
$my_multi_array=array();
//then, in a loop, append to the array:
$my_multi_array[]=array("$date"=>array('visits'=>mysql_num_rows($visit_query),'pageviews'=>$pageview_query));
Now when I print_r that array, everything looks good:
Array ( [0] => Array ( [11-12-24] => Array ( [visits] => 1 [pageviews] => 0) ) [1] => Array ( [11-12-25] => Array ( [visits] => 1 [pageviews] => 0) ) [2] => Array ( [11-12-26] => Array ( [visits] => 1 [pageviews] => 0)))1
Notice the 1 at the end ^^. That seemed to be in the result (not a typo).
Now when I try printing a certain array out (using the date as the key):
print_r $my_multi_array['11-12-24'];
I get:
1
So then i try:
print_r $my_multi_array[2];
and that works fine.
For some reason, it wont let me select an array from $my_multi_array using the date as the key.
Any ideas on how to fix this?
thanks
Everything is correct, because you don't have array('key' => 'value') style array, instead of that you've got array( [0] => array( 'key' => 'value' ) ) that's why you're getting correct result on accessing numeric key of the array.
You have to put the date as the array key, like so:
$my_multi_array[$date]=array("$date"=>array('visits'=>mysql_num_rows($visit_query),'pageviews'=>$pageview_query));
Notice the $my_multi_array[$date].
By doing $my_multi_array[] = ... you are just creating a new numerical index on the array with the content on the right side. That's why when you access the array with the numerical index, like $my_multi_array[2], it works.
On the other hand, by doing $my_multi_array[$date]you are treating the array like an hash table, where you associate a key (in this case a string containing a date) with a value.
I have two arrays: Array ( [0] => 2 [1] => 3 ) and Array ( [0] => 2 ).
I want to get the value, which is not in second array. So I have used the array_diff function but my result will get Array ( [1] => 3 )
Actually this is the result. But a small problem here, its position is (key) 1. I want the result in to a new array starts from 0th position, i.e., Array ( [0] => 3 ).
How can I achieve this?
you can use array_values(array_diff($arr1, $arr2)); if order doesn't matter
You should run array_values() on the result and this would give you a new array with indexes starting at 0.
This is a known shortcoming of array_diff(), check the php docs.