String to array when two comma to one array with php [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 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.

Related

Separate info from an array with to make a comma separated string [closed]

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 last month.
Improve this question
I want to make a string separated by a comma.
Example :
test, sdf
Array (
[0] => Array ( [name] => Test [0] => Test )
[1] => Array ( [name] => sdf [0] => sdf ) )
$List = implode(', ', $Array);
Return : Notice: Array to string conversion
Extract the list of arrays into another array, then perform the implode
Demo
<?php
$Array=[];
$temparray= ["name" => "Test" ];
array_push($Array,$temparray );
$temparray= ["name" => "sdf" ];
array_push($Array,$temparray );
//print_r($Array);
foreach($Array as $key=>$value) {
$newarray[]=$value["name"];
}
$List = implode(', ', $newarray);
var_dump($List);
?>

How to add specific key to array values? [closed]

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 1 year ago.
Improve this question
I stored links to $fotolar variable and I converted to array with explode.
My code:
$ex = explode('<br>', $fotolar);
echo "<pre>";
print_r($ex);
echo "</pre>";
Output:
Array
(
[0] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg
[1] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg
[2] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
)
How can I add "url" key to this array?
For example:
Array
(
[0] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg
)
[1] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg
)
[2] => Array
(
[url] =>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
)
)
After exploded string to array, you can loop through this array to create a new array like this:
<?php
$str = <<<STR
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
STR;
$arr = explode('<br>', $str);
$result = [];
foreach ($arr as $v) {
$result[]['url'] = $v;
}
var_dump($result);
Here is a one liner for you using array_map
$str = <<<STR
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b1f9e4ed8c48-88505152-13660630.jpeg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a438951a1-13859326-45341947.jpg<br>
https://www.mersevkids.com/uploads/images/202112/img_1920x_61b16a3f2df271-08655153-10027442.jpg
STR;
$array = array_map(function($i){return['url'=>$i];},explode('<br>',$str));
Sandbox

How to merge dynamic array into one single array in shopify [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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
im recieveing an output in the following format.When i foreach the loop only one id is iterated i need to combine these array into one single array so i can iterate all the ids.
Array
(
[0] => 6902680092829
)
Array
(
[0] => 6902680125597
)
Array
(
[0] => 6902680158365
)
Array
(
[0] => 6902680223901
)
Array
(
[0] => 6902680256669
)
i want to combine this array into one single array for eg:
array:{
0=>1,
1=>2,
2=>3
}
im adding shopify product id one by one to an array by doing this
$prdid=[];
array_push($prdid,$shopifyCustomers['body']['container']['product']['id']);
You can simply use array_merge()
$arr1 = [ 6902680092829 ];
$arr2 = [ 6902680125597 ];
$arr3 = [ 6902680158365 ];
$arr4 = [ 6902680223901 ];
$arr5 = [ 6902680256669 ];
$newArr = array_merge($arr1, $arr2, $arr3, $arr4, $arr5);

Add one more element to array in loop with PHP [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 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.

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.

Categories