I have an array look like this via CSV:
Array
(
[0] => Array
(
[0] => Agent Name
[1] => Total Calls
[2] => Customer Services
[3] => Voicemail
)
[1] => Array
(
[0] => Paul
[1] => 53
[2] => 0
[3] => 0
)
[2] => Array
(
[0] => Sarah
[1] => 51
[2] => 0
[3] => 0
)
)
I would like to tidy an array to make it to more readable and easy to use.
I want an array to look something like this:
Array
(
[Paul] => Array
(
[Agent Name] => Paul
[Total Calls] => 53
[Customer Services ] => 30
[Voicemail ] => 0
)
[Sarah] => Array
(
[Agent Name] => Sarah
[Total Calls] => 51
[Customer Services ] => 0
[Voicemail ] => 0
)
)
I have came up with this solution and it work:
$fields = $report[0];
array_shift($report);
$data = array();
foreach($report as $row) {
$data[$row[0]] = array();
foreach($row as $k => $val) {
$data[$row[0]][$fields[$k]] = $val;
}
}
return $data;
Is there a better shorter way doing this?
Try
$result =array();
for($i=1;$i<count($arr);$i++){
$result[$arr[$i][0]] = array_combine(array_values($arr[0]),$arr[$i]);
}
See demo here
You could perhaps do it like this:
$f = array_shift($report);
$a = array_combine(array_column($t = array_map(function ($r) use ($f)
{
return array_combine($f, $r);
}
, $report), 'Agent Name'), $t);
Related
This question already has answers here:
how to change index of an array in php [duplicate]
(3 answers)
Closed 4 years ago.
Good day everyone,
I was tasked to change index in arrays.
here is my code:
$file = Storage::get('upload/test.txt');
$lines = explode('\n', $file);
$array = array_map(function($line) {
return explode(',', $line);
}, $lines);
print_r($array);
output is:
Array
(
[0] => Array
(
[0] => john
[1] => male
[2] => 20
[3] => 200
[4] => 174
)
[1] => Array
(
[0] => joe
[1] => male
[2] => 24
[3] => 157
[4] => 166
)
[2] => Array
(
[0] => bea
[1] => female
[2] => 18
[3] => 153
[4] => 160
)
[3] => Array
(
[0] => edd
[1] => male
[2] => 30
[3] => 180
[4] => 180
)
)
what i need to happen is:
Array
(
[0] => Array
(
[name] => john
[sex] => male
[age] => 20
[height] => 200
[weight] => 174
)
[1] => Array
(
[name] => joe
[sex] => male
[age] => 24
[height] => 157
[weight] => 166
)
[2] => Array
(
[name] => bea
[sex] => female
[age] => 18
[height] => 153
[weight] => 160
)
[3] => Array
(
[name] => edd
[sex] => male
[age] => 30
[height] => 180
[weight] => 180
)
)
thanks in advance! :)
You want an associative array.
$newArray = []; // create a new empty array to store your associative arrays.
// Loop through each element in array.
foreach($array as $aPerson) {
// map each element in array into an associative array.
$person = [
"name" => $aPerson[0],
"sex" => $aPerson[1],
"age" => $aPerson[2],
"height" => $aPerson[3],
"weight" => $aPerson[4]
];
// Add your associative array to your new re-indexed array.
array_push($newArray, $person);
}
I hope this helps.
The following should print the array in the format you want.
print_r($newArray);
Simple, Like this:
$arr = [
['joe','male',24,157,166]
];
#mind the & pass by refrence
foreach($arr as &$item){
$item = array_combine(['name','sex','age','height','weight'],$item);
}
print_r($arr);
Output
Array
(
[0] => Array
(
[name] => joe
[sex] => male
[age] => 24
[height] => 157
[weight] => 166
)
)
Sandbox
Note array_combine will blow up if the 2 arrays are not the same size.
You can do this too (84 bytes)
$arr = [['joe','male',24,157,166]];
$headers = ['name','sex','age','height','weight'];
$arr = array_map(function($item)use($headers){return array_combine($headers,$item);},$arr);
print_r($arr);
Same output
Sandbox
A possible solution will be to create a new array, loop through the current one and add the indexes:
$new_array = array();
foreach ($array as $element) {
$new_array[] = array(
'name' => $element[0],
'sex' => $element[1],
'age' => $element[2],
'height' => $element[3],
'weight' => $element[4]
);
$i++;
}
Here is how I would do what you're trying to achieve
<?php
$fp = #fopen('upload/test.txt', 'r'); $props = ['name', 'sex', 'age', 'height', 'weight']; $r = [];
if($fp) {
while(($line = fgets($fp)) !== false) {
$x = explode(',', $line); $a = [];
foreach($props as $k => $v){
$a[$v] = $x[$k];
}
$r[] = $a;
}
fclose($fp);
}
print_r($r);
?>
I am looking to convert a text based DAT file into an array in PHP. Normally I would be able to read each line and explode it into an array but this file is different.
[0]
FirstRideOn=43169.5701090972
Laps=4591
LastRideOn=43224.7924173611
Name=Standard 1
Nr=1
ResetDate=0
RunningTime=2481
Runs=233
TranNr=7435191
[1]
FirstRideOn=43149.5406271644
Laps=5528
LastRideOn=43224.7616565972
Name=Standard 2
Nr=2
ResetDate=0
RunningTime=2957
Runs=292
TranNr=8377256
I was hoping to load it into an associative array.
Any feedback or suggestions are greatly appreciated!
It's not clear from your question exactly what format you want the array to be in. So here are two alternatives. First we read the file into an array:
$input= file('somefile.dat', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Then we can process the array to make an associative array. Here's the first option:
$output = array();
foreach ($input as $line) {
if (preg_match('/^\[(\d+)\]$/', $line, $matches)) {
$entry = (int)$matches[1];
$output[$entry] = array();
}
else {
list($key, $value) = explode('=', $line);
$output[$entry][$key] = $value;
}
}
print_r($output);
Which generates this output:
Array
(
[0] => Array
(
[FirstRideOn] => 43169.5701090972
[Laps] => 4591
[LastRideOn] => 43224.7924173611
[Name] => Standard 1
[Nr] => 1
[ResetDate] => 0
[RunningTime] => 2481
[Runs] => 233
[TranNr] => 7435191
)
[1] => Array
(
[FirstRideOn] => 43149.5406271644
[Laps] => 5528
[LastRideOn] => 43224.7616565972
[Name] => Standard 2
[Nr] => 2
[ResetDate] => 0
[RunningTime] => 2957
[Runs] => 292
[TranNr] => 8377256
)
)
Or we can do it this way:
// alternate style
$output = array();
foreach ($input as $line) {
if (preg_match('/^\[(\d+)\]$/', $line, $matches)) {
$entry = (int)$matches[1];
}
else {
list($key, $value) = explode('=', $line);
if (!array_key_exists($key, $output)) $output[$key] = array();
$output[$key][$entry] = $value;
}
}
print_r($output);
which generates this output. The choice is yours!
Array
(
[FirstRideOn] => Array
(
[0] => 43169.5701090972
[1] => 43149.5406271644
)
[Laps] => Array
(
[0] => 4591
[1] => 5528
)
[LastRideOn] => Array
(
[0] => 43224.7924173611
[1] => 43224.7616565972
)
[Name] => Array
(
[0] => Standard 1
[1] => Standard 2
)
[Nr] => Array
(
[0] => 1
[1] => 2
)
[ResetDate] => Array
(
[0] => 0
[1] => 0
)
[RunningTime] => Array
(
[0] => 2481
[1] => 2957
)
[Runs] => Array
(
[0] => 233
[1] => 292
)
[TranNr] => Array
(
[0] => 7435191
[1] => 8377256
)
)
I want to combine multiple arrays output in single array. Below is the array I am getting when I do this.
print_r($getData_milestone);
I have arrays as below:
[milestone] => Array
(
[0] => milestone 1
[1] => milestone 2
[2] => milestone 3
)
[date] => Array
(
[0] => 10/25/2015
[1] => 10/30/2015
[2] => 11/25/2015
)
[status] => Array
(
[0] => 1
[1] => 1
[2] => 0
)
And I want to get output such as below:
Array
(
[0] => Array
(
[milestone] => milestone 1
[date] => 10/25/2015
[status] => 1
)
[1] => Array
(
[milestone] => milestone 2
[date] => 10/30/2015
[status] => 1
)
[2] => Array
(
[milestone] => milestone 3
[date] => 11/25/2015
[status] => 0
)
)
I have tried by this code
foreach($getData_milestone['milestone'] as $miledata)
{
$allDatamile[$i]=$getData_milestone;
$allDatamile[$i]=$getData_milestone['date'];
$allDatamile[$i]=$getData_milestone['status'];
$i++;
}
Try this out, and let me know the result. It should work.
I am considering the given array as an associative array with keys "milestone", "date" and "status" .. Correct me if I'm wrong.
$outputArray = array();
foreach($givenArray['milestone'] as $key=>$val){
$outputArray[$key]['milestone'] = $val;
$outputArray[$key]['date'] = $givenArray['date'][$key];
$outputArray[$key]['status'] = $givenArray['status'][$key];
}
print_r($outputArray)
try this,
$a["milestone"][] = "milestone 1";
$a["milestone"][] = "milestone 2";
$a["milestone"][] = "milestone 3";
$a["date"][] = "10/25/2015";
$a["date"][] = "10/30/2015";
$a["date"][] = "11/25/2015";
$a["status"][] = "1";
$a["status"][] = "1";
$a["status"][] = "0";
foreach ($a['milestone'] as $key => $val) {
$a1[$key]["milestone"] = $val;
$a1[$key]["date"] = $a['date'][$key];
$a1[$key]["status"] = $a['status'][$key];
}
output is
Array
(
[0] => Array
(
[milestone] => milestone 1
[date] => 10/25/2015
[status] => 1
)
[1] => Array
(
[milestone] => milestone 2
[date] => 10/30/2015
[status] => 1
)
[2] => Array
(
[milestone] => milestone 3
[date] => 11/25/2015
[status] => 0
)
)
array_column (PHP 5 >= 5.5.0) might help -
$keys = array_keys($arr);
// if the number of element increases(to make it more dynamic)
$count = count($arr['milestone']);
$i= 0;
while($i < $count) {
$new[] = array_column($arr, $i);
$i++;
}
foreach($new as $k => $n) {
$new[$k] = array_combine($keys, $n);
}
var_dump($new);
DEMO
Try it out the bellow code
$out= array();
$milestone=array
(
"milestone 1",
"milestone 2",
"milestone 3"
);
$m_date=array
(
"10/25/2015",
"10/25/2015",
"10/25/2015"
);
$status=array
(
0,1,1
);
for($i=0;$i<count($milestone);$i++){
$comArray=array
(
"milestone" => $milestone[$i],
"date" => $m_date[$i],
"status" => $status[$i]
)
$out[]=$comArray;
}
Hope it will solve your problem.
I have an array of dates that looks like this:
Array
(
[0] => '2014-01-01'
[1] => '2014-01-02'
[2] => '2014-01-03'
[3] => '2014-01-04'
[4] => '2014-01-05'
[5] => '2014-01-06'
[6] => '2014-01-07'
)
and I have another array of dates and counts that looks like this:
[All] => Array
(
[0] => Array
(
[count] => 2
[date] => 2014-01-06
)
[1] => Array
(
[count] => 1
[date] => 2014-01-03
)
[2] => Array
(
[count] => 43
[date] => 2013-12-11
)
[3] => Array
(
[count] => 103
[date] => 2013-12-10
)
[4] => Array
(
[count] => 128
[date] => 2013-12-09
)
[5] => Array
(
[count] => 75
[date] => 2013-12-08
)
[6] => Array
(
[count] => 107
[date] => 2013-12-07
)
I want to make a new associative array where all the keys are the dates from the first array above and all of the values are either the count matched up with the corresponding date or "0".
So for instance, the new array would look like this:
Array
(
[2014-01-01] => 0
[2014-01-02] => 0
[2014-01-03] => 1
[2014-01-04] => 0
[2014-01-05] => 0
[2014-01-06] => 2
[2014-01-07] => 0
)
Does that make sense? Please feel free to ask any questions you may have. Thank you!
Try this code:
$result = array();
foreach($firstArray as $f){
foreach($secondArray as $s){
if($s['date'] == $f) $result[$f] = $s['count'];
}
if(!array_key_exists($f, $result)) $result[$f] = 0;
}
$result = array();
foreach($secondArray as $s){
if(in_array($s['date'], $firstArray) {
unset($firstArray[$s['date']]);
$result[$s['date']] = $s['count'];
}
}
// if items left in first array that are not found within foreach:
if (!empty($firstArray))
$result = array_merge($result, array_fill_keys($firstArray, 0));
// sort by key so dates go ascending
ksort($result);
$new = array();
foreach($all as $row)
{
$new[$row['date']] = $row['count'];
}
array_merge ($new, $old);
Here $all is the array with the date and count indices.
$old is the exisisting array.
That's a 2-liner:
// use dates as index and set everything to 0
$result = array_fill_keys($x, 0));
// copy over existing counts
array_walk($all, function($v) use (&$result) { if (array_key_exists($v['date'], $result)) { $result[$v['date']] = $v['count'];}});
I have a new PHP problem. I have 2 arrays, and I want a third array which is a combination of the first 2. The first array, $arr1, is something like this:
Array (
[0] => name [1] => age
)
The second array, $arr2, is something like this:
Array (
[0] => Array( [0] => Dave [1] => 20 )
[1] => Array( [0] => Steve [1] => 25 )
[2] => Array( [0] => Ace [1] => 23 )
)
And my idea is to create a new array, called $arr3, which should like this:
Array (
[0] => Array( [name] => Dave [age] => 20 )
[1] => Array( [name] => Steve [age] => 25 )
[2] => Array( [name] => Ace [age] => 23 )
)
Can anyone tell me how to do this?
$arr3 = array();
foreach ($arr2 as $person) {
$arr3[] = array_combine($arr1, $person);
}
foreach($arr2 as $subArray){
foreach($subArray as $i=>$val){
$arr3[$arr1[$i]] = $val;
}
}