Reputation: 13
I am looking to except single element from array. My array is:
Array
(
[0] => Array
(
[0] => Array
(
[COUNT(ID)] => 0
)
)
)
I have already use PHP functions basename
and array_shift
, but they didn't give me proper value. I want only single string COUNT(ID)
value.
Here is my function using in cakephp model:
$res = $this->query("select COUNT(ID) from users where Username = '".$Username['Username']."'");
if ($res[0][0]['COUNT(ID)'] >= 1)
{
return false;
}
else
{
return true;
}
I don't need $res[0][0]
, I need only COUNT[ID]
. Is there any easy way to find only COUNT[ID]
.
Upvotes: 0
Views: 1653
Reputation: 311
You can use following technique for your solution. i did not get you what you want to do with your array but you can try below solution
$arr = Set::extract("/0/COUNT(ID)",$data);
where $data is your input array.
you will get following output
Array
(
[0] => 5
)
you can refer below link
Remove array key from array in cakephp
Upvotes: 0
Reputation: 2989
Try this code:
public $uses = array('User');
$count = $this->User->find('count', array(
'conditions' => array('Username' => $Username['Username'])
));
if ($count >= 1) {
return false;
} else {
return true;
}
Upvotes: 0
Reputation: 2347
If you are using CakePHP, it's good to use model and inbuilt functions to get answer what you want...
$this->loadModel('User'); //If you are in controller
$total = $this->User->find('count', array(
'conditions' => array('Username' => $Username['Username'])
));
You'll get answer in single variable..!!
Upvotes: 6
Reputation: 1
You can probably fetch it by associative array to reduce dimension but you can remove index. May be you can use foreach to automatically use indexs.
Upvotes: 0
Reputation: 4142
If you are sure that array structure will remain same then this could be useful :-
function array_values_recursive($ary)
{
$lst = array();
foreach( array_keys($ary) as $k ){
$v = $ary[$k];
if (is_scalar($v)) {
$lst[] = $v;
} elseif (is_array($v)) {
$lst = array_merge( $lst,
array_values_recursive($v)
);
}
}
return $lst;
}
$arr=array(array(array('COUNT(ID)'=>5)));
$res=array_values_recursive($arr);
echo '<pre>';print_r($res);
output :-
Array
(
[0] => 5
)
Upvotes: 0
Reputation: 196
If you know the exact count of nesting in array you can just access it with indexes
$array[0][0]['COUNT(ID)']
If no, you might want to use the function which will recursively find you the first nested element that is not an array
function getStringFromArray($array) {
if (is_array($array)) {
foreach ($array as $key => $value) {
if (is_array($value)) {
return getStringFromArray($value);
} else {
return $value;
}
}
} else {
throw new Exception("Not an array given");
}
}
Upvotes: 0
Reputation: 788
Let's say you array's name is $myArray
, than you can assign the value of $myArray[0][0]['COUNT(ID)']
to $value
in the way you usually assign a value: $value = $myArray[0][0]['COUNT(ID)'];
.
If you want to delete an index from an array, use unset()
like this: unset($myArray[0][0]['COUNT(ID)'])
. I hope this answers your question.
Upvotes: 0
Reputation: 3684
Your output is produced by code like this
$data = array(
array(
array(
'COUNT(ID)' => 0
)
)
);
You can access its value by calling directly
$data[0][0]['COUNT(ID)']
This could be wrong problem you are solving, as it seems like database query result that should not look like that and could be done with more ease. You should show your original function/problem, that this script is part of.
Upvotes: 0