I am getting following history from PayPal recurring profile. Please help me to convert into array. I need to verify "P_TRANSTATE" value to every month. It's really hard to check through following array. Please suggest me or help me to convert array to the following view.
[status] => 1
[result] => Array
(
[HTTP/1_1_200_OK ... RESULT] => 0
[RPREF] => RGX51B669592
[PROFILEID] => 0
[P_PNREF1] => BL0PEE6F2E98
[P_TRANSTIME1] => 25-Aug-17 04:46 AM
[P_RESULT1] => 0
[P_TENDER1] => C
[P_AMT1] => 19.99
[P_TRANSTATE1] => 8
[P_PNREF2] => BP0PECB1799B
[P_TRANSTIME2] => 24-Sep-17 04:58 AM
[P_RESULT2] => 0
[P_TENDER2] => C
[P_AMT2] => 19.99
[P_TRANSTATE2] => 8
);
Need to following format
1] => Array
(
[P_PNREF] => BL0PEE6F2E98
[P_TRANSTIME] => 25-Aug-17 04:46 AM
[P_RESULT] => 0
[P_TENDER] => C
[P_AMT] => 19.99
[P_TRANSTATE] => 8
[2] => Array
(
[P_PNREF] => BP0PECB1799B
[P_TRANSTIME] => 24-Sep-17 04:58 AM
[P_RESULT] => 0
[P_TENDER] => C
[P_AMT] => 19.99
[P_TRANSTATE] => 8 );
If I don't misunderstood your question then this should work for you.
<?php
$array = array
(
'HTTP/1_1_200_OK ... RESULT' => 0,
'RPREF' => 'RGX51B669592',
'PROFILEID' => 'RP0000000040',
'P_PNREF1' => 'BQ1PECD4AEB8',
'P_TRANSTIME1' => '25-Aug-17 04:46 AM',
'P_RESULT1' => 0,
'P_TENDER1' => 'C',
'P_AMT1' => 19.99,
'P_TRANSTATE1' => 8,
'P_PNREF2' => 'BT1PFFF8A110',
'P_TRANSTIME2' => '24-Sep-17 04:58 AM',
'P_RESULT2' => 0,
'P_TENDER2' => 'C',
'P_AMT2' => 19.99,
'P_TRANSTATE2' => 8,
);
unset($array['HTTP/1_1_200_OK ... RESULT'],$array['RPREF'],$array['PROFILEID']);
$final_array = [];
foreach($array as $key=>$value){
$index = substr($key, -1);
$key = substr($key, 0, -1);
$final_array[$index][$key] = $value;
}
print '<pre>';
print_r($final_array);
print '</pre>';
?>
Output:
Array
(
[1] => Array
(
[P_PNREF] => BQ1PECD4AEB8
[P_TRANSTIME] => 25-Aug-17 04:46 AM
[P_RESULT] => 0
[P_TENDER] => C
[P_AMT] => 19.99
[P_TRANSTATE] => 8
)
[2] => Array
(
[P_PNREF] => BT1PFFF8A110
[P_TRANSTIME] => 24-Sep-17 04:58 AM
[P_RESULT] => 0
[P_TENDER] => C
[P_AMT] => 19.99
[P_TRANSTATE] => 8
)
)
DEMO: https://eval.in/978871
As per the comment of Parapluie: You can use preg_match() to get keys/index when it cross the digit after 9 because substr($key,-1) or substr($key,0,-1) will not work properly then. See below-
$re = '/(\D+)(\d+)/';
foreach($array as $key=>$value){
preg_match($re, $str, $matches);
$index = $matches[2];
$key = $matches[1];
$final_array[$index][$key] = $value;
}
You can do this, assuming $res is the paypal array response.
<?php
function convertArray($arr){
$temp = array();
$temp[1] = $arr['result'];
return $temp;
}
print_r(convertArray(r$es));
?>
Related
I have an array with some repeating string values. How to replace these string values (as a whole, because some words are repeated in others strings) with corresponding specific numeric values, as bellow?
deloc = 1
foarte puţin = 2
mediu = 3
mult = 4
foarte mult = 5
This is the array (example):
array = (
"tensionat" => "mediu",
"trist" => "mult",
"melancolic" => "deloc",
"fara_speranta" => "foarte puțin",
"nefolositor"] => "deloc",
"ingrijorat" => "foarte mult",
"amarat" => "deloc",
"anxios" => "mediu"
);
How can this
Try this
$data = array (
"tensionat" => "mediu",
"trist" => "mult",
"melancolic" => "deloc",
"fara_speranta" => "foarte puțin",
"nefolositor" => "deloc",
"ingrijorat" => "foarte mult",
"amarat" => "deloc",
"anxios" => "mediu"
);
$repl = array (
'deloc' => 1,
'foarte puţin' => 2,
'mediu' => 3,
'mult' => 4,
'foarte mult' => 5,
);
$result = array ();
foreach ($data as $key => $value) {
$result[$key] = !empty($repl[$value]) ? $repl[$value] : $value;
}
print_r($result);
Output:
Array
(
[tensionat] => 3
[trist] => 4
[melancolic] => 1
[fara_speranta] => foarte puțin
[nefolositor] => 1
[ingrijorat] => 5
[amarat] => 1
[anxios] => 3
)
I want show all post variables if name of variables has start add-
for example: this is a full string with post variables:
Array ( [PartNumber] => sfd [Description] => dsf [Issue] => dfs [Customer] => J.C.B. SERVICE [widget7-table_length] => 5 [add_332/F2684] => [add_333/D1641] => sdf [add_333/D1202] => [add_332/F3144] => sdf [add_332/F3147] => sfd [makeassy] => )
I want to display in array only
[add_332/F2684] => [add_333/D1641] => sdf [add_333/D1202] => [add_332/F3144] => sdf [add_332/F3147] => sfd
I'm trying
print_r($_POST['add_%']);
But, how you see this post, it's not working. Do you have any ideas ?
Use array_filter to extract specific array keys:
$output = array_filter($_POST, function($e) {
return strpos($e, 'add_') === 0;
}, ARRAY_FILTER_USE_KEY);
You coudl try using preg_grep()
$res= preg_grep ('/^add_ (\w+)/i', $_POST);
var_dump($res);
Another Solution with Foreach
<?php
$x = array(
'PartNumber' => 'sfd',
'Description' => 'dsf',
'add_332/F2684' =>'asd',
'add_333/D1641' =>'sdf' ,
'add_333/D1202' => 'asd',
'TESTEST' => 'ASDASD' );
$tmp = array();
foreach($x as $key => $value)
{
if(substr( $key, 0, 3 ) === "add")
{
array_push($tmp,array($key =>$value));
}
}
die(print_r($tmp));
?>
Output:
Array
(
[0] => Array
(
[add_332/F2684] => asd
)
[1] => Array
(
[add_333/D1641] => sdf
)
[2] => Array
(
[add_333/D1202] => asd
)
)
1
I've an array.
Array
(
[initial] => MSS
[hour] => 5.2
[row_checker_1] => 1
[project_name_1] => KGD001
[project_shortcode_1] => KGD001
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
[remarks_1] => on going
[task_id] => 76
[row_checker_2] => 2
[project_name_2] => DG001
[project_shortcode_2] => DG001
[5_2] => 1.1
[6_2] => 2.2
[4_2] => 3.1
[remarks_2] => on going
)
Now I want to split all element upper range key is "project_shortcode_1" and lower range key is remarks_1.
So, new array should look like:
array
(
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
)
Use array_filter with flag ARRAY_FILTER_USE_KEY for using the array keys, and do the comparison with the logic needed to get the desired keys. It works from PHP 5.6.
$arr = array ( "initial" => "MSS",
"hour" => 5.2,
"row_checker_1" => 1,
"project_name_1" => "KGD001",
"project_shortcode_1" => "KGD001",
"5_1" => 23,
"6_1" => 3.3,
"4_1" => 23.2,
"remarks_1" => "on going",
"task_id" => 76,
"row_checker_2" => 2,
"project_name_2" => "DG001",
"project_shortcode_2" => "DG001",
"5_2" => 1.1,
"6_2" => 2.2,
"4_2" => 3.1,
"remarks_2" => "on going",
);
// PHP > 5.6
$result = array_filter($arr, function($k){
$var = explode('_', $k);
return is_numeric($var[0]) && $var[1]==1;
}, ARRAY_FILTER_USE_KEY);
If what you need is a multidimensional array with all the ranges NUMBER_N, then use something like this (extended from Dmitriy Demir answer):
$myArray = array(
'initial' => 'MSS',
'hour' => '5.2',
'row_checker_1' => '1',
'project_name_1' => 'KGD001',
'project_shortcode_1' => 'KGD001',
'5_1' => '23',
'6_1' => '3.3',
'4_1' => '23.2',
'remarks_1' => 'on going',
'task_id' => '76',
'row_checker_2' => '2',
'project_name_2' => 'DG001',
'project_shortcode_2' => 'DG001',
'5_2' => '1.1',
'6_2' => '2.2',
'4_2' => '3.1',
'remarks_2' => 'on going'
);
function splitRange($a){
$newArray = array();
foreach ($a as $k => $v) {
$rightFormat = preg_match('/^\d+_(\d+)$/', $k, $index);
if ($rightFormat)
$newArray[$index[1]][$k] = $v;
}
return $newArray;
}
print_r(splitRange($myArray));
The result will be something like:
Array
(
[1] => Array
(
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
)
[2] => Array
(
[5_2] => 1.1
[6_2] => 2.2
[4_2] => 3.1
)
)
being N from NUMBER_N the index of the array.
Since you mentioned in the comments that you'd prefer to get all values that are in format NUMBER_1 I think you'd need to loop through your array and check the value names with regex, then add the values to a new array if they meet the criteria. Here's how I would do this:
$myArray = array(
'initial' => 'MSS',
'hour' => '5.2',
'row_checker_1' => '1',
'project_name_1' => 'KGD001',
'project_shortcode_1' => 'KGD001',
'5_1' => '23',
'6_1' => '3.3',
'4_1' => '23.2',
'remarks_1' => 'on going',
'task_id' => '76',
'row_checker_2' => '2',
'project_name_2' => 'DG001',
'project_shortcode_2' => 'DG001',
'5_2' => '1.1',
'6_2a' => '2.2',
'4_2' => '3.1',
'remarks_2' => 'on going'
);
$newArray = array();
foreach ($myArray as $k => $v) {
$rightFormat = preg_match('/^\d+_\d+$/', $k);
if ($rightFormat)
$newArray[$k] = $v;
}
print_r($newArray);
The result of print_r in that case would be:
Array ( [5_1] => 23 [6_1] => 3.3 [4_1] => 23.2 [5_2] => 1.1 [6_2] =>
2.2 [4_2] => 3.1 )
If the number after the underscore should always be 1 then change the regex from /^\d+_\d+$/ to /^\d+_1$/.
You can play around and see how regex works here.
PS: I've set all values to strings out of convenience. Feel free to modify that.
A regex-based solution seems fitting for this question.
preg_grep() is a function designed to apply a regex filter upon each value in an array. I little more tweaking is necessary for this case because the keys must be filtered instead.
The One-liner:
$output=array_intersect_key($input,array_flip(preg_grep("/^\d+_1$/",array_keys($input)))));
/* array (
'5_1' => 23,
'6_1' => 3.3,
'4_1' => 23.2,
)*/
Here is the step-by-step array manipulation...
array_keys($input); // create array with input keys as values
/* array (
0 => 'initial',
1 => 'hour',
2 => 'row_checker_1',
3 => 'project_name_1',
4 => 'project_shortcode_1',
5 => '5_1',
6 => '6_1',
7 => '4_1',
8 => 'remarks_1',
9 => 'task_id',
10 => 'row_checker_2',
11 => 'project_name_2',
12 => 'project_shortcode_2',
13 => '5_2',
14 => '6_2',
15 => '4_2',
16 => 'remarks_2',
) */
preg_grep("/^\d+_1$/",array_keys($input)); // filter the input array using regex pattern
/* array (
5 => '5_1',
6 => '6_1',
7 => '4_1',
) */
array_flip(preg_grep("/^\d+_1$/",array_keys($input))); // flip the filtered array
/* array (
'5_1' => 5,
'6_1' => 6,
'4_1' => 7,
)*/
array_intersect_key($input,array_flip(preg_grep("/^\d+_1$/",array_keys($input)))); // filter input by comparing keys against filtered array
/* array (
'5_1' => 23,
'6_1' => 3.3,
'4_1' => 23.2,
)*/
I have an array with same customerid. I want to merge all same customerid arrays in to one with few amends to the array.
Array
(
[0] => Array
(
[customerid] => 13
[customer_fullname] => Chris
[profession_id] => 8
[profession_name] => Producer
)
[1] => Array
(
[customerid] => 1
[customer_fullname] => John
[profession_id] => 8
[profession_name] => Producer
)
[2] => Array
(
[customerid] => 13
[customer_fullname] => Chris
[profession_id] => 7
[profession_name] => Camera
)
)
So now I want a new array to be created like this:
Array(
[customerid] => 13
[customer_fullname] => Chris
[new_array] => array(
[0]=>[profession_id] => 8, [profession_name] => Producer,
[1]=>[profession_id] => 7, [profession_name] => Camera
)
)
Spent some time on it but wasn't able to get it right
There are better approaches if you're merging lots of records, but if you want a way to just merge two records as stated, I'd just do this:
$array1 = array(
'customerid' => 13
'customer_fullname' => 'John',
'profession_id' => 8,
'profession_name' => 'Producer'
);
$array2 = array(
'customerid' => 13
'customer_fullname' => 'John',
'profession_id' => 7,
'profession_name' => 'Director'
);
function merge_customers($customerA, $customerB)
{
$newCustomer = array();
if ($customerA['customerid'] == $customerB['customerid'])
{
$newCustomer['customerid'] = $customerA['customerid'];
$newCustomer['customer_fullname'] = $customerA['customer_fullname'];
$newCustomer['new_array'] = array(
array(
'profession_id' => $customerA['profession_id'],
'profession_name' => $customerA['profession_name']
),
array(
'profession_id' => $customerB['profession_id'],
'profession_name' => $customerB['profession_name']
)
);
return $newCustomer;
}
/* We can't merge these if they're different customers. */
return NULL;
}
The extended solution which is also well-suited for finding and "merging" multiple groups of entries which has same customerid. Used functions: array_filter, array_count_values, array_keys, array_walk, array_chunk and array_values:
// supposing $arr is your initial array
// finds which 'customerid' has multiple entries
$dupIds = array_filter(array_count_values(array_column($arr, "customerid")), function($v) {
return $v > 1;
});
$dupIds = array_keys($dupIds);
$result = [];
array_walk($arr, function($v) use(&$result, $dupIds) {
if (in_array($v['customerid'], $dupIds)) {
$parts = array_chunk($v, 2, true);
if (!isset($result[$v['customerid']])) {
$result[$v['customerid']] = $parts[0] + ['new_array' => [$parts[1]]];
} else {
$result[$v['customerid']]['new_array'][] = $parts[1];
}
}
});
print_r(array_values($result));
The output:
Array
(
[0] => Array
(
[customerid] => 13
[customer_fullname] => Chris
[new_array] => Array
(
[0] => Array
(
[profession_id] => 8
[profession_name] => Producer
)
[1] => Array
(
[profession_id] => 7
[profession_name] => Camera
)
)
)
)
Quick hack, maybe there is a nicer solution.
Note: The second "for each" loop is only needed if there is the possibility that the arrays don't have the same fields.
function merge($array1, $array2){
$result = array();
foreach($array1 as $key => $value){
if(isset($array2[$key]) && $array2[$key]!=$array1[$key]){
$result[$key][]=$value;
$result[$key][]=$array2[$key];
}else{
$result[$key]=$value;
}
}
foreach($array2 as $key => $value){
if(!isset($result[$key])){
$result[$key] = $value;
}
}
return $result;
}
print_r(merge($array1, $array2));
I have following array of stdClass Object structure and want count number of offers. Std class is received as a response from third party API so it is dynamic.
stdClass Object
(
[Offer] => Array
(
[0] => stdClass Object
(
[Offerid] => 1
[LoanAmount] => 2****
[InterestRate] => 2*
[Term] => 36
[MonthlyPayment] => 7***
[Annualfee] => 0
[OriginationFee] => 1***
)
[1] => stdClass Object
(
[Offerid] => 1
[LoanAmount] => 2****
[InterestRate] => 2*
[Term] => 36
[MonthlyPayment] => 7***
[Annualfee] => 0
[OriginationFee] => 1***
)
[2] => stdClass Object
(
[Offerid] => 1
[LoanAmount] => 2****
[InterestRate] => 2*
[Term] => 36
[MonthlyPayment] => 7***
[Annualfee] => 0
[OriginationFee] => 1***
)
)
)
i want count number of arrays in [Offer], for that i have done following:
echo "count----------".count($offers);
but it gives 1 as a count like
count----------1
in this case count is 3 and i want 3 as output.
Please suggests.
i have also used this
echo "count----------".count((array)$offers);
This also dont works.
You can do it like convert "stdClass Object" into normal array and try after that count($offer);
Just write (array)$object; It will convert as normal array
Count should work.
<?php
$obj = new stdClass();
$obj->Offer = array(
array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'),
array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'),
array('Offerid' => 1, 'LoanAmount' => '2***', 'InterestRate' => '2*', 'Term' => 36, 'MonthlyPayment' => '7***', 'Annualfee' => 0, 'OriginationFee' => '1***'),
);
echo count($obj->Offer); // Outputs 3
?>
Live example
I have solved my question by following way:
foreach ($offers as $key=> $value)
{
echo "<br/>count->".count($value);
}
this loops itreate only once, and give me result.
Try this :
<?php
class Example {
public $public = 'prop:public';
private $prv = 'prop:private';
protected $prt = 'prop:protected';
}
$arrayobj = new ArrayObject(new Example());
var_dump($arrayobj->count());
$arrayobj = new ArrayObject(array('first','second','third'));
var_dump($arrayobj->count());
?>
The above example will output:
int(1)
int(3)
Answer Source