I have an array $result as follows
Array
( [0] => Array (
[0] => Mr
[1] => vinay
[2] => hs
[3] => tester
[4] =>vinay.hs#abc.com
[5] => 909099
[6] => Yes )
[1] => Array (
[0] => Mr
[1] => Suresh
[2] => Kumar
[3] => tester
[4] => vinay.hs#abc.com
[5] => 809090
[6] => No )
).
I want to store this array as
Array
([0]=>Array (
[title] => Mr
[firstname] => vinay
[lastname] => hs
[job_title] => tester
[email] =>vinay.hs#abc.com
[phone] => 909099
[is_employed] => Yes )
[1] => Array (
[title] => Mr
[firstname] => Suresh
[lastname] => Kumar
[job_title] => tester
[email] => vinay.hs#abc.com
[phone] => 809090
[is_employed] => No ) ).
Explain me how to do this
$fp = fopen('foo.csv', 'r');
$fields = fgetcsv($fp); // assumes fields are the first row in the file
// or $fields = array('title', 'firstname', 'lastname', 'job_title', 'email', 'phone', 'is_employed');
$records = array();
while ($record = fgetcsv($fp))
{
$records[] = array_combine($fields, $record);
}
Obviously it needs error handling added to it.
$newarray=array();
foreach($result as $res) {
$a = array();
$i = 0;
foreach(array('title','firstname','lastname','job_title','email','phone','is_employed') as $key) {
$a[$key] = $res[$i++];
}
$newarray[] = $a;
}
$arr = array("title" => "Mr", "title" => "vinay", "key" => "value")
access it with:
$arr["title"]
array(
array(
'title' => 'Mr',
'firstname' => 'vinay',
'lastname' => 'hs',
'job_title' => 'tester',
'email' => 'vinay.hs#abc.com',
'phone' => '909099',
'is_employed' => TRUE
),
array(
'title' => 'Mr',
'firstname' => 'Suresh',
'lastname' => 'Kumar',
'job_title' => 'tester',
'email' => 'vinay.hs#abc.com',
'phone' => '809090',
'is_employed' => FALSE
)
);
UPDATED:
My answer previously is dumb. If you are loading from CSV file, assuming the first element of array is the keys.
You might want to do something like this.
Sorry for my bad naming.
$keysArray = array_shift($arrayFromCSV);
$wantedArrayStructure = array();
foreach ($arrayFromCSV as $person) {
$item = array();
foreach ($person as $key => $value) {
$item[$keysArray[$key]] = $value;
}
$wantedArrayStructure[] = $item;
}
var_dump($wantedArrayStructure);
If, is is result from msql query. You should using function to get the expected result:
mysql_fetch_assoc
Related
I have an array that contains two values, but i need upper value as key of lower value like value of name replace with value and also remove name from array.
Array
(
[0] => Array
(
[name] => firt_name
[value] => Robin
)
[1] => Array
(
[name] => last_name
[value] => Singh
)
[2] => Array
(
[name] => email
[value] => 123#gmail.com
)
[3] => Array
(
[name] => password
[value] => 12345
)
)
Here is the code
function key_replace($params = array())
{
if (!empty($params)) {
$array[] = array();
foreach ($params as $key => $value) {
$array[$value['name']] = $value['value'];
}
print_r($array);
}
}
Any solution appreciated!
Another approach is to use array_column and array_combine
array_combine(array_column($array, 'name'), array_column($array, 'value'));
https://3v4l.org/boAOI
A Simple foreach() will do the trick for you.
$result = [];
foreach($array as $k=>$v){
$result[$v['name']] = $v['value'];
}
print_r($result);
WORKING DEMO: https://3v4l.org/hH39i
$datas = $array = array
(
'0' => array
(
'name' => 'firt_name',
'value' => 'Robin'
)
,
'1' => array
(
'name' => 'last_name',
'value' => 'Singh'
)
,
'2' => array
(
'name' => 'email',
'value' => '123#gmail.com'
)
,
'3' => array
(
'name' => 'password',
'value' => '12345',
)
,
'4' => array
(
'name' => 'phone',
'value' => 'skdsjdkdjskd'
)
,
'5' => Array
(
'name' => 'city',
'value' => 'dskjdksjd'
)
,
'6' => Array
(
'name' => 'state',
'value' => 'kjksdjskdsk'
)
);
$array = '';
foreach ($datas as $key => $value) {
$array[$value['name']] = $value['value'];
}
echo '<pre>';
print_r($array);
echo '</pre>';
Array
(
[firt_name] => Robin
[last_name] => Singh
[email] => 123#gmail.com
[password] => 12345
[phone] => skdsjdkdjskd
[city] => dskjdksjd
[state] => kjksdjskdsk
)
Below is my array which i have printed:-
I want only the product_image from the array in loop
Array
(
[0] => Array
(
[product_option_id] => 247
[product_id] => 66
[product_option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 42
[color_product_id] => 54
[name] => Pink
[product_image] => catalog/demo/teddy/03.jpg
[image] => http://192.168.15.9/Kids_stores/image/cache/catalog/axalta-ral-3015-light-pink-polyester-30-matt-powder-coating-20kg-box--1447-p-50x50.jpg
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 41
[color_product_id] => 67
[name] => Light Brown
[product_image] => catalog/Teddies/12-Baby-teddy/05.jpg
[image] => http://192.168.15.9/Kids_stores/image/cache/catalog/option-color/light_brown-50x50.jpg
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 43
[color_product_id] => 68
[name] => Cream
[product_image] => catalog/Teddies/12-Baby-teddy/11.jpg
[image] => http://192.168.15.9/Kids_stores/image/cache/catalog/option-color/cream-images-50x50.jpg
[price] =>
[price_prefix] => +
)
)
[option_id] => 5
[name] => COLOR
[type] => image
[value] =>
[required] => 0
)
)
Try this,
foreach($array as $val)
{
echo $val['product_image'];
}
Solution for your edited input:-
$image_array = array();
foreach ($your_array as $arr){
$image_array[] = array_column($arr['product_option_value'],'product_image');
}
Output:- https://eval.in/657966
You can take the array array_column and make it like this
$records = array (
array (
// your array
)
);
$variable = array_column($records, 'image');
echo $variable;
<?php $samples=$data['options'][0][product_option_value];
$product_image = array_column($samples, 'product_image');
echo'<pre>'; print_r($product_image );
?>
foreach($array as $key => $val){
if($key == 'product_image'){
echo "<img src='".$val."' />";
}
}
Try
You have to foreach the inner array:
foreach($array[product_option_value] as $val)
{
echo $val['product_image'];
}
Solution One:
<?php
$req_image=array();
$req_image[] = array_column($resultant_array, 'product_image');
print_r($req_image); // This will print all the images that are grouped under the array().
?>
Example:
The below is the PHP code and the sample output that you can obtain using the array_column().
PHP:
<?php
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe'
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith'
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones'
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe'
)
);
$lastNames = array_column($records, 'last_name', 'id');
Output:
If we call print_r() on $lastNames, you’ll see a resulting array that looks a bit like this:
Array
(
[2135] => Doe
[3245] => Smith
[5342] => Jones
[5623] => Doe
)
Solution Two: (As per requirement at last)
You can iterate the single key value alone in the foreach so that you can get the required parameter that you need.
foreach($resultant_array as $single_array)
{
foreach($single_array['product_option_value'] as $inner_array)
{
echo $inner_array['product_image']; // This will print the u=mage name that you need.
}
}
If you want one specified key and want minimal assumptions about array structure shoud use array_walk_recursive like this
$result = [];
array_walk_recursive($input,function ($value,$key) use (&$result) {
if ( 'product_image' == $key) {
$result[] = $value;
}
});
I have following array..I am trying to group this.
Array
(
[0] => Array
(
[Title] => HoMedics MAN-300
[ean] => 31262006288
[upc] => 31262006288
[ProductImageName] =>
[CdnUri] =>
[ASIN] => B000050FEU
[ListPrice] => 129.99
[Status] => 2
[ActualPrice] => 129.99
[ProductID] => 5286728
)
)
I want to group this into
Array
(
[0] => Array
(
[VitalInfo]=>array(
[Title] => HoMedics MAN-300
[ean] => 31262006288
[upc] => 31262006288
[ProductImageName] =>
[CdnUri] =>
[ASIN] => B000050FEU
)
[Price]=>array(
[ListPrice] => 129.99
[Status] => 2
[ActualPrice] => 129.99
[ProductID] => 5286728
)
)
I tried but it doesn't happen as I want ...any help would be great...Thanx in advance..
try this,
CODE :
foreach($old_array as $key_old => $val_old)
{
foreach($val_old as $key => $val)
{
if(in_array($key, $VitalInfo_array))
{
$new_array[$key_old]['VitalInfo'][$key] = $val;
}
else
{
$new_array[$key_old]['Price'][$key] = $val;
}
}
}
OUTPUT :
Array
(
[0] => Array
(
[VitalInfo] => Array
(
[Title] => HoMedics MAN-300
[ean] => 31262006288
[upc] => 31262006288
[ProductImageName] =>
[CdnUri] =>
[ASIN] => B000050FEU
)
[Price] => Array
(
[ListPrice] => 129.99
[Status] => 2
[ActualPrice] => 129.99
[ProductID] => 5286728
)
)
)
DEMO
i hope it will be helpful.
Simply loop through your array and customize a new array accordingly.
Assuming $array is the original array, and $result is the customized array, try this:
foreach ($array as $k => $arr) {
$result[$k]['VitalInfo'] = array(
'Title' => $arr['Title'],
'ean' => $arr['ean'],
'upc' => $arr['upc'],
'ProductImageName' => $arr['ProductImageName'],
'CdnUri' => $arr['CdnUri'],
'ASIN' => $arr['ASIN']
);
$result[$k]['Price'] = array(
'ListPrice' => $arr['ListPrice'],
'Status' => $arr['Status'],
'ActualPrice' => $arr['ActualPrice'],
'ProductID' => $arr['ProductID']
);
}
Input : $info // Your Original Array
Ouput : $finalArr // Your Required Array
$vitalInfo = array ('Title','ean','upc','ProductImageName','CdnUri','ASIN');
$price = array ('ListPrice','Status','ActualPrice','ProductId');
$finalArr = array();
foreach ($info as $arr) {
$result = array();
foreach($arr as $k => $v){
if(in_array($k,$vitalInfo))
$result['VitalInfo'][$k] = $v;
else if(in_array($k,$price))
$result['Price'][$k] = $v;
}
$finalArr[] = $result;
}
If you are grouping this in php, have a look at these answers.
Or just copy this:
$input = [0 => [
'Title' => 'HoMedics MAN-300',
'ean' => 31262006288,
'upc' => 31262006288,
'ProductImageName' => '',
'CdnUri' => '',
'ASIN' => 'B000050FEU',
'ListPrice' => 129.99,
'Status' => 2,
'ActualPrice' => 129.99,
'ProductID' => 5286728
]];
foreach ($input as $in){
$out['VitalInfo'] = [];
$out['Price'] = [];
foreach ($in as $key => $i){
if (in_array($key, ['Title', 'ean', 'upc', 'ProductImageName', 'CdnUri', 'Asin'])){
$out['VitalInfo'][] = [$key => $i];
} else {
$out['Price'][] = [$key => $i];
}
}
$output[]=$out;
}
echo '<pre>';
var_dump($output);
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 );
}