Words to array function [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 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.

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 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.

Regular Expression to split at number with 5 digits (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 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, '|'));

transform array in 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 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.

Categories