PHP: Getting array type - php

Is there a php function that someone can use to automatically detect if an array is an associative or not, apart from explictly checking the array keys?

My short answer: YES
Quicker and easier, IF you make the assumption that a "non-associative array" is indexed starting at 0:
if ($original_array == array_values($original_array))

quoted from the official site:
The indexed and associative array
types are the same type in PHP,
So the best solution I can think of is running on all the keys, or using array_keys,implode,is_numeric

function is_associative_array($array) {
return (is_array($array) && !is_numeric(implode("", array_keys($array))));
}
Testing the keys works well.

Check out the discussions at is_array.

Short answer: no.
Long answer: Associative and indexed arrays are the same type in PHP. Indexed arrays are a subset of associative arrays where:
The keys are only integers;
They range from 0 to N-1 where N is the size of the array.
You can try and detect that if you want by using array_keys(), a sort and comparison with a range() result.

Related

How does mysql_fetch_array($result_type=MYSQL_BOTH) return both associative and numeric indices?

In the documentation http://php.net/manual/en/function.mysql-fetch-array.php, it states that "By using MYSQL_BOTH (default), you'll get an array with both associative and number indices." But it is my understanding that php arrays can either be associative or numeric, not both. You could obviously create an associative array that contains the numeric indices as well, as keys, but that would double the array count. So how do they do this? Have they made a special exception for this one function? Is what it returns not actually a conventional array but their own special array just for this purpose? Or is there something that I am missing?
According to the PHP Documentation on Arrays a PHP array is "Actually an ordered map", and can be both associative and numeric. Using MYSQL_BOTH uses associative and numeric keys in the same array.

php array creation like (''=>'')

What is difference between $prefix=array(''=>''); and $prefix=array();
what exactly $prefix=array(''=>''); using for ?
There isn't really a difference, both are arrays. The difference is, the latter has an array key.
For instance,
$test1=array(1,2,3,4,5);
$test2=array('name'=>'bob','lastname'=>'fossil');
will return;
print_r($test1[0]);
//1
print_r($test2['name']." ".$test2['lastname']);
//bob fossil
Basically, it gives the value a name
key=>val
can be used like this
foreach($test2 as $val){
echo$val;
}
//bob
//fossil
It's used for creating associative arrays
No difference. first option creates an array with some elements at once, the second creates an empty array.

How to check an associative array for all null values in php?

This is probably a very trivial question, but please bear with me.
I am trying to read a lot of data into an array of associative arrays. The data contains a lot of empty arrays and arrays with keys set and but all null values. I want to ignore those and only push arrays with at least one key mapped to a non-null value. (The data comes from an excel sheet and it has lots of empty cells that are registered as "set" anyway.) So far I have tried:
if(!empty(${$small_dummy}))
array_push(${$big_dummy}, ${$small_dummy});
That gets rid of the empty arrays but not the ones where all keys map to null. Is there a better way to do this than looping through the entire array and popping all null values?
Judging by the code you have already, you can change:
if(!empty(${$small_dummy}))
to:
if(!empty(array_filter(${$small_dummy})))
That will filter out all empty values (values evaluating to FALSE to be precise) and check if the resulting array is empty. Also see the manual on array_filter().
Note that this would also filter 0 values so you might need to write a custom callback function for array_filter().
You can try if(!array_filter($array)) { also
This isn't an ideal approach, but array_sum will return 0 if all values can't be cast to a numeric value. So :
$small_dummy = array("a" => null, "foo", "", 0);
if(array_sum($small_dummy) === 0)
would pass. But this is only the way to go if you are expecting the values to be numeric.
Actually, if the problem is that the array keys have values and therefor are not passing as empty(), the go with array_values:
if(!empty(array_values(${$small_dummy})))

Getting PHP $_POST values by index?

I'm curious, more than anything - is it possible to get a PHP value posted? i.e. $_POST['foo'] via some kind of indexing? i.e. $_POST[0]?
($_POST[0] does not work by the way)
No, it's not possible: you cannot fetch values from an associative array by numeric indexes (because, as clearly noted in the doc, PHP does not distinguish between indexed and associative arrays).
That's why some functions (PDOStatement::fetch and its siblings, for example) that return arrays take an additional param to control the 'type' of indexes in the array returned: numeric (FETCH_NUM), string (FETCH_ASSOC) or both (FETCH_BOTH, the default value). )
The closest you might get with reindexing:
$myPost = array_values($_POST);
not that i'm aware of, check out a print_r($_POST) to see all goodies that you can access. You could iterate over the values with:
foreach($_POST as $key=>$value){
echo $key.' '.$value."\n";
}
You could throw in an $i++ if you wanted to keep track of a count ....
You can take data stored in $_POST variables and store them into indexed elements. But they are not stored as an indexed element initially.

I Don't Understand This Array Syntax

I don't understand this array accessing syntax:
$target[$segs[count($segs)]]
Is it really possible to use variables as multidimensional array keys?
That might result in an error, if $segs is a numerical array with continuous indices only.
Meaning, it would fail for:
array("foo","bar");
but work for
array("foo", 2=>"bar");
Assuming now, that we deal with the first case, then this would work:
$target[$segs[count($segs) - 1]]
First, count($segs) - 1 will be evaluated and return a number. In this case the last index of $segs (provided it is a numerical array).
$segs[count($segs) - 1] will therefore return the last element in $segs. And whatever that value is, will be used as index for $target[...].
To sum up: It is nested array indexing and evaluated inside out.
See it in action.
Whether or not such a method is necessary depends on the problem you are trying to solve. If you don't know where you would use such nested, variable array indexing then you probably don't need it.
That syntax is fine, provided $segs is an array. It's worth noting, though, that if you're using a numerically indexed array for $segs, calling count($segs) is a non-existent key because indexing starts at zero.

Categories