array_column() expects at most 3 parameters, 13 given - php

I have a PHP array as follows:
print_r($myarray);
Array
(
[0] => Array
(
[JAN] => 484603732
[FEB] => 350203732
[MAR] => 133347732
[APR] => 203347732
[MEI] => 79797732
[JUNI] => 112047732
[JULI] => 380597732
[AGS] => 76597732
[SEP] => 86597732
[OKT] => 120397732
[NOV] => 391597732
[DES] => 58597732
)
)
I want to delete element like [JAN], [FEB], [MAR], ...
But I little bit confused about how to handle it with array_column
So far, I've tried:
$array = array_column($myarray, 'JAN','FEB','MAR','APR','MEI','JUNI','JULI','AGS','SEP','OKT','NOV','DES');
But, still facing error
array_column() expects at most 3 parameters, 13 given
My expected output:
Array
(
[0] => 484603732
[1] => 350203732
[2] => 133347732
[3] => 203347732
[4] => 79797732
[5] => 112047732
[6] => 380597732
[7] => 76597732
[8] => 86597732
[9] => 120397732
[10] => 391597732
[11] => 58597732
)
Any well thought to advise will be appreciated.
Thanks.

You don't need array_column() here. According to PHP documentation
array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.
You can simply use array_values() to remove all the keys.
$array = array_values($myarray[0])

Related

in_array() not working with two dimensional associative array?

I'm trying very simply to use in_array() to check a key is in an array and then echo its value.
$array = Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
And I have value 556729685 which I want to check that this value exists or not? So I am using in_array() function for this.
in_array(556729685, array_keys($array));
in_array(556729685, array_values($array));
in_array(556729685, $array);
All above three i have used but result always showing NULL means blank.
I am really frustrated to find the solution. I don't understand what's happening.
You should use array_column() which will return the values from a single column in the input array as an array.
$product_auto_ids = array_column($array['cart_item'], 'product_auto_id');
In this case, it would return the following:
Array
(
[0] => 556729685
[1] => 951790801
)
Then you can use in_array() like you currently are.
in_array(556729685, $product_auto_ids);

How to set php array index as array value...?

I have this array from a query.
Array
(
[0] => Array
(
[user_id] => 5
[first_name] => Diyaa
[profile_pic] => profile/user5.png
)
[1] => Array
(
[user_id] => 8
[first_name] => Raj
[profile_pic] => profile/user8.jpg
)
[2] => Array
(
[user_id] => 10
[first_name] => Vanathi
[profile_pic] => profile/user10.jpg
)
)
I need to set array index as like array value (user_id) as given below:
Array
(
[5] => Array
(
[user_id] => 5
[first_name] => Diyaa
[profile_pic] => profile/user5.png
)
[8] => Array
(
[user_id] => 8
[first_name] => Raj
[profile_pic] => profile/user8.jpg
)
[10] => Array
(
[user_id] => 10
[first_name] => Vanathi
[profile_pic] => profile/user10.jpg
)
)
Note: user_id is an unique value, it won't repeat again. No need to worry about index value.
How to convert and get that array as specified index value..?
This is exactly what array_column() is for:
$result = array_column($array, null, 'user_id');
array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.
column_key
The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).
You can try this code, here I do some extra work. Refer to AbraCadaver's clever answer $result = array_column($array, null, 'user_id');.
array_combine(array_column($array, 'user_id'), $array);
Both structures are unnecessarily complex and redundant. Why not
$foo = array(5 =>
array('first_name' => 'Diyaa',
'profile_pic' => 'profile/user5.png'),
8 =>
array('first_name' => 'Raj',
'profile_pic' => 'profile/user8.png'),
...
);
Then access it via $foo[$user_id], which will give you a 2-element associative array such as
array('first_name' => 'Raj',
'profile_pic' => 'profile/user8.png'),
For changing a profile_pic:
$foo[$user_id]['profile_pic'] = $new_pic;

Three Dimensional Array Changing

I'm calling an three dimension array from an API, however after different calls to the API the data back is slightly different, sometimes the array keys change. For example the first array may relate to type one in one case, whereas in another case it relates to type two. It's laid out like this
Array
(
[id] =>
[stats] => Array
(
[0] => Array
(
[type] =>
[option] =>
[modifyDate] =>
As stated before sometimes it relates to different types, is there a way of getting an array based on what is inside of it, for example if the "type" in the first array equals type one then assign that to the variable Type1?
Perhaps in a better example, in scenario 1 it shows this:
Array
(
[summonerId] => 39562006
[playerStatSummaries] => Array
(
[0] => Array
(
[playerStatSummaryType] => AramUnranked5x5
[wins] => 4
[modifyDate] => 1481110651000
[aggregatedStats] => Array
(
[totalChampionKills] => 48
[totalTurretsKilled] => 2
[totalAssists] => 171
)
)
whereas in scenario 2 it shows this
Array
(
[summonerId] => 34951469
[playerStatSummaries] => Array
(
[0] => Array
(
[playerStatSummaryType] => CAP5x5
[wins] => 16
[modifyDate] => 1481117277000
[aggregatedStats] => Array
(
[totalChampionKills] => 325
[totalMinionKills] => 1996
[totalTurretsKilled] => 26
[totalNeutralMinionsKilled] => 1048
[totalAssists] => 298
)
)
After some trial and error myself i think a foreloop will be good as it could iterate each array and output chosen keys from the array however i'm still unsure on how to do this, any suggestions?
My advice is similar to this answer: https://stackoverflow.com/a/42708457/2943403
Unfortunately, I cannot give more detailed information without small relevant samples of the related input arrays and a desired output array.
// $new=array(...);
// $old=array(...);
foreach($new as $new_key=>$new_subarray){
foreach(array_diff_key($old,range(0,$new_key)) as $old_subarray){ // no dupe loops
// perform checks between $new_subarray and $old_subarray
}
}

How to merge these two arrays in this specific order

I have two array as following
Array
(
[0] => 641
[1] => 622
[2] => 100
[3] => 6431
)
Array
(
[0] => 64.1
[1] => 62.2
[2] => 10
[3] => 643.1
)
How can I make it as following
Array
(
[0] => 641
[1] => 64.1
[2] => 622
[3] => 62.2
[4] => 100
[5] => 10
[6] => 6431
[7] => 643.1
)
It's as simple as
$result=array_merge($array1,$array2);
Note: Your values wont be in the order you presented though. If that is important then you need to loop through your arrays to build a new array accordingly.
Ummm ok here is that version as well
if(count($array1)==count($array2))
{
for($i=0;$i<count($array1);$i++)
{
$result[]=$array1[$i];
$result[]=$array2[$i];
}
}
print_r($result);
Fiddle
Manual
you can use array_merge() function merges one or more arrays into one array.
example:
array_merge(array1,array2,array3...)

How to remove empty values from the multi dimensional array

I have array structure like this,
Array
(
[1] => Array
(
[1] =>
[2] =>
[3] =>
[4] => Product
[5] => Product Name
..
[59] => Color
)
[2] => Array
(
[1] =>
[2] =>
[3] => 1
[4] => 9155
....
[59] =>
)
[3] => Array
(
[1] =>
[2] =>
[3] => 1
[4] => 9165
...
[59] => Green
)
[4] => Array
(
[1] =>
[2] =>
[3] =>
[4] =>
...
[58] =>
[59] =>
)
)
Its reading data from Excel sheets , the issue is when i read data from excel sheet it reads empty rows too, I already tried to ignore empty rows from the excel sheet some how its working (when the excel is created from MSexcel ) but from Google Drive its reading empty rows. So I would like to remove those rows with 1- 59 are blanks. in the above example array with index 4 .
Note that some index have missing values in many sub index but I don't want to remove those, only all sub indexes from 1-59 are blank then that main index (here its 4) needs to remove.
Is there any smart way to remove those array index that have empty values. I not like to iterate all the array and store to another.
if you want to remove the index 4 that is an empty array :
array_filter(array_map('array_filter', $array));
Use array_map
$array = array_map('array_filter', $array);
let try with array_filter
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);
print_r(array_filter($entry));
Array
(
[0] => foo
[2] => -1
)
Use array_filter..It will remove all empty values..
array_filter($array);

Categories