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));
?>
I've been trying and trying on this and can't seem to work it out on my own.
So could someone please show me the direction I want to take with this?
I want to be able to implode the object items which contain the string - grade_id.
I've tried getting them all by using array_filter() etc. However seems to not return back the right values.
array_keys() doesn't bring anything back either to try and match array_keys with a preg_match.
I'm just looking for some guidance for this, you don't have to give me a full answered answer, just a point in the right direction.
meeting Object
(
[errors] => 0
[id] => 1
[school_id] => 1
[staff_id] => 2
[grade_id] => 85
[grade_id_2] => 0
[grade_id_3] => 0
[grade_id_4] => 0
[grade_id_5] => 0
[grade_id_6] => 0
[grade_id_7] => 0
[grade_id_8] => 0
[grade_id_9] => 0
[grade_id_10] => 0
[inserted] => 2018-02-19 11:46:13
[updated] => 2018-02-19 12:00:31
)
Result i'm looking for is: (I'm wanting to find a way without using a loop "If possible")
$grade_ids = "85";
You can do this if you convert your object into array:
$array = (array)$object;
/* find keys in array that contain 'grade_id' */
$keys = preg_grep('/grade_id/', array_keys($array));
/* discard other array elements */
$filtered = array_intersect_key($array, array_flip($keys));
$result = implode(',', $filtered);
You can use array_walk_recursive(), I try like this code:
$var = array('meeting'=> array("errors" => 0,
"id" => 1,
"school_id" => 1,
"staff_id" => 2,
"grade_id" => 85,
"grade_id_2" => 0,
"grade_id_3" => 0,
"grade_id_4" => 0,
"grade_id_5" => 0,
"grade_id_6" => 0,
"grade_id_7" => 0,
"grade_id_8" => 0,
"grade_id_9" => 0,
"grade_id_10" => 0,
"inserted" => "2018-02-19 11:46:13",
"updated" => "2018-02-19 12:00:31"));
$uniques = array();
array_walk_recursive($var, function($val, $key) use (&$uniques){
$i=2;
if($key == 'grade_id') {
$uniques[] = $val;
} if($key == 'grade_id_'.$i) {
$uniques[] = $val;
}
$i++;
});
$uniques = array_unique($uniques);
print_r($uniques);
and output is:
Array ( [0] => 85 [1] => 0 )
This question already has answers here:
How to sum all column values in multi-dimensional array?
(20 answers)
Closed 1 year ago.
I have array like this :
Array
(
[0] => Array
(
[impr] => 800000
[clicks] => 500
[ctr] => 0.000625
[cvr] => 0.04
[cpc] => 0.14024
[cpm] => 0.08765
[cpa] => 3.51
)
[1] => Array
(
[impr] => 889000
[clicks] => 600
[ctr] => 0.000625
[cvr] => 0.08
[cpc] => 0.34024
[cpm] => 0.08765
[cpa] => 4.41
)
)
I want to sum that array and get result like this
Array
(
[impr] => 1689000
[clicks] => 1100
[ctr] => 0.0025
[cvr] => 0.12
[cpc] => 0.96096
[cpm] => 0.1753
[cpa] => 7.92
)
I trying to using
array_sum()
with looping my array to that array_sum, but I getting error like this
array_sum() expects parameter 1 to be array, integer given
And even try looping with foreach and using += for sum the value, but the result is not I want
the result is 800000889000
Can anyone suggest me the better code for getting my result like I want
First create an array that will contain the sums:
$sumArray = array(
"impr" => 0,
"clicks" => 0,
"ctr" => 0,
"cvr" => 0,
"cpc" => 0,
"cpm" => 0,
"cpa" => 0,
);
Then do the calculating:
foreach ($oldArray as $row)
{
foreach ($rows as $key => $value)
{
$sumArray[$key] += $value;
}
}
Then to view your results, just do:
print_r($sumArray);
NOTE: I am assuming that the keys that you have in your original array don't have the brackets around them, however if the actual key strings do have the brackets, then use the following code for creating $sumArray:
$sumArray = array(
"[impr]" => 0,
"[clicks]" => 0,
"[ctr]" => 0,
"[cvr]" => 0,
"[cpc]" => 0,
"[cpm]" => 0,
"[cpa]" => 0,
);
i have foreach loop that returns multi-array from the database
and i want to convert this array to multi array in json ,
how to do this ?
php array example
Array
(
[0] => Array
(
[it_code] => 2894
[it_quantity] => 300
[it_price] => 0
[it_notes] =>
)
[1] => Array
(
[it_code] => 2894
[it_quantity] => 284
[it_price] => 0
[it_notes] =>
)
[2] => Array
(
[it_code] => 2894
[it_quantity] => 4
[it_price] => 0
[it_notes] =>
)
[3] => Array
(
[it_code] => 2894
[it_quantity] => 3
[it_price] => 0
[it_notes] =>
)
)
i want returned json to be like this format
[
['2894', 300, 0,''],
['2894', 284, 0,''],
['2894', 4, 0,''],
['2894', 3, 0,''],
['2894', 10, 0, '']
]
my code like this
$this->db->where("it_parent_item", $parent_id);
$this->db->select("d_items.it_code,d_items_type.it_ty_ar_desc,d_items.it_quantity,d_items.it_price,it_notes");
$this->db->join('d_items_type','d_items_type.it_ty_id=d_items.it_type','left');
$this->db->from("d_items");
$result = $this->db->get()->result_array();
echo "<pre>";
print_r($result);
echo "</pre>";
You can use array_values() and array_walk_recursive() to convert integer to string
$newArray = array();
foreach($sourceArray as $element) {
$newArray[] = array_values($element);
}
array_walk_recursive($newArray,
function(&$value, $key){
$value = (string)$value;
});
print_r (json_encode($newArray));
Note that other answers will give null instead of ''.
So, without using array_values, this code returns all values, but in case there is any null, it returns '' instead (as expected in the question):
$arr = array();
foreach($foo as $value){
$tmp = array();
foreach($value as $v){
$tmp[] = $v===null ? '' : $v;
}
$arr[] = $tmp;
}
echo json_encode($arr);
Output:
[[2894,300,0,""],[2894,284,0,""],[2894,4,0,""],[2894,3,0,""]]
[
[2894,300,0,""],
[2894,284,0,""],
[2894,4,0,""],
[2894,3,0,""]
]
This a copyable array:
$foo = array
(
0 => array
(
'it_code' => 2894,
'it_quantity' => 300,
'it_price' => 0,
'it_notes' => null
),
1 => Array
(
'it_code' => 2894,
'it_quantity' => 284,
'it_price' => 0,
'it_notes' => null
),
2 => Array
(
'it_code' => 2894,
'it_quantity' => 4,
'it_price' => 0,
'it_notes' => null
),
3 => Array
(
'it_code' => 2894,
'it_quantity' => 3,
'it_price' => 0,
'it_notes' => null
),
);
Here's the initial array (shown like a PHP array, but the same as your post):
$initialArray = array(
array(
"it_code" => 2894,
"it_quantity" => 300,
"it_price" => 0,
"it_notes" => '',
),
array(
"it_code" => 2894,
"it_quantity" => 284,
"it_price" => 0,
"it_notes" => '',
),
array(
"it_code" => 2894,
"it_quantity" => 4,
"it_price" => 0,
"it_notes" => '',
),
array(
"it_code" => 2894,
"it_quantity" => 3,
"it_price" => 0,
"it_notes" => '',
),
);
You can loop over each element, assigning just the values to a new set of arrays, like this:
$newArray = array();
foreach ($initialArray as $subArray)
{
$newArray[] = array_values($subArray);
}
The resulting array will look like this:
[[2894,300,0,""],[2894,284,0,""],[2894,4,0,""],[2894,3,0,""]]
Looks to me like you want to loop through your array so it's formatted how you want in PHP and then convert that PHP array into JSON:
$dataArray = array(); //The array containing your values
$jsonArray = array(); //The array which will be formatted for json
foreach($dataArray as $value){
$keylessValues = array_values($value);
$jsonArray[] = $keylessValues;
}
$jsonArray = json_encode($jsonArray); //This is now a JSON array storing your values
What we do here is move through the array and then take only the values with array_values() and put them into a new index in our $jsonArray.
Once we have moved through the entire array we can convert our newly formatted and populated array into JSON with json_encode()
It's worth noting that your values that are set as '' will come through as null. If you need those values as '' instead of null have a look at the answer #FirstOne gave.
I have array in php :
Array
(
[id] => 1
[comp_id] => 1
[transaction_purpose] => 0
[source_of_funds] => 1
[beneficiary_relationship] => 0
[cus_occupation] => 0
[cus_id_image_2] => 0
[cus_id_image_3] => 0
[ben_id_type] => 0
[ben_id_number] => 1
)
I want to get only array key=>value pair if the valie is 1.
result array should be:
Array
(
[id] => 1
[comp_id] => 1
[source_of_funds] => 1
[ben_id_number] => 1
)
I tried with:
$returnArray = array();
foreach($mainArray as $r){
if($r>0){
array_push($returnArray, $mainArray);
}
}
But, It's giving me 4 times main array. Is there any way to achieve this? Thanks..
Just use array_filter():
$newarray = array_filter($array, function($var) {
return ($var === 1);
});
$newarray = array_filter($array);
Demo
$array = array(
'id' => 1,
'comp_id' => 1,
'transaction_purpose' => 0,
'source_of_funds' => 1,
'beneficiary_relationship' => 0,
'cus_occupation' => 0,
'cus_id_image_2' => 0,
'cus_id_image_3' => 0,
'ben_id_type' => 0,
'ben_id_number' => 1
);
$newarray = array_filter($array);
print_r($newarray);
Array
(
[id] => 1
[comp_id] => 1
[source_of_funds] => 1
[ben_id_number] => 1
)
Try this:
$returnArray = array_filter($result);
You can see PHP's array_filter function for more info.
Well, what else are you expecting to happen?
array_push($returnArray, $mainArray);
If you find an element which has >0 value, you push the ENTIRE original array onto the new one, not just the key/value you just tested.
You probably want:
$newarr = array();
foreach($mainArray as $key => $value) {
if ($value > 0) {
$newarr[$key] = $value;
}
}