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 7 years ago.
Improve this question
I have array:
Array
(
[0] => #EX-X
[1] => #EX-DURATION:5
[2] => X-MEDIA
[3] => #EXDATE-TIME
[4] => YEAR
[5] => color
[6] => #EX-DURATION:10
)
How to remove everything that begins with #EX in array?
Can't figure out!
Final result should be:
Array
(
[0] => X-MEDIA
[1] => YEAR
[2] => color
)
With array_filter :
$arrayFiltered = array_filter($yourArray, function($val) {
return strpos($val, '#EX') !== 0;
});
$resultArray = array_values($arrayFiltered);
A simple loop with a check for #EX at the start of each value.
$array = array( ... ); //array with "dirty" values
$new_array = array();
foreach($array AS $val)
{
if(substr($val, 0, 3) != '#EX')
{
$new_array[] = $val;
}
}
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
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 confused how to make depth from basic array.
$array = array('736', '827', '831');
With foreach loop, I want final result like this:
Array
(
[736] => Array
(
[827] => Array
(
[831] => Array
(
)
)
)
)
Just like you want them, do this:
array
(
736 => array
(
827 => array
(
831 => array
(
)
)
)
)
or did I misunderstand the question?
using a foreach backwards:
$arr=array(831,827,736);
$newref=array();
foreach($arr as $el)
{
$newref=array($el=>$newref);
}
for reversing an array: $arr=array_reverse($arr);
Possibly not the most efficient, but something like this should do it.
function nestedArray($array) {
$newArray = [];
$pointer = &$newArray;
foreach($array as $value) {
$pointer[$value] = [];
$pointer = &$pointer[$value];
}
return $newArray;
}
$arr = [736, 827, 831];
var_dump(nestedArray($arr));
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.
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 8 years ago.
Improve this question
I have a script that makes an array that comes in like this:
[0] => 1_Result1
[1] => 2_Result2
[2] => 3_Result3
But i want it to come out like this:
[0] => Result1
[1] => Result2
[2] => Result3
How can this be done?
Well it may help to know more specific rules about how to filter the array as well as how it's formed, but to answer your specific question:
PHP 5.4:
array_map(function ($elem) { return explode('_', $elem)[1]; }, $arr)
PHP 5.3:
array_map(function ($elem) {
$elem = explode('_', $elem);
return $elem[1];
}, $arr);
foreach ($array as $key => $item) {
//Cut 2 characters off the start of the string
$array[$key] = substr($item, 2);
}
or if you want to be more fancy and cut off from the _:
foreach ($array as $key => $item) {
//Find position of _ and cut off characters starting from that point
$array[$key] = substr($item, strpos($item, "_"));
}
This will work in any version of PHP 4 and 5.
Here:
<?php
$results = array( "1_result1", "2_result2", "3_result3", "4_reslut4");
$fixed_results = array();
foreach ($results as $result)
{
$fixed_results[]= substr($result, 2);
}
print_r($fixed_results);
?>
Will return
Array
(
[0] => result1
[1] => result2
[2] => result3
[3] => reslut4
)
Warning: This will only work if you know the size of the prefix to be removed (2 in this case)
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 8 years ago.
Improve this question
I am trying to loop over the array called $res, but I want to obtain only the values 6 and 2, for each name it is an id, is its with a foreach o just a normal for? I am confused because the array has two arrays insiden and it increments if I add a new name like 'Joe'
$arrayDirectory = array('Nick', 'Alex');
$res = array();
foreach($arrayDirectory as $user) {
$res[] = $obj->obtainID($user);
}
echo print_r($res);
Array ( [0] => Array ( [0] => Array ( [id_usuario] => 6 [0] => 6 ) ) [1] => Array ( [0] => Array ( [id_usuario] => 2 [0] => 2 ) ) ) 1
foreach ($res as $item) {
echo $item[0][0];
}
Or
foreach ($res as $item) {
echo $item[0]['id_usuario'];
}
Depending on what you are looking for
With PHP5.3 you can also use array_map() with a lambda
$idUsarios = array_map(function ($item) {
return $item[0]['id_usario'];
}, $res);
Change
$res[] = $obj->obtainID($user);
to
$user = $obj->obtainID($user);
$res[] = $user[0]['id_usuario'];