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 2 years ago.
Improve this question
Question:
I have a PHP array like this
Array
(
[read] => 0
[edit_posts] => 1
[delete_posts] => 2
)
I have to change the values 0, 1, 2 into 1 like this
Array
(
[read] => 1
[edit_posts] => 1
[delete_posts] => 1
)
How can I achieve this?
Any help will be appreciated.
Thanks & Regards
Uzair Ahmed
If you want to put the same value to the whole array, run it simply and initialize it
array_walk($array,function(&$value){$value = 1;});
value is passed by reference.
You can change it in multiple ways, but the simplest one is by array_replace() function.
<?php
$initial = [
'read' => 0,
'edit_posts' => 1,
'delete_posts' => 2
];
$update = [
'read' => 1,
'edit_posts' => 1,
'delete_posts' => 1
];
// Old values
var_dump($initial);
// Change with array_replace function same keys
$newArray = array_replace($initial, $update);
// Check if values changed
var_dump($newArray);
Working demo https://repl.it/#kallefrombosnia/array-change
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.
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"]
]
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 8 years ago.
Improve this question
Here is an example:
PHP:::
<?php
$tag = id3_get_tag( "path/to/example.mp3" );
print_r($tag);
?>
response
Array
(
[title] => DN-38416
[artist] => Re:\Legion
[album] => Reflections
[year] => 2004
[genre] => 19
)
I want to set this like
$title => DN-38416
$artist => Re:\Legion
$album => Reflections
If you mean to assign value to new variables, simply do it using the assignment operator = in PHP.
$title = $tag["title"];
$artist = $tag["artist"];
// ... and so on
Read more about assignment operator in 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 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';