I am fetching some of the datas from my database and one of my column has multiple entries from one user say col(col1) values(a,b,c). Now when I am selecting data from this database, my array looks like this,
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 1
)
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 2
)
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 3
)
Now as my array shows only fourth element of array is different.
I want to insert this data into another table which has different columns for different values means only one row corresponding to one user. Thus
col(col1) turns in to col(cola,colb,colc).
Now I want to insert like
cola -> 1,colb -> 2,colc -> 3.
But I am not able to access these values. If I select array[4] then it selects all four elements if I try this $array=array($result[4]); then it create an array of result but with same index value
Array ( [0] => 1 )
Array ( [0] => 2 )
Array ( [0] => 3 )
Array ( [0] => 4 )
I hope I am able to clarify my question.
So please suggest me some way so that I can access these values in some way.
Thanks !
EDIT
my code
while($result= mysql_fetch_array($select))
{
echo "<pre>";
print_r($result);
$array=array($result[4]);
echo "</pre>";
print_r($array);
my result
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 1
[attribute_id] => 1
[4] => suresh
[info] => suresh
)
Array ( [0] => suresh )
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 2
[attribute_id] => 2
[4] => patidar
[info] => patidar
)
Array ( [0] => patidar )
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 4
[attribute_id] => 4
[4] => e5c59af60000c8dff51e4e9a315ab152:vN
[info] => e5c59af60000c8dff51e4e9a315ab152:vN
)
Array ( [0] => e5c59af60000c8dff51e4e9a315ab152:vN )
You could make a loop first, then from there you could slice the array after the loop
<?php
$arr = array();
while($result= mysql_fetch_array($select))
{
$arr[] = $result;
}
print_r($arr);
echo $arr[0][4] ;
Related
I have 2 different arrays with different dimensions that i need to merge in order to get a result with a specific this structure:
the first:
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
)
The second:
Array
(
[0] => 2017-12-26
[1] => 2018-01-30
)
The result should be :
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] =>2017-12-26
[1] => 2018-01-30
[2] => 1
)
)
I tried using array_merge but it does not work because they have not the same dimension. Also I need an element in the second tab ([2] => 1).
<?php
$array=Array
(
0 => Array
(
0 => '2017-11-03',
1 => '2017-11-05',
2 => '1',
),
1 => Array
(
0=> '2017-11-23',
1 => '2017-11-25',
2 => '1'
),
);
$arraySmall=Array
(
0 => '2017-12-26',
1 => '2018-01-30'
);
array_push($arraySmall, "1");
array_push($array, $arraySmall);
echo'<pre>';
print_r($array);
And the output is :
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] => 2017-12-26
[1] => 2018-01-30
[2] => 1
)
)
This way can work even without this line array_push($arraySmall, "1");
You can give it a try. In order to "merge" you need same size but for "push" you don't. So if you commend the line i told you the output will look like this:
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] => 2017-12-26
[1] => 2018-01-30
)
)
What you describe is appending, not merging. Try this:
$arraySecond[] = 1; // This adds [2]=> 1
$arrayFirst[] = $arraySecond; // This adds second array to end of first
for your given example:
$x = ... // first array [2 dimensions]
$y = ... // second array [1 dimension]
$y = array_merge($y, array_diff($x[0], $y)); // add missing '1' to $y or any other key that are present in elements of $x and have to be added to $y
$x[] = $y; // append the 1-dim array as the new element in $x
I have an array like
$sele_itmid = Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 4
[4] => 4
)
$fltrd_serl_batch = Array
(
[0] => SAL121
[1] => SAL122
[2] => SAL 145
[3] => M121
[4] => M122
)
At this time i need to combine them like $sele_itmid values as keys $fltrd_serl_batch some thing like
array(
[1] => SAL121
[1] => SAL122
[1] => SAL 145
[4] => M121
[4] => M122
)
I know cant use array_combine because it wont accept duplicate keys. what is the other way to do this? Is there any array function to do this?
$itm_btch = array_map(function($key, $val) {return array($key=>$val);}, $sele_itmid, $fltrd_serl_batch);
print_r($itm_btch );
Array
(
[0] => Array
(
[1] => SAL122
)
[1] => Array
(
[1] => SAL 145
)
[2] => Array
(
[4] => M121
)
[3] => Array
(
[4] => M122
)
)
Below are my arrays echoed inside a for-each loop.
Array
(
[0] => 1
[1] => 2
[2] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 1
)
Array
(
[0] => 2
[1] => 1
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 3
)
Array
(
[0] => 3
[1] => 3
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 1
)
I would like to group these arrays based on the value of the first item (index = 0) and get the following dimension array.
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
)
Array
(
[3] => Array
(
[0] => 2
[1] => 1
[2] => 1
)
)
Array
(
[4] => Array
(
[0] => 3
[1] => 3
[2] => 1
)
[5] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[6] => Array
(
[0] => 3
[1] => 3
[2] => 2
)
)
Array
(
[7] => Array
(
[0] => 4
[1] => 2
[2] => 2
)
[8] => Array
(
[0] => 4
[1] => 2
[2] => 1
)
)
I have spent hours to figure this out but due to less experience I still cannot get this done. Please help me with some algoritme.
Won't that do the trick?
$result = [];
foreach ($originalArrays as $array) {
$result[$array[0]][] = $array;
}
I have a dataset similar to this in which I am trying to replace the numeric key values within DATA to the corresponding values in COLUMNS. I can do this in a loop but I don't think I'm doing it in the most efficient way possible. Can anyone suggest any nice functions that I haven't considered to accomplish this?
Existing Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[0] => 141627
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 18
[4] => 11
[5] => Final
)
[1] => Array
(
[0] => 140895
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 16
[4] => 10
[5] => Semi-Final
)
)
)
Desired Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[MATCHID] => 141627
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 18
[ROUNDSORT] => 11
[ROUNDNAME] => Final
)
[1] => Array
(
[MATCHID] => 140895
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 16
[ROUNDSORT] => 10
[ROUNDNAME] => Semi-Final
)
)
)
foreach ($data->DATA as $key => $array) {
$data->DATA[$key] = array_combine($data->COLUMNS, $array);
}
$data is the object you showed.
Loop trough the data and combine the keys with the data, see array_combine
$data->DATA = array_map(function (array $entry) use ($data) {
return array_combine($data->COLUMNS, $entry);
}, $data->DATA);
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
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.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.