This question already has answers here:
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
Closed 3 years ago.
I want to change the key of the multidimensional array. the array contains key like 1,15,23,45 which should be replaced by normal index key like 0,1,2,3. I tried with below code. Something is missing in below code. Please, anyone, suggest to me.
$keys = array_keys($data);
$d = 0;
foreach($data as $row){
$key_data[$d] = $data[$keys[$d]];
unset($row[$keys[$d]]);
$d++;
}
Current Output
Array
(
[15] => Array
(
[0] => Array
(
[app_dealer_id] => 15
[dealer_name] => Sharad Thombre
[shopname] => Shivshankar Fertilizer
[contact_num] => 9049121143
[district] => Parbhani
)
)
[18] => Array
(
[0] => Array
(
[app_dealer_id] => 18
[dealer_name] => Gajanan Khapre
[shopname] => Shreyas Krishi Kendra
[contact_num] => 8007791946
[district] => Parbhani
)
)
)
Expected Output:
Array
(
[0] => Array
(
[0] => Array
(
[app_dealer_id] => 15
[dealer_name] => Sharad Thombre
[shopname] => Shivshankar Fertilizer
[contact_num] => 9049121143
[district] => Parbhani
)
)
[1] => Array
(
[0] => Array
(
[app_dealer_id] => 18
[dealer_name] => Gajanan Khapre
[shopname] => Shreyas Krishi Kendra
[contact_num] => 8007791946
[district] => Parbhani
)
)
)
use array_values()
$array = array_values($array);
Output:-https://3v4l.org/cUAdl
From php.net :
array_values() returns all the values from the array and indexes the array numerically.
So just add it after your loop to reindex you array :
$keys = array_keys($data);
$d = 0;
foreach($data as $row){
$key_data[$d] = $data[$keys[$d]];
unset($row[$keys[$d]]);
$d++;
}
$newArray = array_keys($key_data);
Related
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have nested array with key & value pair. i want to convert it in single array.
/* This is current array */
Array
(
[id] => Array
(
[0] => 1
[1] => 2
)
[qty] => Array
(
[0] => 1
[1] => 1
)
[price] => Array
(
[0] => 364.41
[1] => 300
)
[amount] => Array
(
[0] => 364.41
[1] => 300
)
)
/*Now, I want this type of array*/
Array
(
[0] => Array
(
[id] => 1
[qty] => 1
[price] => 364.41
[amount] => 364.41
)
[1] => Array
(
[id] => 2
[qty] => 1
[price] => 300
[amount] => 300
)
)
I have tried array_walk and some other solutions but i could not find any proper solution
Thanks in advance
Main array is your nested array and Collection is the result you want
foreach($mainArray["id"] as $key => $value ){
$collection[$key] = ['id' => $value];
foreach(array_keys($mainArray) as $arrayKeysOfmainArray){
if(!array_key_exists($arrayKeysOfmainArray, $collection)){
$collection[$key][$arrayKeysOfmainArray] = $mainArray[$arrayKeysOfmainArray][$key];
}
}
}
print_r($collection);
$mainarray['id'] = Array(1,2,3);
$mainarray['qty'] = Array(1,1,4);
$mainarray['price'] = Array(364.41,300,500);
$mainarray['amount'] = Array(364.41,300,600);
$new = [];
foreach($mainarray as $key=> $singleArray){
if(count($singleArray) > 0){
foreach($singleArray as $childKey=> $value){
$new[$childKey][$key] = $value;
}
};
}`
I've a following associative array named $data. Here is some same key value pairs
Array
(
[0] => Array
(
[id] => 1
[config_id] => 31
[language] => "English"
)
[1] => Array
(
[id] => 2
[config_id] => 33
[language] => "English"
)
[2] => Array
(
id] => 3
[config_id] => 32
[language] => "French"
)
)
And i wanted to convert this array as
Array
(
["English"] => Array(
[0]=> Array
(
[id] => 1
[config_id] => 31
)
[1] => Array
(
[id] => 2
[config_id] => 33
)
)
["French"] =>
Array(
[0]=> Array
(
[id] => 3
[config_id] => 32
)
)
)
)
I need the language as key in the output array,Can anyone help me out to resolve this issue? Thanks in advance.
I tried the following code, but printed the last array value only
$arry = array();
foreach ($data as $val) {
$arry[$val->language]["id"] = $val->id;
$arry[$val->language]["config_id"] = $val->config_id;
}
You are nearly there, just need to make the new array all in one go and just use the $arry[$val->language][] to create a new sub array under that new or existing language key.
Also $data is an array or arrays not an array of objects so the addressing of the items was wrong.
$arry = array();
foreach ($data as $val) {
$arry[$val->language][] = ['id' => $val['id'], 'config_id' => $val['config_id']];
}
This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 5 months ago.
Problem: I want to add the values of Array2 one-after-another to a multidimensional array (array1).
Array 1:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
)
)
Array2:
Array
(
[0] => 10
[1] => 15
[2] => 25
)
Goal:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
[0] => 10
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
[0] => 15
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
[0] => 25
)
)
I am just learning programming since january 2021 so my best solution I got was this:
foreach ($array1 as $main => $value) {
foreach ($array2 as $sec => $entry) {
$array1[$main][] = $array2[$sec];
}
}
print_r($array1);
Output:
Array
(
[0] => Array
(
[date] => 2020-01-01
[itemsSold] => 25.00000000
[0] => 10
[1] => 15
[2] => 25
)
[1] => Array
(
[date] => 2020-01-02
[itemsSold] => 50.00000000
[0] => 10
[1] => 15
[2] => 25
)
[2] => Array
(
[date] => 2020-01-03
[itemsSold] => 25.00000000
[0] => 10
[1] => 15
[2] => 25
)
)
What am I doing wrong in my loop?? How can I get the values of array2 one-by-one into array1??
Thanks for any help ;)
The issue with your code is that you have nested a loop within another when you do not need to. Instead, use the loop format foreach ($array1 as $key => $value) so you can capture its index in $key, then use that as an index to $array2.
foreach ($array1 as $key => $value) {
// Append from $array2 by its index $key
// Using [] will append at index [0]
// You can modify $array1 inside the loop if you
// target it by $key. Notice that we are not using
// $value at all in this loop - only the $key matters.
$array1[$key][] = $array2[$key];
}
print_r($array1);
foreach also allows you to modify the value directly if you use a & reference. This is a little bit shorter:
foreach ($array1 as $key => &$value) {
// Looping over references to $array1 sub-arrays
// means that $value can be modified directly
$value[] = $array2[$key];
}
print_r($array1);
Iteration by reference is described in the PHP foreach manual
This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 1 year ago.
I have read a lot of answers here on SO but havent been able to sort this out.
I have multidimensional array that looks like this:
Array
(
[0] => Array
(
[0] =>
[1] => 655
)
[1] => Array
(
[0] => IT-82
[1] => 14
)
[2] => Array
(
[0] => IT-21
[1] => 5
)
[3] => Array
(
[0] => IT-82
[1] => 7
)
[4] => Array
(
[0] =>
[1] => 3
)
[5] => Array
(
[0] => IT-21
[1] => 4
)
[6] => Array
(
[0] =>
[1] => 3
)
[7] => Array
(
[0] => IT-21
[1] => 3
)
[8] => Array
(
[0] => IT-72
[1] => 7
)
[9] => Array
(
[0] => IT-75
[1] => 22
)
[10] => Array
(
[0] => IT-75
[1] => 3
)
)
I would like to sum the values according to the keys ending with a single array like:
Array
(
=> 661
IT-82 => 21
IT-21 => 12
IT-82 => 12
IT-72 => 7
IT-75 => 25
)
Tried with
foreach ($array as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
but this only returned the sum of all the values.
Any help appreciated.
Try:
$sumArray = array();
foreach ($array as $k=>$subArray) { //loop through array
if(isset($sumArray[$subArray[0]]))
$sumArray[$subArray[0]] += $subArray[1]; // set 0th index as key and 1st as value and add value to current index
else
$sumArray[$subArray[0]] = $subArray[1];
}
print_r($sumArray);
Output:
Array
(
[] => 661
[IT-82] => 21
[IT-21] => 12
[IT-72] => 7
[IT-75] => 25
)
I suppose it should be:
foreach ($array as $subArray) {
$sumArray[$subArray[0]] += $subArray[1];
}
This question already has answers here:
How to group subarrays by a column value?
(20 answers)
Closed 5 months ago.
I have this result from a foreach loop.
I tried looping through the array using foreach from the answers in StackOverflow, but I'm having trouble when doing it under another foreach loop.
Array
(
[0] => Array
(
[referenceUid] => FF875951-87CB-942F-84A2-46C620BF07C8
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 300.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
What I want is to merge the array with duplicate values on referenceUid column. Something like this:
Array
(
[0] => Array
(
[referenceUid] => FF875951-87CB-942F-84A2-46C620BF07C8
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 300.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
[1] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
You can construct a new (merged) array and loop your input to assemble the new structure.
An important consideration is to use the common key (referenceUid) as the array key in your new array so you can reference it easily. If you don't want it at the end, simply reset the array keys e.g. $out = array_values($out).
Here's an example:
$output = array();
foreach ($input as $values) {
$key = $values['referenceUid'];
$output[$key][] = $values;
}
// Don't want the referenceUid in the keys? Reset them:
$output = array_values($output);
Example
//both arrays will be merged including duplicates
$result = array_merge( $array1, $array2 );
//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
//array is sorted on the bases of id
sort( $result );`