Getting the first key in a multidimensional array with PHP - php

I have a multidimensional array called $test:
Array (
[First item] => Array (
[screen] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => 2
)
[Second Item] => Array (
[screen] => 3
[1] => 3
[2] => 3
[3] => 3
[4] => 3
)
)
I am trying to get the keys: screen, 1, 2, 3, and 4.
They are the same for First item and Second item. Do you know how I can loop through this array to get those values? So, basically getting the keys for the first array in my multidimensional array. Thank you!

How about this:
$keys = array_keys($test['First item']);
Or if you want to do it manually:
$keys = array();
foreach($test['First item'] as $key => $value) {
$keys[] = $key;
}

That would be something like:
$keys = array_keys(reset($your_array));
reset() gets the first value of your array and array_keys() the keys of the resulting array.
You might want to split it in two lines using a temporary variable to avoid strict warnings.

It depends on what you are trying to archive:
$test = Array (
"First item" => Array (
"screen" => 2,
1 => 2,
2 => 2,
3 => 2,
4 => 2,
),
"Second Item" => Array (
"screen" => 3,
1 => 3,
2 => 3,
3 => 3,
4 => 3,
)
);
To get all the keys
$vals = [];
foreach($test as $k=>$v){
$vals = array_merge($vals, array_keys($v));
}
this will yield you:
Array
(
[0] => screen
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => screen
[6] => 1
[7] => 2
[8] => 3
[9] => 4
)
Separated in another multidimensional array:
foreach($arr as $k=>$v){
$vals[] = array_keys($v);
}
Only the unique keys:
foreach($test as $k=>$v){
$vals = array_unique(array_merge($vals, array_keys($v)));
}

Related

Convert Multidimensional arrays as key and value in PHP

I have 2 Multidimensional arrays as follow:
Array1:
Array (
[0] => Array (
[0] => 2D Design
[1] => 3D Design & Modeling)
[1] => Array ( [0] => Android Developer
[1] => Artificial Intelligence
[2] => Web Developer)
)
Array2:
Array (
[0] => Array (
[0] => 5
[1] => 10)
[1] => Array ( [0] => 2
[1] => 4
[2] => 6)
)
I want to combine the above 2 arrays as key and value as below.
Array (
[0] => Array (
[2D Design] => 5
[3D Design & Modeling] => 10 )
[1] => Array (
[Android Developer] => 2
[Artificial Intelligence] => 4
[Web Developer] => 6 )
)
Please help me to do this. Answers will be appreciated.
use array_combine() function creates an array by using the elements from one "keys" array and one "values" array.
Note: Both arrays must have equal number of elements!
First parameter array taken as key of new array and second parameter taken as value new array .
$new_array=array();
for($i=0;$i<count($arr1);$i++)
{
$new_array[$i]=array_combine($arr1[$i],$arr2[$i]);
}
print_r($new_array);
Output :
Array
(
[0] => Array
(
[2D Design] => 5
[3D Design & Modeling] => 10
)
[1] => Array
(
[Android Developer] => 2
[Artificial Intelligence] => 4
[Web Developer] => 6
)
)
This will work,
$arr1 = array(
0 => array(
0 => "2D Design",
1 => "3D Design & Modeling"),
1 => array(0 => "Android Developer",
1 => "Artificial Intelligence",
2 => "Web Developer",
),
);
$arr2 = array(
0 => array(
0 => 5,
1 => 10,
),
1 => array(0 => 2,
1 => 4,
2 => 6,
),
);
$temp = [];
foreach ($arr1 as $k => &$v) {
foreach ($v as $k1 => &$v1) {
$temp[$k][$v1] = $arr2[$k][$k1];
}
}
print_r($temp);
I have fetched values of first array arr1 as key to temp variable and map it with values of arr2as value to temp array.
This code will work even if index i.e. 0,1,2,3 can be anything.
Here is working code.
Simply make mapped calls of array_combine(). So long as the same positioned rows have the same number of elements in them, everything will work perfectly.
Code: (Demo)
$keys =[
['2D Design', '3D Design & Modeling'],
['Android Developer', 'Artificial Intelligence', 'Web Developer']
];
$values = [
[5, 10],
[2, 4, 6]
];
var_export(
array_map('array_combine', $keys, $values)
);

Changing array key value in PHP

I have an array like below,
[test] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 13
[4] => 32
[5] => 51
)
i need to change this array into like below,
[test] => Array
(
[2] => 1
[4] => 3
[6] => 5
[8] => 13
[10] => 32
[12] => 51
)
i need to change the key value. How can i do this?.
$newArray = array_combine(
range(2,count($originalArray)*2,2),
array_values($originalArray)
);
The array_values() function returns an array containing all the values of an array and also it reset all the keys. you can do it as
$arr = array(0 => 1, 1 => 3, 2 => 5, 3 => 13, 4 => 32, 5 => 51);
$count = 1;
$tempArr = array();
foreach ($arr as $key => $val) {
$tempArr[$count * 2] = $val;
$count++;
}
var_dump($tempArr);exit;
Try this code at your side.

How to combine both array and insert into database php

if only one array is there for example
$values = array(x, y, z);
i am adding them into database like this
foreach ($values as $value)
{
$insertFunction = addValues($value);
}
my arrays:
$array1 = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 );
$array2 = Array ( fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );
But i want both array to combine and insert them into database.
How can i do this please help me
Updated:
When i am printing the POST values i am getting out put like this
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 )
Array ( [0] => fb1 [1] => or1 [2] => fb2 [3] => or2 [4] => fb3 [5] => or3 [6] => fb4 [7] => or4 [8] => fb5 [9] => or5 )
when i tried with array_merge my out put is like this
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 [10] => fb1 [11] => or1 [12] => fb2 [13] => or2 [14] => fb3 [15] => or3 [16] => fb4 [17] => or4 [18] => fb5 [19] => or5 )
How to insert them in separate columns in a table $array1 and $array2
my database table is like this
1.id
2.username
3.network_id
id is primary key
network_id values coming in array1
username values coming in array2
EDIT:
After you mentioned seperated columns I think I understand what you're looking for:
I'm assuming that array1 and array2 are in the same size.
for($i = 0; $i < count($array1); $i++)
{
$array2[$i] = (int)$array2[$i]; //"validating" the username (an integer)
mysql_query("INSERT INTO yourTableName (`username`,`network_id`) VALUES('".$array2[$i]."','".$array1[$i]."')");
}
Result:
tblName:
username: 1 2 1 ...
network_id: fb1 or1 fb2 ...
Is that what you were looking for?
Ignore this and merging:
$combined = array_merge($array1 , $array2);
//$combined = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );
I think you need array_merge.
You can use array_merge(), function to merge multiple array into one.
$arrays = array_merge($array1 , $array2);
foreach ($arrays as $value)
{
$insertFunction = addValues($value);
}
If you want associate an element from array a to an element from array b, you have to use array_combine() function.
$arrays = array_combine($array1,$array2);
foreach ($array as $aValue)
{
$insertFunction = addValues($aValue);
}

how to replace Array with number and then will be sort by number?

I have an array:
Array
(
[Asia] => Array
(
[Edisi] => 187September2001
[Hongkong] => Array
(
[Edisi] => 193Oktober2001
[1 India] => Array
(
[Edisi] => 176September2001
[2 India] => Array
(
[Edisi] => 177September2001
)
)
How can I replace the country name with number and then I will be sort with number, like:
"hongkong" => 1,
"1 India" => 2,
"2 India" => 3,
"Asia" => 4,
You can use $keys = array_keys($array) to extract the keys of your array.
This should give an array like this:
array(
[0] => Hongkong
[1] => 1 India
[2] => 2 India
)
Then generate an array for your index:
$index = range(1, count($keys));
Then combine your index and keys:
$result = array_combine($keys, $index);
This should result in:
array(
[Hongkong] => 1
[1 India] => 2
[2 India] => 3
)

Summing of all elements in the sub array - PHP

I've got the following array as $main_array .Wanted to sum up all the elements in the sub array such as [0]=>6, [1]=>11, [2]=>15.
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 2
[1] => 4
[2] => 5
)
[2] => Array
(
[0] => 8
[1] => 4
[2] => 3
)
)
Tried the following code.
foreach ($main_array as $key => $value)
$main_array[$key] = Array('1'=>array_sum($value));
print_r($main_array);
But the array structure I got was,
Array
(
[0] => Array
(
[1] => 6
)
[1] => Array
(
[1] => 11
)
[2] => Array
(
[1] => 15
)
)
I'm expecting the array structure as follows.
Array
(
[0] => 6
[1] => 11
[2] => 15
)
Thanks in advance!
When you're calling Array function you're explicitly making an array so you have to remove this from Array('1'=>array_sum($value));
This is how your code should look like
foreach ($main_array as $key => $value)
$main_array[$key] = array_sum($value);
Try this:
foreach ($main_array as $key => $value)
$main_array[$key] = array_sum($value);
That is, place the sum directly in the top level array.
Call array_sum() on every row in your input array. array_map() makes this operation expressive, concise, and doesn't require any new variables to be declared.
Code: (Demo)
$array = [
[1, 2, 3],
[2, 4, 5],
[8, 4, 3],
];
var_export(array_map('array_sum', $array));
Output:
array (
0 => 6,
1 => 11,
2 => 15,
)

Categories