I have one array and try to change some key and value for example if sku are same than i need to merge image. Below array i have
Array
(
[0] => Array
(
[sku] => h-eldora
[name] => H ELDORA
[image] => s/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054
)
[1] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
)
[2] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[3] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
)
[4] => Array
(
[sku] => hl-dracy
[name] =>
[image] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
)
[5] => Array
(
[sku] => hl-dracy
[name] =>
[image] =>s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
I need to merge array like this
Array
(
[0] => Array
(
[sku] => h-eldora
[name] =>
[image1] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
[image2] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
[image3] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[1] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image1] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
[image2] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
[image3] => s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
If any php function is there than please let me know or any code suggestion
Using simple PHP:
<?php
$arr1 = array(
0 => array(
'sku' => 'h-eldora',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054'
),
1 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221'
),
2 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598'
),
3 => array(
'sku' => 'hl-dracy',
'name' => 'HL DRACY',
'image' => 's/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222'
),
4 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223'
),
5 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793'
)
);
$newArr = $imgIndex = array();
foreach($arr1 as $a){
if( !array_key_exists($a['sku'],$newArr) ){
$newArr[$a['sku']] = array(
'sku' => $a['sku'],
'name' => $a['name'],
'image1' => $a['image']
);
$imgFound[$a['sku']] = 1;
}else{
$imgFound[$a['sku']]++;
$newArr[$a['sku']]['image'.$imgFound[$a['sku']]] = $a['image'];
}
}
unset($imgFound);
echo '<pre>'; print_r($newArr); echo '</pre>';
?>
This could work for you:
$data = // your input array
$uniqueSKUs = Array();
$newArray = Array();
$currentIndex = -1;
foreach ($data as $item) {
if (!in_array($item['sku'], $uniqueSKUs)) {
$currentIndex++;
$uniqueSKUs[] = $item['sku'];
$newArray[$currentIndex] = Array(
'sku' => $item['sku'],
'name' => $item['name']
);
}
$newArray[$currentIndex]['images'][] = $item['image'];
}
echo "<pre>";
var_dump($newArray);
echo "</pre>";
Related
I want to expand my city array with post code value.
If the city_postcode array contain city array name record then push postcode value into city array. That's what i want to achive somehow.
city array:
Array
(
[0] => Array
(
[id] => 1
[city] => Budapest
[population] => 1700000
)
[1] => Array
(
[id] => 2
[city] => Szeged
[population] => 160000
)
)
city_postcode array:
Array
(
[0] => Array
(
[name] => Budapest
[post_code] => 12345
)
[1] => Array
(
[name] => Szeged
[post_code] => 33356
)
)
The result I want:
Array
(
[0] => Array
(
[id] => 1
[city] => Budapest
[population] => 1700000
[post_code] => 12345
)
[1] => Array
(
[id] => 2
[city] => Szeged
[population] => 160000
[post_code] => 33356
)
)
If you can rely on the cities and post codes to be equal in length and sorted, you could just do something like this:
<?php
$cities = array(
array("id"=>1,"city"=>"Budapest","Population"=>"1700000"),
array("id"=>2,"city"=>"Szeged","Population"=>"160000")
);
$cityPostCode = array(
array("name"=>"Budapest","post_code"=>12345),
array("name"=>"Szeged","post_code"=>33356)
);
for($i = 0; $i < count($cities); $i++){
$cities[$i]['post_code'] = $cityPostCode[$i]['post_code'];
}
print_r($cities);
Other wise you could do something more dyanmic like:
function _parsePostCode($targetName,$targetArr){
foreach($targetArr as $el){
if($el['name'] == $targetName){
return $el['post_code'];
}
}
return false;
}
for($i = 0; $i < count($cities); $i++){
$cities[$i]['post_code'] = _parsePostCode($cities[$i]['city'],$cityPostCode);
}
print_r($cities);
As an alternative you can use 'reference' PHP in foreach loop as follows
$city = array(
0 => array(
'id' => 1,
'city' => "Budapest",
'population' => 1700000
),
1 => array(
'id' => 2,
'city' => "Szeged",
'population' => 160000
)
);
$city_postcode = array(
0 =>array(
'name' => 'Budapest',
'post_code' => 12345
),
1 => array(
'name' => 'Szeged',
'post_code' => 33356
)
);
foreach ($city as $ckey => &$cval) {
$cval['post_code'] = $city_postcode[$ckey]['post_code'];
}
unset($cval);
var_dump($city);
You can use this simple approach :
<?php
$city = array(
0 => array(
'id' => 1,
'city' => "Budapest",
'population' => 1700000
),
1 => array(
'id' => 2,
'city' => "Szeged",
'population' => 160000
)
);
$city_postcode = array(
0 => array(
'name' => 'Budapest',
'post_code' => 12345
),
1 => array(
'name' => 'Szeged',
'post_code' => 33356
)
);
function apply_post_code(&$item, $key, $postcode)
{
$item['post_code'] = $postcode[ $key ][ 'post_code' ];
}
array_walk($city, 'apply_post_code', $city_postcode);
var_dump($city);
Note that the $city variable has been passes by reference
I want to modify the existing array to a particular format please see the below array what i have and what i want
I have array as :
Array
(
[0] => Array
(
[block_id] => 1
[title] => Test1
[identifier] => test1
[content] => some test data
[creation_time] => 2019-09-03 09:47:35
[update_time] => 2019-09-03 09:47:35
[is_active] => 1
)
[1] => Array
(
[block_id] => 2
[title] => test2
[identifier] => twst2
[content] => dfdsffsdfsdfsfsdf
[creation_time] => 2019-09-03 09:48:03
[update_time] => 2019-09-03 09:48:03
[is_active] => 1
)
)
And I want this array as :
$options = [
['value' => 'test1', 'label' => __('Test1')],
['value' => 'test2', 'label' => __('Test2')],
['value' => 'test3', 'label' => __('Test3')],
['value' => 'test4', 'label' => __('Test4')],
['value' => 'test5', 'label' => __('Test5')],
['value' => 'test6', 'label' => __('Test6')]
];
Try this Solution.
$data = Array ( Array
(
'block_id' => 1,
'title' => 'Test1'
), Array
(
'block_id' => 2,
'title' => 'test2'
)
);
foreach($data as $k => $val){
$options[$k]['value'] = $val['title'];
$options[$k]['label'] = '__("'.ucfirst($val['title']).'")';
}
echo "<pre>";
print_r( $options);
Expected Result is
Array
(
[0] => Array
(
[value] => Test1
[label] => __("Test1")
)
[1] => Array
(
[value] => test2
[label] => __("Test2")
)
)
If $array is your array, then
foreach ($array as $k => $v)
{
$options[] = [ 'value' => $v['identifier'], 'label' => "__('" . $v['title'] . "')"];
}
If you are trying "to modify the existing array to a particular format" , next approach may help. When you precede $value with &, the $value will be assigned by reference and you can directly modify it.
<?php
foreach($array as &$value) {
$value = array(
'value' => $value["identifier"],
'label' => "__('".$value["title"]."')"
);
};
unset($value);
?>
Try this Solution.
foreach ($array as $k => $val)
{
$options[] = [ 'val' => $val['identifier'], 'label' => "__('" . $val['title'] . "')"];
}
I've tried to loop of following array.
Array
(
[mech_info] => Array
(
[make] => Amaka
[0] => Array
(
[year] => 2001
[model] => Array
(
[0] => Test one
[1] => test fix
[2] => Hamour
[3] => Imagica
)
)
[1] => Array
(
[year] => 2002
[model] => Array
(
[0] => Test Two
)
)
[2] => Array
(
[year] => 2014
[model] => Array
(
[0] => Test three
)
)
[3] => Array
(
[year] => 2015
[model] => Array
(
[0] => test four
)
)
)
)
Array
(
[mech_info] => Array
(
[make] => PRI
[0] => Array
(
[year] => 2005
[model] => Array
(
[0] => PRIMODE
[1] => Temp Pri
[2] => primode
[3] => yyy
)
)
)
)
I want to do it with foreach loop. I have tried by following code but it is not show anything except
`print_r($_POST['mech_show']);`.
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model_data as $key => $mec_value) {
echo "string";
echo $meta_value['make'];
}
echo "<pre>";
print_r($_POST['mech_show']);
exit();
also not able to go under foreach and data not print in loop.
given me error
Notice: Undefined index: mech_info
Warning: Invalid argument supplied for foreach() in
i also trie this way but
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $_POST['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
echo "<pre>";
print_r($all_make_model['mech_info']);
but it's showing Warning: Illegal string offset 'mech_info' in ..
I don't know if my code is wrong or I'm missing something anyone pls help me.
Thank You
Some change your foreach loop. it $meta_value['make'] should be $mec_value['make']
So,
$all_make_model= $_POST['mech_show'];
//$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
try
$all_array=array("mech_info"=>array("make"=>"Amaka",array("year"=>2001,"model"=>array("one","two","three")),array("year"=>2002,"model"=>array("one","two","three")),array("year"=>2003,"model"=>array("one","two","three")),array("year"=>2004,"model"=>array("one","two","three"))),array("mech_info"=>array("make"=>"PRI",array("year"=>2001,"model"=>array("one","two","three")))));
$all_make_model= $all_array;
//$all_make_model_data = $all_make_model['mech_info'];
//print_r($all_make_model['mech_info']);
foreach ($all_make_model['mech_info'] as $key => $mec_value) {
if(is_numeric($key)) continue;
echo $mec_value; // output Amaka
}
exit();
This Code just work.
To iterate on multiple mech_info, i added a leave in the array, because otherwise you are tring to create multiple object with the same index.
$p = Array('mech_show' => Array(
0 => Array(
'mech_info' => Array(
'make' => 'Amaka',
'0' => Array(
'year' => 2001,
'model' => Array(
0 => 'Test one',
1 => 'test fix',
2 => 'Hamour',
3 => 'Imagica'
)
),
'1' => Array(
'year' => 2002,
'model' => Array(
0 => 'Test Two'
)
),
'2' => Array(
'year' => 2014,
'model' => Array(
0 => 'Test three'
)
),
'3' => Array(
'year' => 2015,
'model' => Array
(
0 => 'test four'
)
)
)
),
1=>Array(
'mech_info' => Array(
'make' => 'PRI',
'0' => Array(
'year' => 2005,
'model' => Array(
0 => 'PRIMODE',
1 => 'Temp Pri',
2 => 'primode',
3 => 'yyy'
)
)
)
)
)
);
$all_make_model= $p['mech_show'];
foreach($all_make_model as $all_make_model_data){
foreach($all_make_model_data as $mech_info)
var_dump($mech_info['make']);
}
where you have to replace $p with $_POST
I'm needing to rearrange Array 1 by the order of how the [id] (oper-%%%%%%%%%%) appears within Array 2, and I'm not quite sure as to how to approach this.
Array 1
Array ( [0] => Array ( [id] => oper-4c597644-402490ee [amount] => 17498.5 ) [1] => Array ( [id] => oper-4f30019a-27f27473 [amount] => 10383.5 ) [2] => Array ( [id] => oper-4bceffd1-21e0af5b [amount] => 6277 ) [3] => Array ( [id] => oper-4f300d33-de9592e3 [amount] => 11382 ) [4] => Array ( [id] => oper-4c236420-0b11e945 [amount] => 14759 ) [5] => Array ( [id] => oper-50f6e4ad-9effbec7 [amount] => 3058 ) [6] => Array ( [id] => oper-4f05a90b-03b379f9 [amount] => 12112.5 ) [7] => Array ( [id] => oper-qtgjvw8y-1uqtw058 [amount] => 10023 ) [8] => Array ( [id] => oper-52657816-3d6516e2 [amount] => 3495 ) )
Array 2
Array ( [0] => Array ( [0] => Bob [1] => oper-4bceffd1-21e0af5b ) [1] => Array ( [0] => Bob [1] => oper-4c236420-0b11e945 ) [2] => Array ( [0] => Bob [1] => oper-4c597644-402490ee ) [3] => Array ( [0] => Bob [1] => oper-4f05a90b-03b379f9 ) [4] => Array ( [0] => Bob [1] => oper-4f30019a-27f27473 ) [5] => Array ( [0] => Bob [1] => oper-4f300d33-de9592e3 ) [6] => Array ( [0] => Bob [1] => oper-50f6e4ad-9effbec7 ) [7] => Array ( [0] => Bob [1] => oper-52657816-3d6516e2 ) [8] => Array ( [0] => Bob [1] => oper-qtgjvw8y-1uqtw058 ) [9] => Array ( [0] => Empty [1] => ) [10] => Array ( [0] => Upg. [1] => ) [11] => Array ( [0] => Ren. [1] => ) )
Bob is just an example "name"
Here is my code so far (And I know this isn't clean because this isn't done with PDO, I'm just trying to get this to work at the moment):
$result = mysql_query("SELECT * FROM tblOperatorGoals WHERE MonthlyGoal LIKE '$currentDate%'");
while ($row = mysql_fetch_array($result)) {
$opernameArray[] = $row['OperatorName'];
$operIDArray[] = $row['OperatorID'];
$monthlyGoal[] = substr($row['MonthlyGoal'], 8);
$operArray[] = array('name' => $row['OperatorName'], 'id' => $row['OperatorID']);
}
$result = mysql_query("SELECT * FROM tblUserPayments WHERE ChargeAmount IS NOT NULL AND PaymentStatus='OK' AND PaymentDate LIKE '$currentDate%'");
while ($row = mysql_fetch_array($result)) {
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
}
foreach ($operearnedArray as $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
if(array_key_exists($value['id'], $operSums1)) {
$operSums1[$value['id']] += $value['amount'];
} else {
$operSums1[$value['id']] = $value['amount'];
}
}
foreach ($operSums1 as $id => $value) {
if (in_array($id,$operIDArray)) {
$operSums[] = array(
'id' => $id,
'amount' => $value);
}
}
Any help is greatly appreciated. Associative arrays are just not my cup of tea.
Basically I'm looking for an Array that looks like this:
Array ( [name] => Bob [id] => oper-%%%%%%%%%%%%%% [amount] => $$$$$$$$$$$ )...ect...ect...
Crude but should work:
$toSort = array(
array ('id' => 'oper-4c597644-402490ee', 'amount' => 17498.5,),
array ('id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5,),
array ('id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277,),
array ('id' => 'oper-4f300d33-de9592e3', 'amount' => 11382,),
array ('id' => 'oper-4c236420-0b11e945', 'amount' => 14759,),
array ('id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058,),
array ('id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5,),
array ('id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023,),
array ('id' => 'oper-52657816-3d6516e2', 'amount' => 3495,),
);
$assigned = array (
array ('Bob','oper-4bceffd1-21e0af5b'),
array ('Bob','oper-4c236420-0b11e945'),
array ('Bob','oper-4c597644-402490ee'),
array ('Bob','oper-4f05a90b-03b379f9'),
array ('Bob','oper-4f30019a-27f27473'),
array ('Bob','oper-4f300d33-de9592e3'),
array ('Bob','oper-50f6e4ad-9effbec7'),
array ('Bob','oper-52657816-3d6516e2'),
array ('Bob','oper-qtgjvw8y-1uqtw058'),
);
$sorted = assignData($toSort, $assigned);
var_dump($sorted);
function assignData($raw, $order)
{
$returnArray = array();
foreach($order as $entry):
foreach($raw as $rawEntry):
if ($rawEntry['id'] == $entry[1]):
$temp = array(
'name' => $entry[0],
'oper' => $rawEntry['id'],
'amount' => $rawEntry['amount'],
);
$returnArray[] = $temp;
endif;
endforeach;
endforeach;
return $returnArray;
}
Try this.
<?php
$data = [
['id' => 'oper-4c597644-402490ee', 'amount' => 17498.5],
['id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5],
['id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277],
['id' => 'oper-4f300d33-de9592e3', 'amount' => 11382],
['id' => 'oper-4c236420-0b11e945', 'amount' => 14759],
['id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058],
['id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5],
['id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023],
['id' => 'oper-52657816-3d6516e2', 'amount' => 3495]
];
$names = [
['BobA', 'oper-4bceffd1-21e0af5b'],
['BobB', 'oper-4c236420-0b11e945'],
['BobC', 'oper-4c597644-402490ee'],
['BobD', 'oper-4f05a90b-03b379f9'],
['BobE', 'oper-4f30019a-27f27473'],
['BobF', 'oper-4f300d33-de9592e3'],
['BobG', 'oper-50f6e4ad-9effbec7'],
['BobH', 'oper-52657816-3d6516e2'],
['BobI', 'oper-qtgjvw8y-1uqtw058'],
['Empty', ''],
['Upg.', ''],
['Ren.', '']
];
$idMappedNames = array_column($names, 0, 1);
$summary = array_reduce($data, function($carry, $item) use(&$idMappedNames) {
if (!array_key_exists($item['id'], $carry)) {
$carry[$item['id']] = [
'name' => $idMappedNames[$item['id']],
'amount' => 0
];
}
$carry[$item['id']]['amount'] += $item['amount'];
return $carry;
}, []);
You can connect these information it in just single SQL Query
$result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal
FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID
WHERE a.ChargeAmount IS NOT NULL
AND a.PaymentStatus='OK'
AND a.PaymentDate LIKE '$currentDate%'
AND u.MonthlyGoal LIKE '$currentDate%' " );
while ($row = mysql_fetch_assoc($result)) {
$wantedArray[] = array(
'OperatorName' => $row['OperatorName'],
'MonthlyGoal'=>$row['MonthlyGoal'],
'ChargeAmount' => $row['ChargeAmount'],
'OperatorID' => $row['OperatorID']);
}
But as the question originally asked goes:
$desiredArray = array();
$id_to_amount = array();
foreach($array1 as $x)
$id_to_amount[$x['id']] = $x['amount'];
foreach($array2 as $x)
{
$id = $x[1];
$name = $x[0];
$amount = -1;
if(isset($id_to_amount[$id]))
$amount = $id_to_amount[$id];
$desiredArray[] = array('id'=>$id,'name'=>$name,'amount'=>$amount );
}
I'm needing to rearrange Array 1 by the order of how the [id] (oper-%%%%%%%%%%) appears within Array 2, and I'm not quite sure as to how to approach this.
Array 1
Array ( [0] => Array ( [id] => oper-4c597644-402490ee [amount] => 17498.5 ) [1] => Array ( [id] => oper-4f30019a-27f27473 [amount] => 10383.5 ) [2] => Array ( [id] => oper-4bceffd1-21e0af5b [amount] => 6277 ) [3] => Array ( [id] => oper-4f300d33-de9592e3 [amount] => 11382 ) [4] => Array ( [id] => oper-4c236420-0b11e945 [amount] => 14759 ) [5] => Array ( [id] => oper-50f6e4ad-9effbec7 [amount] => 3058 ) [6] => Array ( [id] => oper-4f05a90b-03b379f9 [amount] => 12112.5 ) [7] => Array ( [id] => oper-qtgjvw8y-1uqtw058 [amount] => 10023 ) [8] => Array ( [id] => oper-52657816-3d6516e2 [amount] => 3495 ) )
Array 2
Array ( [0] => Array ( [0] => Bob [1] => oper-4bceffd1-21e0af5b ) [1] => Array ( [0] => Bob [1] => oper-4c236420-0b11e945 ) [2] => Array ( [0] => Bob [1] => oper-4c597644-402490ee ) [3] => Array ( [0] => Bob [1] => oper-4f05a90b-03b379f9 ) [4] => Array ( [0] => Bob [1] => oper-4f30019a-27f27473 ) [5] => Array ( [0] => Bob [1] => oper-4f300d33-de9592e3 ) [6] => Array ( [0] => Bob [1] => oper-50f6e4ad-9effbec7 ) [7] => Array ( [0] => Bob [1] => oper-52657816-3d6516e2 ) [8] => Array ( [0] => Bob [1] => oper-qtgjvw8y-1uqtw058 ) [9] => Array ( [0] => Empty [1] => ) [10] => Array ( [0] => Upg. [1] => ) [11] => Array ( [0] => Ren. [1] => ) )
Bob is just an example "name"
Here is my code so far (And I know this isn't clean because this isn't done with PDO, I'm just trying to get this to work at the moment):
$result = mysql_query("SELECT * FROM tblOperatorGoals WHERE MonthlyGoal LIKE '$currentDate%'");
while ($row = mysql_fetch_array($result)) {
$opernameArray[] = $row['OperatorName'];
$operIDArray[] = $row['OperatorID'];
$monthlyGoal[] = substr($row['MonthlyGoal'], 8);
$operArray[] = array('name' => $row['OperatorName'], 'id' => $row['OperatorID']);
}
$result = mysql_query("SELECT * FROM tblUserPayments WHERE ChargeAmount IS NOT NULL AND PaymentStatus='OK' AND PaymentDate LIKE '$currentDate%'");
while ($row = mysql_fetch_array($result)) {
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
}
foreach ($operearnedArray as $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
if(array_key_exists($value['id'], $operSums1)) {
$operSums1[$value['id']] += $value['amount'];
} else {
$operSums1[$value['id']] = $value['amount'];
}
}
foreach ($operSums1 as $id => $value) {
if (in_array($id,$operIDArray)) {
$operSums[] = array(
'id' => $id,
'amount' => $value);
}
}
Any help is greatly appreciated. Associative arrays are just not my cup of tea.
Basically I'm looking for an Array that looks like this:
Array ( [name] => Bob [id] => oper-%%%%%%%%%%%%%% [amount] => $$$$$$$$$$$ )...ect...ect...
Crude but should work:
$toSort = array(
array ('id' => 'oper-4c597644-402490ee', 'amount' => 17498.5,),
array ('id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5,),
array ('id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277,),
array ('id' => 'oper-4f300d33-de9592e3', 'amount' => 11382,),
array ('id' => 'oper-4c236420-0b11e945', 'amount' => 14759,),
array ('id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058,),
array ('id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5,),
array ('id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023,),
array ('id' => 'oper-52657816-3d6516e2', 'amount' => 3495,),
);
$assigned = array (
array ('Bob','oper-4bceffd1-21e0af5b'),
array ('Bob','oper-4c236420-0b11e945'),
array ('Bob','oper-4c597644-402490ee'),
array ('Bob','oper-4f05a90b-03b379f9'),
array ('Bob','oper-4f30019a-27f27473'),
array ('Bob','oper-4f300d33-de9592e3'),
array ('Bob','oper-50f6e4ad-9effbec7'),
array ('Bob','oper-52657816-3d6516e2'),
array ('Bob','oper-qtgjvw8y-1uqtw058'),
);
$sorted = assignData($toSort, $assigned);
var_dump($sorted);
function assignData($raw, $order)
{
$returnArray = array();
foreach($order as $entry):
foreach($raw as $rawEntry):
if ($rawEntry['id'] == $entry[1]):
$temp = array(
'name' => $entry[0],
'oper' => $rawEntry['id'],
'amount' => $rawEntry['amount'],
);
$returnArray[] = $temp;
endif;
endforeach;
endforeach;
return $returnArray;
}
Try this.
<?php
$data = [
['id' => 'oper-4c597644-402490ee', 'amount' => 17498.5],
['id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5],
['id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277],
['id' => 'oper-4f300d33-de9592e3', 'amount' => 11382],
['id' => 'oper-4c236420-0b11e945', 'amount' => 14759],
['id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058],
['id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5],
['id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023],
['id' => 'oper-52657816-3d6516e2', 'amount' => 3495]
];
$names = [
['BobA', 'oper-4bceffd1-21e0af5b'],
['BobB', 'oper-4c236420-0b11e945'],
['BobC', 'oper-4c597644-402490ee'],
['BobD', 'oper-4f05a90b-03b379f9'],
['BobE', 'oper-4f30019a-27f27473'],
['BobF', 'oper-4f300d33-de9592e3'],
['BobG', 'oper-50f6e4ad-9effbec7'],
['BobH', 'oper-52657816-3d6516e2'],
['BobI', 'oper-qtgjvw8y-1uqtw058'],
['Empty', ''],
['Upg.', ''],
['Ren.', '']
];
$idMappedNames = array_column($names, 0, 1);
$summary = array_reduce($data, function($carry, $item) use(&$idMappedNames) {
if (!array_key_exists($item['id'], $carry)) {
$carry[$item['id']] = [
'name' => $idMappedNames[$item['id']],
'amount' => 0
];
}
$carry[$item['id']]['amount'] += $item['amount'];
return $carry;
}, []);
You can connect these information it in just single SQL Query
$result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal
FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID
WHERE a.ChargeAmount IS NOT NULL
AND a.PaymentStatus='OK'
AND a.PaymentDate LIKE '$currentDate%'
AND u.MonthlyGoal LIKE '$currentDate%' " );
while ($row = mysql_fetch_assoc($result)) {
$wantedArray[] = array(
'OperatorName' => $row['OperatorName'],
'MonthlyGoal'=>$row['MonthlyGoal'],
'ChargeAmount' => $row['ChargeAmount'],
'OperatorID' => $row['OperatorID']);
}
But as the question originally asked goes:
$desiredArray = array();
$id_to_amount = array();
foreach($array1 as $x)
$id_to_amount[$x['id']] = $x['amount'];
foreach($array2 as $x)
{
$id = $x[1];
$name = $x[0];
$amount = -1;
if(isset($id_to_amount[$id]))
$amount = $id_to_amount[$id];
$desiredArray[] = array('id'=>$id,'name'=>$name,'amount'=>$amount );
}