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
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I also have a problem with the array_filtrer function, more specifically I have an array of free spaces, ex:
Array (
[0] => "val1"
[1] =>
[2] => "val2"
[3] =>
);
When I use the array_filtrer function, it only removes my last key (in the example above, 3) the middle keys remaining there (as in the example above, the key 1), it can create a solution for it so that removes any key with that null space?
I got the array from a .txt file after I rewrote some parts of it, and at the end of the day I ran into such a problem.
$entry = [
0 => 'val1',
1 => false,
2 => "val1",
3 => null,
4 => '',
5 => '0',
6 => 0,
7 => '',
];
print_r(array_filter($entry));
output
Array ( [0] => val1 [2] => val1 )
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 5 years ago.
Improve this question
I have two array I need to compare with in_array but for some reason it cannot compare, only if I replace manually like this in_array(294, $answer) it shows true.
$answers ----> // Array ( [0] => 294 [1] => 296 )
$answer ---> // Array ( [id] => 294 [question_id] => 87 [correct] => 1 )
in_array($answers, $answer)
It seems you have an answer ($answer = array('id' => 294, 'question_id' => 87, 'correct' => 1 );) identified by id (value 294) and you need to find out if this answer is one of the valid answers whose ids are stored in a list ($answers = array(294, 296);).
The PHP function in_array() is the tool to use, just use it the right way. Search the ID of $answer into the list of IDs stored in $answers:
$answer = array('id' => 294, 'question_id' => 87, 'correct' => 1 );
$answers = array(294, 296);
if (in_array($answer['id'], $answers)) {
echo("Good answer");
} else {
echo("Answer not found in list");
}
Read more about PHP arrays and don't leave that reading until you reach the "Accessing array elements with square bracket syntax" section.
in_array is used to search a string in an array, so you should try to pass a single value from answers array rather than whole array.
For example -
$answers = array(294, 296);
$answer = array('id' => 294, 'question_id' => 87, 'correct' => 1 );
foreach ( $answers as $answer_id ) {
$check = in_array($answer_id, $answer);
echo "<pre>";
var_dump( $check );
echo "</pre>";
}
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 9 years ago.
Improve this question
How can I transform this array:
Array
(
[0] => 6
[1] => 25
[2] => 29
[3] => 27
[4] => 24
[5] => 7
)
into array a comma separated string list:
6,25,29,27,24,7
without altering the order.
My goal and then I get this string array, but I do not know how to do.
The array is an array already. If you mean you want to turn it into a string, then use the implode function:
$string = implode(",", $array);
The numbers you are seeing first (eg 0 => 6) show the KEY of the element.
You can access individual elements in your code by that key. For example:
echo $array[0];
Would output 6 - the value stored in the element with a key of 0.
Use implode() in php
Try like
$arr = array
(
'0' => 6,
'1' => 25,
'2' => 29,
'3' => 27,
'4' => 24,
'5' => 7
);
$newArr = array();
$newArr[0] = "'".implode("','",$arr)."'";
print_r($newArr);
Output
Array
(
[0] => '6','25','29','27','24','7'
)
implode(',',$array)
Try to print this.
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';