how to push data into array in php - php

Dear All,
I want to push the data into array. i am using flowing code. There are two arrays. one holding keys and 2nd values. i am using flowing code
while($data=mysql_fetch_array($result))
{
foreach ($arrTemp as $val)
{
array_push($arrKeys, $val);
array_push($arrValues, $data[$val]);
}
}
print_r($arrKeys);
print_r($arrValues);
$arrReturn = array_combine($arrKeys,$arrValues);
...................................
and get flowing results of two arrays.
Array ( [0] => due_date [1] => flag_code [2] => due_date [3] => flag_code [4] => due_date [6] => flag_code )
Array ( [0] => 12:04:2011 [1] => 0 [2] => 13:04:2011 [3] => 0 [4] => 14:04:2011 [6] => 0 )
when i try to combined the array using array_combined function it only return an array of two values like: Array (due_date => 14:04:2011 flag => 0)
how i can get all values in single array.....!

Its because your have multiple the same array keys. So first it inserts due_date, then flag_code, then it will try and insert another due_date but since this already exists in the array it will overwrite it. Thus the only values left in the array will be the last pair.
The solution is to not have multiple keys that are the same in one array (your due_date and flag_code)
You could do:
foreach ($arrTemp as $val) {
$arrReturn[] = array($val => $data[$val];
}
This will give you each set of results keyed in an array like so:
$arrReturn[0] = array (due_date => 14:04:2011 flag => 0);
$arrReturn[1] = array (due_date => 14:04:2011 flag => 0);
$arrReturn[2] = array (due_date => 14:04:2011 flag => 0);
...

$ctr = 0;
foreach ($arrKeys as $id => $key) {
$res_array[$ctr][$key] = $arrValues[$id];
if ($key == 'flag_code') $ctr++;
}
print_r($res_array);
Output:
Array
(
[0] => Array
(
[due_date] => 12:04:2011
[flag_code] => 0
)
[1] => Array
(
[due_date] => 13:04:2011
[flag_code] => 0
)
[2] => Array
(
[due_date] => 14:04:2011
[flag_code] => 0
)
)

Related

PHP Array to PHP variables

I got a PHP multidimensional array, I want to create variables from it first element as variable name and the second element as the variable value. I want to use this logic to print create the variables based on the language selected, the first column always will have the same names but the second value will generate different strings based on the language selected.
Array
(
[0] => Array
(
[0] => el1
[1] => Grouping
)
[1] => Array
(
[0] => el2
[1] => Type
)
[2] => Array
(
[0] => el3
[1] => Starting Date
)
[3] => Array
(
[0] => el4
[1] => Ending Date
)
[4] => Array
(
[0] => el5
[1] => Section
)
[5] => Array
(
[0] => el6
[1] => Cell
)
[6] => Array
(
[0] => el7
[1] => Client
)
[7] => Array
(
[0] => el8
[1] => Status
)
[8] => Array
(
[0] => el9
[1] => Article
)
[9] => Array
(
[0] => el10
[1] => Search
)
)
I want to assign the [0] value as a variable name and [1] as the variable value, the declaration should be in this way related to my array presented before:
<?php
el1="Grouping";
el2="Type";
el3="Starting Date";
?>
... and so on.
I want to echo out on the HTML page the string from the variable.
Try this :
foreach ($array as $index => $subarray) {
${$subarray[0]} = $subarray[1];
}
You loop throught your big array
You get the first value with key 0 and transform it as variable : see documentation
You assign the value with key 1 and assign it to your variable
Test :
$array = array(
0 => array(
0 => "test",
1 => "value"
),
1 => array(
0 => "test2",
1 => "value2"
)
);
foreach ($array as $index => $subarray) {
${$subarray[0]} = $subarray[1];
}
var_dump($test, $test2);
The output is :
string 'value' (length=5)
string 'value2' (length=6)
A simple loop through the array should do the trick:
foreach ($data as $item) {
$temp = $item[0];
${$temp} = $item[1];
}

Combining array values in pairs without pairing with previous elements in array

i'm having trouble with combining array values. I have tried to combine them and make pairs something what i have done already but i want to erase some pairs that i do not want. Please some one to help me this happen .
// the Code I have now is:
$inputarray = array('Mussafiri', 'Fire', 'Ubungo', 'Mbezi');
$outputarray = array();
$i = 0;
foreach($inputarray as $values) {
$j = 0;
foreach($inputarray as $values2) {
if($values != $values2){
$outputarray[] = array($values => $values2);
}
$j++;
}
$i++;
}
print_r($outputarray);
//Output array is:
Array ( [0] => Array ( [Mussafiri] => Fire)
[1] => Array ( [Mussafiri] => Ubungo)
[2] => Array ( [Mussafiri] => Mbezi)
[3] => Array ( [Fire] => Mussafiri)
[4] => Array ( [Fire] => Ubungo)
[5] => Array ( [Fire] => Mbezi)
[6] => Array ( [Ubungo] => Mussafiri)
[7] => Array ( [Ubungo] => Fire)
[8] => Array ( [Ubungo] => Mbezi)
[9] => Array ( [Mbezi] => Mussafiri )
[10] => Array ( [Mbezi] => Fire)
[11] => Array ( [Mbezi] => Ubungo) )
NOTE: I DO NOT WANT THE FOLLOWING PAIRS TO APPEAR, I want element to pair with the next element and not pair with the previous element so i do not like index 3,6,7,8,9,10 and 11 to appear in the pair list i.e
[3] => Array ( [Fire] => Mussafiri)
[6] => Array ( [Ubungo] => Mussafiri)
[7] => Array ( [Ubungo] => Fire)
[8] => Array ( [Ubungo] => Mbezi)
[9] => Array ( [Mbezi] => Mussafiri)
[10] => Array ( [Mbezi] => Fire)
[11] => Array ( [Mbezi] => Ubungo)
If I understand correctly you're trying to create all unique pairs from the array, in which case something like this should work:
$pairs = array();
foreach ($inputarray as $key => $value) {
for ($i = $key + 1; isset($inputarray[$i]); $i++) {
$pairs[] = array($value => $inputarray[$i]);
}
}
The for loop sets $i as the key of the current array element + 1, so it'll never be referencing itself, and will never reference any previous array members.
for($i=0; $i<$inputarray.length;$i++){
$k=$i+1;
for($j=$k; $j<$inputarray.length;$j++){
$outputarray[] = array($inputarray[$i] => $inputarray[$j]);
}
}

How to combine inner array values in one multidimensional array?

Hi I have one multidimensional array, I retrieved the data using mysql PDO and formatted it this way
array('id'=>$row['empname'],'data'=>array(array($row['salestat'],$row['salecount'])))
Array
(
[0] => Array
(
[id] => 'employee1'
[data] => Array
(
0 => 'Sold'
1 => 1
)
)
[1] => Array
(
[id] => 'employee2'
[data] => Array
(
0 => 'On Process'
1 => 4
)
)
[2] => Array
(
[id] => 'employee2'
[data] => Array
(
0 => 'Sold'
1 => 2
)
)
)
I am trying to make my output like this, trying to format this so I can use it for drilldown series in highcharts, thank you
Array
(
[0] => Array
(
[id] => 'employee1'
[data] => Array
(
0 => 'Sold'
1 => 1
)
)
[1] => Array
(
[id] => 'employee2'
[data] => Array
[0]
(
0 => 'On Process'
1 => 4
)
[1]
(
0 => 'Sold'
1 => 2
)
)
)
Firstly, you can not use a field inside an array twice with the same key like salescount or salestat. But you can do something like this.
function wrapSameId( $employeeArray ) {
//create the new array.
$newEmployeeArray = array();
//loop the old array.
foreach ($employeeArray as $key => $value) {
$exists = 0;
$i=0;
$id = $value['id'];
$data = $value['data'];
//see if you can find the current id inside the newly created array if you do, only push the new data in.
foreach ($newEmployeeArray as $key2 => $value2) {
if( $id == $value2['id'] ) { $newEmployeeArray[$key2]['data'][] = $data;$exists = 1;var_dump($value2['data']); }
$i++;
}
//if we didnt find the id inside the newly created array, we push the id and the new data inside it now.
if( $exists == 0 ){
$newEmployeeArray[$i]['id'] = $id;
$newEmployeeArray[$i]['data'][] = $data;
}
}
return $newEmployeeArray;
}
This function should return a new Array but if in the old array there were multiple arrays with the same id, in this one we should have:
Array
(
[0] => Array
(
[id] => employee1
[data] => Array
(
[0] => Array
(
[salescount] => 4
[salesstat] => Sold
)
)
)
[1] => Array
(
[id] => employee2
[data] => Array
(
[0] => Array
(
[salescount] => 2
[salesstat] => In Progress
)
[1] => Array
(
[salescount] => 5
[salesstat] => Sold
)
)
)
)
You can use it like this:
$employees = wrapSameId( $PDOArray );
where $PDOArray is your initial Array. Hope this is what you were looking for.
Why not index your output array by id?
Loop over your initial array and add the salecount, salestat pair to an employee's data array.
$outputArray = array();
//Loop over each entry pulled from you database...
foreach ($employeeArray as $employeeInfo)
{
//If no index for the current employee exists, create an empty array
//The only reason I'm including this is because I'm not sure how array_merge handles nulls -- just to be safe
if (!$outputArray[$employeeInfo['id']])
{
$outputArray[$employeeInfo['id']] = array();
}
//Merge the existing array for that employee with the new data for the same employee
$outputArray[$employeeInfo['id']] = array_merge($outputArray[$employeeInfo['id']], $employeeInfo['data']);
}
print_r($outputArray);
It's easier to visualize the output...
Array
(
[employee1] => Array
(
0 => 1
1 => 'Sold'
)
[employee2] => Array
(
0 => 4
1 => 'On Process'
2 => 2
3 => 'Sold'
)
)
If you're not satisfied with your output being indexed by id, loop over it again and format it as you want.
$otherOutputArray = array();
foreach ($outputArray as $key => $value)
{
$otherOutputArray[] = array('id' => $key, 'data' => $value);
}
That gives you exactly what you want.

Combining arrays with common values

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'];}});

PHP resort keys from multidimensional array

i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);

Categories