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

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" )

Related

String to array when two comma to one array with php [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 2 months ago.
Improve this question
my string has
$string = "apple,banana,orange,lemon";
I want to as
$array = [
[apple,banana],
[orange,lemon]
]
Use array_chunk in combination with explode defined in php https://www.php.net/manual/en/function.array-chunk.php
<?php
$string = "apple,banana,orange,lemon";
$input_array = explode (",", $string);
print_r(array_chunk($input_array, 2));
?>
Will output as below:
Array
(
[0] => Array
(
[0] => apple
[1] => banana
)
[1] => Array
(
[0] => orange
[1] => lemon
)
)
I hope this helps you get on your way. To transform a string to an array, you can use
$elements = explode(',', $string);
This will leave you with this array: ($elements == [apple,banana,orange,apple])
From there, you can build it the way you want, like:
$array[] = [$elements[0], $elements[1]];
$array[] = [$elements[2], $elements[3]];
This results in the array you are looking for, but this solution is only for the string you've given with four elements, separated by comma.

Distinct values using PHP [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 3 years ago.
Improve this question
I want to get only the distinct values from $pjt. I have tried the below code:
$unique_pjtdata = array_unique($pjt);
foreach($unique_pjtdata as $val) {
echo $val;
}
I am getting an HTTP Error 500 after trying this code.
Use array_unique().
Example:
$pjt = array(1, 2, 2, 3);
$array = array_unique($pjt);
and if you still get error then you need to enable error reporting to know error.
You can use array_flip() to switch the keys and values in the array therefore forcing duplicate values to overwrite into a unique set of keys:
$b = ["test1", "test2", "test1", "test3"];
$b = array_flip($b);
print_r( $b );
//output
Array
(
[test1] => 2
[test2] => 1
[test3] => 3
)
You can then extract the keys using array_keys():
$b = array_keys($b);
print_r($b)
// output
Array
(
[0] => test1
[1] => test2
[2] => test3
)

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.

Add double quotes to array elements [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'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"]
]

Words to array function [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 4 years ago.
Improve this question
Is there a function to convert a group of words to an array in PHP? The goal of my code is to take an input date and to rearrange it. Would it be best simply to use "explode()"?
yes. explode() is essentially what you need, then implode() to get it back into a string.
Something like the following?
$date='2010-10-01'; // YYYY-MM-DD
$dateArray=explode('-',$date); print_r($dateArray);
$dateCattedAgain=implode('-',$dateArray); echo $dateCattedAgain;
You can rearrange the year, month, and day bits as needed.
Cheers!
The best thing to do in this specific situation would be to parse the date and then reformat it as needed. For example:
$d = date_parse('12-12-2009');
print_r($d);
Which yields:
Array
(
[year] => 2009
[month] => 12
[day] => 12
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
This will also help you catch invalid date ranges.
A function you can use is explode(). Before PHP version 5.3.0 the function split() was also useful for this, but it's now deprecated.

Categories