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.
Related
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.
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
I have array like below:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg"]];
i want to add one more element to array $arr by "link"=>"uploads/hello.jpg"
My expected result:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg","link"=>"uploads/hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg","link"=>"uploads/abc.jpg"]];
Any solution for this thank.
You can iterate over the array using foreach, passing a reference into the loop to allow the values to be modified directly:
$arr=[["id"=>"001","name"=>"Hello","pict"=>"hello.jpg"],["id"=>"002","name"=>"Abc","pict"=>"abc.jpg"]];
foreach ($arr as &$a) {
$a['link'] = 'uploads/' . $a['pict'];
}
print_r($arr);
Output:
Array
(
[0] => Array
(
[id] => 001
[name] => Hello
[pict] => hello.jpg
[link] => uploads/hello.jpg
)
[1] => Array
(
[id] => 002
[name] => Abc
[pict] => abc.jpg
[link] => uploads/abc.jpg
)
)
Demo on 3v4l.org
You can iterate over each element in the array and set it that way.
for ($i = 0; $i < count($arr); i++) {
$arr[$i]['link'] = 'uploads/'.$arr[$i]['pict'];
}
foreach($arr as $key => $value){
$arr[$key]['link'] = "uploads/".$value['pict'];
}
Use the foreach loop to modify the original array. The $key value is used to refer to each index in the array.
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 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 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.