How to use these array in php - php

Can someone explain what is happening in these codes?
I don't understand how cols and rows are being used.
Thank you
$chartdata['cols'][] = array('label'=>'Days Range','type'=>'string');
$chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$prevyearbal)));
how these associative arrays get saved in the two dimensional array?
Is $chardata['cols'][] going from row 0 to row n?

As explained in the documentation, assigning to [] is basically the same as array_push but without the function call.

Related

PHP array_combine is not returning all the values

I am reading from the excel file two columns. One contains old ids and the other one returns new ids. So I am having two arrays:
$newAttributeIDs and $oldAttributeIDs
If I do count of each array separatly, I will get this result:
var_dump(count($newAttributeIDs)); // result is 3440
var_dump(count($oldAttributeIDs)); // result is 3440
And I would like to have key value pair of this values, but when I do:
$keyValueNewOldAttributeIDs = array_combine($oldAttributeIDs, $newAttributeIDs);
And then:
var_dump(count($keyValueNewOldAttributeIDs)); // result is 1990
I am getting wrong result, and some ids are now missing in the $keyValueNewOldAttributeIDs array.
Does anyone knows what is causing this? Thanks!
I solve this flipping the values. Since I had some of the same values in the $oldAttributeIDs, result was unexpected. First value should have all unique values in the array. I missed that fact.

Need to merge arrays but leave only one value if duplicated

I have two arrays with the same keys but different values. I need to merge it but if the values are the same leave only one of this
$array1 = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_1);
$array2 = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_2);
I need to get:
$array_result = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_1, 'address'=>$addres_2);
can anybody help to solve this?
array_merge does not work for me..
First you need to merge 2 arrays, using array_merge() function. then get the unique elements from the array using array_unique() function will get you the result
var_dump(array_unique(array_merge($array1, $array2)));
Edit
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
php doc
Thanks #Marco

arrays. getting the 3rd item of this multi array

Please help. I want to print the price. I need to print the price only. Help me. Thanks
$x=array(array('itm_name'=>'Optimization','price'=>'100','desc'=>'Chuchu'));
It seems you are using a 2 dimensional array
have you tried
echo $x[0]['price']
note '0' is the first element of the 1st layer of the array
so this can be substituted with 'x' so can iterate through a list
additionally if you want to get the other items of the array then
echo $x[0]['itm_name']
i hope you have grasped the basic concept of multi-dimensional arrays
using this simple explanation
Use this:
$x[0]['price']
$x[0] means the first element of the outer array, and ['price'] gets the price element of the inner 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})))

Grabbing specific values from a multi-dimensional array in PHP

I'm new to programming and I'm tackling arrays. I thought I had multi-dimensional arrays figured out but I guess I don't. Here's the code I'm working on:
$loopcounter = 0;
while ($myrow = mysql_fetch_array($results)) {
//...other stuff happens...
$allminmax[$loopcounter][] = array("$myrow[3]","$currentcoltype","$tempmin","$tempmax");
$loopcounter++;
}
This chunk of code is supposed to create an array of four values ($myrow[3], currentcoltype, tempmin, tempmax) and insert it into another array every time the loop goes through. When I do this:
echo implode($allminmax);
I get:
ArrayArrayArrayArrayArrayArrayArrayArrayArray
Do I need to implode each array before it goes into the master array? I really want to be able to do something like $allminmax[0][4] and get the $tempmax for the first row. When I try this now nothing happens. Thanks -- any help is appreciated!
It looks like you should either use [$loopcounter] or [], but not both. You also should drop the quotes. They're unnecessary and in the case of "$myrow[3]" they interfere with the variable interpolation.
$allminmax[] = array($myrow[3], $currentcoltype, $tempmin, $tempmax);
By the way, arrays are zero-indexed, so to get $tempmax for the first row it'd be $allminmax[0][3] not $allminmax[0][4].
Also, a better way to display the contents of your array is with print_r or var_dump. These will display arrays within arrays while a simple echo will not.
var_dump($allminmax);

Categories