Add double quotes to array elements [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm using Datatables JQuery plugin and requires data elements to be wrapped in double quotes.
Array
(
[0] => ticket #6,2015-05-20T19:36:02Z,open,normal,34
[1] => testing org,2015-05-15T19:20:11Z,closed,,19
[2] => testing ticket,2015-05-20T19:29:09Z,open,normal,29
[3] => testing ticket #2 ,2015-05-20T19:30:55Z,open,normal,30
[4] => ticket #3,2015-05-20T19:33:25Z,open,normal,31
[5] => ticket #4,2015-05-20T19:34:32Z,open,normal,32
[6] => ticket #5,2015-05-20T19:35:03Z,open,normal,33
)
I have tried using implode function, but haven't had success, I'm hoping if someone could help me achieving this task
Desired output:
"ticket #6","2015-05-20T19:36:02Z","open","normal","34"
so that when using json_encode it will be printed like this.
{
"data": [
[
"ticket #6",
"2015-05-20T19:36:02Z",
"open",
"normal",
"34"
]
]
}

This should work for you:
Just go through all of your array elements with array_map() and explode() them, e.g.
$result = array_map(function($v){
return explode(",", $v));
}, $arr);
So with json_encode() you will end up with:
[
["ticket #6","2015-05-20T19:36:02Z","open","normal","34"],
["testing org","2015-05-15T19:20:11Z","closed","","19"],
["testing ticket","2015-05-20T19:29:09Z","open","normal","29"],
["testing ticket #2 ","2015-05-20T19:30:55Z","open","normal","30"],
["ticket #3","2015-05-20T19:33:25Z","open","normal","31"],
["ticket #4","2015-05-20T19:34:32Z","open","normal","32"],
["ticket #5","2015-05-20T19:35:03Z","open","normal","33"]
]

Related

How to add double quote to array values php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I have an array like below format:
Array(
[0] => 25/1
[1] => 10/1
);
But I want to convert the array so it would look like below format
Array(
[0] => "25/1"
[1] => "10/1"
);
I have already tried lots of things about this issue but can't get the desire solution yet.
Anybody help please ?
Thx to the community. 25/1 are allready a string. If you echo 25/1 echo 25/1;you will get 25. PHP will convert it to an integer echo getType(25/1). That means if you #Praful have these values you have already strings.
But in general you can cast (Explicit cast) integer or other types with (string). or you can use the php function strval()
If you really need to wrap array values in double quotes, here is a simple method with array_map():
$current_array = array(red, green);
$new_array = array();
$new_array = array_map(function($rec){
//concatenate record with ""
$result = $rec= '"'.$rec.'"';
return $result;
},$current_array);
print_r($result);
Output will be:
Array ( [0] => "red" [1] => "green" )

Combining two PHP arrays with some conditional addition thrown in [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've got an unusual problem, but I'm fairly certain it's not impossible to solve.
Consider the two arrays below.
Array ( [0] => 1 [1] => 2 [2] => 2 )
Array ( [0] => 879 [1] => 482 [2] => 1616 )
I need to add the values in second array where the values in the first array are the same so that I would end up with...
Array ( [0] => 879 [1] => 2098 )
How might this be accomplished? Thanks in advance!
This isn't a full-proof way of completing this task, but it achieves the goal you desire. What's happening here is we're looping through your first array (the keys) and using the set values of these keys to add the values from the second array:
$new = array();
foreach($keys as $i => $key) {
if(!isset($new[$key])) { $new[$key] = 0; }
$new[$key] += $vals[$i];
}
Example/Demo
Notes
$keys being your first array: $keys = array(1, 2, 2);
$vals being your second array: array (879, 482, 1616);
As I stated, this isn't full-proof. You will need to modify it to ensure integrity, but it is a start that shows the flow of how you can go about doing what you require.

How to use implode function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have this array:
Array ( [e] =>
[r] =>
[c] =>
[Overflow] =>
[f] =>
[b] => [
true] =>
[Stck] =>
[d] =>
[js] =>
)
and i need to get this
e, r, c, Overflow, f, b, true, stck, d, js
The data you want is in the keys of your array, not the values. So you have to implode the keys:
implode(', ', array_keys($array));
use implode() implode — Join array elements with a string
Implode

How to change the structure of an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this array:
[12] => Array
(
[groupID] => 19
[groupApprovalRefID] => 123433322
[jobrequisitionRefID] => eRS/2015/00023/021/HQ
[groupApprovalSubject] => Principle
[jobrequisitionGroupID] => 19
[groupCreatedDate] => 2015-05-13 14:43:55
[groupReference] => HQ/05/2015/016/00016
)
But I need Group ID like this:
[19]=> Array
(
[groupApprovalSubject] => Principle
[jobrequisitionGroupID] => 19
[groupCreatedDate] => 2015-05-13 14:43:55
[groupReference] => HQ/05/2015/016/00016
)
You can loop over each element of your array and create a new array with the indexes you want.
$result_array = array();
foreach ($original_array as $value)
{
$result_array[$value['groupID']] = $value;
}
Note that if groupIP isn't unique, some values may be lost, since PHP arrays have unique indexes.

How can i add key, value in PHP Object? (Codeigniter) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Example,
$params = array($demandnum,$demandnum,$demandnum,$demandnum,$demandnum,$demandnum,$demandnum,$demandnum,$demandnum,$demandnum,$demandnum,$demandnum );
$stmt = $this->db_gcm->query($query, $params);
$test = $stmt->row();
print_r($test);
Result,
stdClass Object( [server_price] => 32398.63 [server_unit] => 2 [network_price] => 0.00
[network_unit] => 3 [storage_price] => 4948.02 [storage_unit] => 2
[loadbalancer_price] => 2200.00 [loadbalancer_unit] => 1
[additionalservice_price] => 105375.60 [additionalservice_unit] => 4 [support_price] => [support_unit] => 0)
I want to add key value in here.
Is there any good idea?
I want to add key value in here.
In where? $test? How about
$test->key = 'value';

Categories