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 )
Related
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 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.
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
I've got the following problem.
I would like to split a string at a specific place. It's used to work with a csv file.
Following situation:
My CSV File goes like this:
33333|'AB'|'01.01.2014'|'short'44444|'AB'|'01.01.2014'|'short'11111|'AB'|'01.01.2014'|'short'
This means: I've got an ID at the beginning containing 5 digits, and I want to split this string and parse it into an array. I would like to use preg_split but how could I do this? I think of matching this part:
'44444|
And now the special wish: I would like to get this 5 digits into my array. So I want to get access to the part, that preg_split splices - is this possible?
Thank you very much in advance
Is that what you want?
$str = "33333|'AB'|'01.01.2014'|'short'44444|'AB'|'01.01.2014'|'short'11111|'AB'|'01.01.2014'|'short'";
$res = preg_split('/(\b\d{5}\|)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($res);
output:
Array
(
[0] => 33333|
[1] => 'AB'|'01.01.2014'|'short'
[2] => 44444|
[3] => 'AB'|'01.01.2014'|'short'
[4] => 11111|
[5] => 'AB'|'01.01.2014'|'short'
)
You don't necessarily need regex here:
$array = explode('|', $your_csv_data);
will give you an array of the csv values. This of course only works if the pipe character cannot appear in the actual values. If you want the first item as the key for the dataset, you could do this:
$key = array_shift($array);
$list_of_csv_datas[$key] = $array;
Please correct me if I misunderstood you.
I think you are trying to split the strings according to boundary which was present just before to the 5 digit number.
<?php
$string="33333|'AB'|'01.01.2014'|'short'44444|'AB'|'01.01.2014'|'short'11111|'AB'|'01.01.2014'|'short'";
$regex = '~(?<!^)(?=\d{5})~';
$splits = preg_split($regex, $string);
print_r($splits);
?>
Output:
Array
(
[0] => 33333|'AB'|'01.01.2014'|'short'
[1] => 44444|'AB'|'01.01.2014'|'short'
[2] => 11111|'AB'|'01.01.2014'|'short'
)
This is really a parsing problem, not a regular expression problem. PHP has several built-in functions for parsing CSV data:
str_getcsv
fgetcsv
If you want to parse the entire row into an array, you can use str_getcsv() like so:
$csv = "33333|'AB'|'01.01.2014'|'short'44444|'AB'|'01.01.2014'|'short'11111|'AB'|'01.01.2014'|'short'";
$csvArray = str_getcsv($csv, "|", "'");
print_r($csvArray);
Which gives you:
Array
(
[0] => 33333
[1] => AB
[2] => 01.01.2014
[3] => short44444
[4] => AB
[5] => 01.01.2014
[6] => short11111
[7] => AB
[8] => 01.01.2014
[9] => short
)
Or, if you really just want the first column, you can just get everything on that line up until the first | delimiter:
echo substr($csv, 0, strpos($csv, '|'));
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 8 years ago.
Improve this question
I have an array, $data, in the form of :
Array
(
[1] => Array
(
[group_exp] => Group 2
[cat_exp] => Category 3
[sub_exp] => Sub Category 4
)
[2] => Array
(
[group_exp] => Group 3
[cat_exp] => Category 4
[sub_exp] => Sub Category 5
)
)
but i want to save this array in this form :
Array
(
[0] => Array
(
[group_exp] => Group 2
[cat_exp] => Category 3
[sub_exp] => Sub Category 4
)
[1] => Array
(
[group_exp] => Group 3
[cat_exp] => Category 4
[sub_exp] => Sub Category 5
)
)
As the comments has shown, use array_values to reorder the indexes if the array already is in the correct order.
$array = array_values($array);
If the array should be sorted by its indices (if their inherent order is not sorted, such as [2], [1], [3], sort the array by key first:
ksort($array);
$array = array_values($array);
If you just want to reduce your indexes by one, rather than all the way to zero, consider using a for loop approach:
$length = count($data);
for($i = 1; $i <= $length; $i++){
$data[$i-1] = $data[$i];
unset($data[$i]);
}
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.