How to check if array like this
array(3) { [0]=> array(0) { } [1]=> array(0) { } [2]=> array(0) { } }
is actually empty ? Because for me this is an empty array but for the empty() this is an array with 3 elements and for count this is a array with length 3 .. so is there a way to do it without foreaching the array ?
Thank you in advance
if(!array_filter($array)){
// empty
}
(docs)
It is also stated here
Check whether an array is empty
use array_filter();
Related
I'm working on a php magento script which have a array variable for store some script urls.
array variable $items['js']
var_dump
array(1) {
[""]=>
array(17) {
["prototype/prototype.js"]=>
string(22) "prototype/prototype.js"
["varien/form.js"]=>
string(14) "varien/form.js"
["mage/translate.js"]=>
string(17) "mage/translate.js"
["mage/cookies.js"]=>
string(15) "mage/cookies.js"
["wyomind/layer/native.history.js"]=>
string(31) "wyomind/layer/native.history.js"
["varien/weee.js"]=>
string(14) "varien/weee.js"
["geissweb/vatvalidation-min.js"]=>
string(29) "geissweb/vatvalidation-min.js"
}
}
I tried to access the "geissweb/vatvalidation-min.js" value like this
$items['js']['geissweb/vatvalidation-min.js']
but it return empty value, is there have a way to get that value without use foreach or for loop. Thank You
Your index is '', shown by...
array(1) {
[""]=>
so you need to use...
$items['js']['']['geissweb/vatvalidation-min.js']
You have your variable $items['js'] as an array of arrays what your looking for without a foreach is this :
$items['js'][0]['geissweb/vatvalidation-min.js'] is not valid
after tests
$items['js'][""]['geissweb/vatvalidation-min.js'] is valid.
pls, i would like to get the values of the $aa variable, i'm using the mysqli_fetch_all because all the values need to be used in another layer.
Thanks
$aa = mysqli_fetch_all($ttt,MYSQLI_ASSOC);
Output with var_dump($aa):
array(2) { [0]=> array(1) { ["followe"]=> string(8) "bammyww " } [1]=> array(1) { ["followe"]=> string(5) "demo " } }
i have tried using $aa['followe'] , but i'm getting invalid index error.
Just loop through it. It's an array containing associative arrays.
foreach($aa as $item)
{
$item['followe'] // do something with this.
}
Instead of $aa['followe'], try:
$aa[0]['followe'];
as its a multi-dimension array. And the right approach to get all the array element is using foreach() like:
foreach($aa as $item)
{
$item['followe']
}
use
$aa[0]['followe'];
$aa[0] is array(1) { ["followe"]=> string(8) "bammyww " }
$aa[0]['followe'] is string(8) "bammyww "
You can also use array_column as
array_column($aa,'followe');//retrieves values associated with the key followe
A var_dump of $_POST gives the following result:
array(1) {
["postID"]=>
array(1) {
[0]=>
string(2) "76"
}
}
I want to bind the data from position [0] -> "76" to a variable called $id.
What is the correct way to handle this?
Thanks in advance!
You can access this value doing:
$id = $_POST['postID'][0];
i am trying to ksort an array, it worked when i was working with php, but i tried this in smarty template it didnt worked..
i tried like this
{{ksort($var)}}
but it returned this bool value and it even prints it..
1
actual array in $var is
array(1) { [1]=> array(2) { ["Name"]=> NULL ["SubMenu"]=> array(1) { [1]=> array(1) { ["SubName"]=> NULL } } } }
I have even tried {{$var = ksort($var)}} but it just store that bool value.
any ideas how to ksort this array in smarty??
According to the documentation ksort() always returns a boolean and the array is modified by reference.
You could assign the result of ksort() to a unused variable and use the array for your output like this.
{$tmp = ksort($var)}
{$var}
I have an array like this:
array(2) {
[0]=> array(1) { ["cate_id"]=> string(2) "14" }
[1]=> array(1) { ["cate_id"]=> string(2) "15" }
}
How can I check if the value 14 exists in the array without using a for loop?
I've tried this code:
var_dump(in_array('14',$categoriesId));exit;
but it returns false, and I do not know why.
I wonder why you don't need a for. Well a quickest way would be to serialize your array and do a strpos.
$yourarray = array('200','3012','14');
if(strpos(serialize($yourarray),14)!==false)
{
echo "value exists";
}
Warning : Without using looping structures you cannot guarantee the value existence inside an array. Even an in_array uses internal looping structures. So as the comments indicate you will get a false positive if there is 1414 inside the $yourarray variable. That's why I made it a point in the first place.
If you need to find a specific value in an array. You have to loop it.
Do this :
var_dump(in_array("14",array_map('current',$categoriesId))); //returns true