How to extract value from COUNT array within array - php

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.

Related

Get the key value from a multidimensional array in PHP

I've the following type of array:
Array (
[2017-01-01] => Array (
[booking_nb] => 0
)
[2017-01-02] => Array (
[booking_nb] => 0
);
How can I get the value of booking_nb if the date is equal to 2017-01-02 ?
Do I need to loop into the array ?
Thanks.
Assuming 2017-01-02 is an array key, you can do the following:
$array['2017-01-02']['booking_nb']; // will return the value 0
However, I recommend that if you are only storing the booking_nb value inside of each sub-array (i.e. no other elements inside the sub-arrays), you simply store them like so:
array(
'2017-01-01' => 0,
'2017-01-02' => 0,
)
This way, you can select with the following:
$array['2017-01-01']; // gives 0
The simplicity gained from this method also has the downside of the inability to store additional data, so use according to your needs.

PHP: Separate 2 or more values of 1 key in Array

I have array came from my ajax
$contestant_name_arr = $_GET['contestant_name_arr'];
print_r($contestant_name_arr);
Whenever i try to get the value of each in loop i got error because instead of this
Array ( [0] => value1,value2 )
It should be look like this:
Array (
[0] => value1
[1] => value2
)
How do I separate like that in the example above.
Either devise your url query string to be:
http://yourhost.com?contestant_name_arr[0]=value&contestant_name_arr[1]=value2
Or just simply explode;
$contestant_name_arr = explode(',', $contestant_name_arr[0]);

php array_diff compare arrays with new array

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).

Change The Value of a Field in a Multidimensional Array in PHP

I have an array that looks like this
Array
(
[0] => Array
(
[Title] => The Title
[Price] => 700
[Quantity] => 2
)
)
Say I wanted to change the Quantity to 5 how would I do that if the array were stored in the variable $ItemArray?
Try $itemArray[0]['Quantity'] = 5;.
Basically, you have an array, $itemArray, which contains an associative array. To access that inside array, you simply use standard PHP array syntax: $itemArray[0].
Then, you need the Quantity field of that inner array. Using the nested array syntax, you append ['Quantity'] to the end of our previous statement, resulting in: $itemArray[0]['Quantity'].
At this point, you have the field you want, and you can use the normal = to set the field value.
$itemArray[0]['Quantity'] = 5;
thats very simple, try
$itemArray[0]["Quantity"] = 5;
What we're doing here is accessing the first index within $itemArray which is 0; 0 contains an array, so we now specify which part of 0 we want to access: Like this basically:
$array[index][innerarrayindex]

How to reset indexes in array_diff result?

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.

Categories