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

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

Related

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

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);
?>

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.

How to store array in table split by array_chunk in php? [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 4 years ago.
Improve this question
I am getting data in array format.Some times i get duplicate data
after 2nd index. so i decide to split array in 3 index with
array_chunk function of php. But after doing this on foreach loop get
only 0th index. i want to store all 0th,1st,2nd.. in tables with new
row.
Array
(
[0] => Array
(
[0] => xyz
[1] => dublin
[2] => 2
[3] => yxd
[4] => canada
[5] => 2
)
)
I am using array_chunk to split my array.
Array
(
[0] => Array
(
[0] => Suhas shinde
[1] => Jogeshwari
[2] => 2
)
[1] => Array
(
[0] => Suhas shinde
[1] => Jogeshwari
[2] => 2
)
)
Now, i have to store this in table. so i used foreach loop :
foreach ($matches as $value)
{
$share_holder_info->trad_id = $rand_id;
$share_holder_info->name = $value[0];
$share_holder_info->address = $value[1];
$share_holder_info->shares = $value[2];
$share_holder_info->save();
}
It stores only 0th index of array in table.
if (preg_match_all('~(?:\G(?!\A)|In respect of Shareholders)\s*[^:\r\n]+:\h*\K.*~', $string, $matches)) {
$matches = array_chunk($matches[0],3);
//foreach loop
}
You need to change your foreach to this to allow for the creation of new Model instance for each record set.
foreach ($matches as $value) {
$share_holder_info = new ShareHolderInfo();
$share_holder_info->trad_id = $rand_id;
$share_holder_info->name = $value[0];
$share_holder_info->address = $value[1];
$share_holder_info->shares = $value[2];
$share_holder_info->save();
}
You will need to change it to what ever your actual Model is named.
I hope this helps

Placing data into 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 9 years ago.
Improve this question
I have the following code:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = $attributeSelected['id'];
}
?>
This saves the data as:
Array (
[0] => SimpleXMLElement Object ( [0] => 23914175_laptop )
[1] => SimpleXMLElement Object ( [0] => 23914175_laptop )
[2] => SimpleXMLElement Object ( [0] => price_range_10_50004 )
)
However I just want the array data and not to include the "SimpleXMLElement Object". So I added the following code:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = json_decode(json_encode($attributeSelected['id']), true);
}
?>
Which now gives me the data as:
Array (
[0] => Array ( [0] => 23914175_laptop )
[1] => Array ( [0] => 23914175_laptop )
[2] => Array ( [0] => price_range_10_50004 )
)
I'm not sure what I'm doing wrong. I just want to save the data to an array and then use an If statement to check if the array contains specific data.
How about:
$attr_data[] = $attributeSelected['id'][0];
You are trying to store an object, if you need the first value of object you should change the third line to:
$attr_data[] = $attributeSelected['id']->0;
Please add a snippet of your XML, it makes things a lot clearer.
typecast the values to string:
$xml = simplexml_load_string($x); // assume XML in $x
$att = array();
foreach ($xml->search as $a) {
$att[] = (string)$a['id'];
}
see it working: http://codepad.viper-7.com/P0DI0S
The class SimpleXMLElement has a __toString method, so you could call it directly (which is kinda ugly IMO) or just typecast it to a string:
<?php
$attr_data = array();
foreach ($content->search as $attributeSelected)
{
$attr_data[] = (string) $attributeSelected['id'];
// Or
$attr_data[] = $attributeSelected['id']->__toString();
}
?>
Read more at http://php.net/manual/en/simplexmlelement.tostring.php

Categories