This question already has answers here:
Add a prefix to each item of a PHP array
(7 answers)
Closed 9 years ago.
I have an array in PHP:
$pbx01_connection = array("customer/voip_extensions.php");
how can i add a prefix and suffix to each item in the array?
for example,
$pbx01_connection = '/admin/'.array("customer/voip_extensions.php");
so /admin/ is added before each item in the array?
Use array_map():
<?php
function addPrefix($value)
{
return '/admin/' . $value
}
$new_array = array_map("addPrefix", $array);
print_r($new_array);
?>
Related
This question already has answers here:
PHP: Sort an array by the length of its values?
(12 answers)
Closed 5 years ago.
I have an array like the following:
$a= $array('PHP','HTML','JS','LARAVEL');
I want to sort the elements in the array, descending by the total number of characters of an element
$b= $array('LARAVEL','HTML','PHP','JS');
Please help me to descend the elements of the array, based on the number of characters in an array.
I see you have the Laravel tag, so you can use Laravel collections with the sortByDesc function for that.
$array = collect(['PHP','HTML','JS','LARAVEL'])->sortByDesc(function($value) {
return strlen($value);
});
You need to use usort():
function sort($a,$b){
return strlen($b)-strlen($a);
}
$array = ['PHP','HTML','JS','LARAVEL'];
usort($array,'sort');
This question already has answers here:
How can I check if an array contains a specific value in php? [duplicate]
(8 answers)
Closed 6 years ago.
I have the following code
$var = "cat";
$array = ["cat", "dog", "mouse"];
How would I check if any item from $array is in $var? Something like
if($array in $var) {
task()
}
in_array is the way to go!
Like this:
if(in_array($var, $array)){
task();
}
in_array($needle, $haystack) searches your Array $haystack for your var $needle. returns true if your Array contains your var.
This question already has answers here:
Iterate in reverse through an array with PHP - SPL solution?
(11 answers)
Closed 7 years ago.
How to do a "for each" from the end of an array in PHP ?
The end function can only return me one element http://php.net/manual/fr/function.end.php
Reverse the array and do a normal foreach?
$newArray = array_reverse($array);
foreach ($newArray as $key => $value) {
...
}
This question already has answers here:
PHP string to array
(4 answers)
Closed 8 years ago.
I have a complete string how can i get some part of it and insert into an array this is my string in php
[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]
i want something similar to this
$value1 = $array[0];
// {"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"}
is this possible to get each value like this
$value1 = $array[0]['albumid'];
// ASaBFzCtl8
Yes use json_decode()
$j = '[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]';
$data = json_decode($j,true);
You can use loop to read the data as
foreach($data as $key=>$val){
echo $val["albumid"]."<br />";
}
Above will just get the albumid you can read whatever you want from this array.
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}