This question already has answers here:
PHP: Adding prefix strings to array values
(4 answers)
Closed 5 years ago.
I have an array that needs some modification, I need to add a word before every value. Is there a easy way to do this?
[
'site'=>'some-slug-of-page',
'site'=>'some-slug-new-page',
'site'=>'my-page'
]
needs to be
[
'site'=>'blog/some-slug-of-page',
'site'=>'blog/some-slug-new-page',
'site'=>'blog/my-page'
]
For the sake of immutability, I create a new array, reusing the old keys.
$originalArr = [
'site1' => 'some-slug-of-page',
'site2' => 'some-slug-new-page',
'site3' => 'my-page'
];
$newArr = [];
foreach ($originalArr as $key => $value) {
$newArr[$key] = 'blog/' . $value;
}
array_walk($arr, function (& $val, $key) { $val = 'blog/' . $val;});
Related
This question already has answers here:
Filter multidimensional array based on partial match of search value
(3 answers)
Closed 8 months ago.
I'm trying to get the data in the array according to the input value, like the example below, if I enter the correct value, it will get the value, but when I enter the incorrect value, it doesn't work.
You can see a small example below.
<?php
echo "<pre>";
$array = [
[
"id" => "33704",
"name" => "Total apple"
],
[
"id" => "33706",
"name" => "Used apple"
],
[
"id" => "33694",
"name" => "banana: Bits received"
],
[
"id" => "33697",
"name" => "banana: Bits sent"
]
];
print_r($array);
function searchItem($array, $key, $value)
{
$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
foreach ($array as $subarray) {
$results = array_merge($results, searchItem($subarray, $key, $value));
}
}
return $results;
}
$result = searchItem($array, 'name', 'Bits received');
print_r($result);
When I enter the exact value 'banana: Bits received' it can be retrieved, but the value is many and unlimited, I want when I enter 'Bits received' it must also be found.
Hope everyone can help. Thanks!
you can use strstr or strpos or even preg_match to achieve this.
foreach ($array as $sub) {
if (strstr($sub['name'], $value) !== false) {
// founded value
}
}
however, this may be a more complicated task, you may need to include more advanced techs with this, like databases & Fulltext search, or some indexing softwares like elastic search and so on.
There are built-in functions for this: array_filter and str_contains:
$result = array_filter(
$array,
function ($item) {
return str_contains($item['name'], 'Bits received');
}
);
The same solution with an arrow function:
$result = array_filter(
$array,
fn($item) => str_contains($item['name'], 'Bits received')
);
Moving array_filter into a function:
function searchItem($array, $key, $value) {
return array_filter(
$array,
fn($item) => str_contains($item[$key], $value)
);
}
$result = searchItem($array, 'name', 'Bits received');
print_r($result);
This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed 9 months ago.
0 => array[
"id" => "94e568e2-e5ef-4565-b9d1"
"code" => "8899014500001"
"name" => "John"
"age" => "24" ]
I'm trying to convert an array of users into a CSV file. And i set the array of the key that I want to export.
$fields = ['id', 'name'];
How can I fputcsv in foreach loop return only user id and name from $fields array ?
foreach ($user as $item) {
fputcsv($output, $item);
}
Make an array containing only the fields you want using the $fields array to decide what to place in the output
$fields = ['id', 'name'];
foreach ($users as $user) {
$out = [];
foreach ( $fields as $field){
$out[$field] = $user[$field];
}
fputcsv($output, $out);
}
This question already has answers here:
Fastest way to add prefix to array keys?
(10 answers)
Closed 7 months ago.
I'm working off the following combination of array_combine and array_map - ideally I'd like something more like an array_map_keys to map a function to rename keys. An example:
$input = array(
'a'=>1
'b'=>2
);
$desired_output = array(
'prefix.a'=>1
'prefix.b'=>2
);
function rename_keys($input) {
array_combine(
array_map(
function($col) {
return 'prefix.'.$col;
},
array_keys($input)
),
array_values($input)
);
array_combine isn't necessary. A simple foreach should be enough.
$old = array(
'a'=>1,
'b'=>2
);
$new = array();
foreach ($old as $key => $value) {
$new['prefix.' . $key] = $value;
}
var_dump($new);
output:
array(2) {
["prefix.a"]=>
int(1)
["prefix.b"]=>
int(2)
}
edit;
Your question has already been answered here, including benchmarks for different approches
You just need to for loop it with $key and $value
$a = array('a'=>1,'b'=>2);
echo'<pre>';print_r($a);
$b=array();
foreach($a as $key => $value){
$b['prefix.'.$key] = $value;
}
echo'<pre>';print_r($b);die;
Output:
$a:
Array
(
[a] => 1
[b] => 2
)
$b :
Array
(
[prefix.a] => 1
[prefix.b] => 2
)
This question already has answers here:
Array containing keys and associative array, how to make a relation
(3 answers)
Closed 5 years ago.
I have this array :
$firstArray = [
'location', 'address', 'streetNumber'
];
Extracted from that string variable :
$string = "location.address.streetNumber"
I would like get a value dynamically in an other array with these variables :
$value = $secondArray[$firstArray[0]][$firstArray[1]][$firstArray[2]];`
But without using keys directly ([0], [1], ...).
Is it possible ?
Thanks a lot !
will this do the work for you ?
$string = "location.address.streetNumber";
$firstArray = explode('.', $string);
$secondArray = ['location' => ['address' => ['streetNumber' => 'street 854']]];
$ref = &$secondArray;
foreach($firstArray as $val){
$ref = &$ref[$val];
}
$value = $ref;
unset($ref);
echo $value // street 854
This question already has answers here:
PHP search array in array
(3 answers)
Closed 8 years ago.
I have an array:
$members = array(
'Group1' => array(
'user1',
'user2'
),
'Group2' => array(
'user3',
'user4'
),
'Group3' => array(
'user5'
));
How can I search group name for specific user ?
Simplest (logic wise) way is a quick foreach loop with an in_array() check to find whether or not your name is in the sub array:
$search = 'user4';
foreach($members as $group_name => $names)
if(in_array($search, $names))
echo $search . ' is in ' . $group_name; // user4 is in Group2
Example: https://eval.in/148332
Assuming a user may occur in multiple groups:
$groups = array();
foreach ($members as $group_name => $group_members) {
if (in_array('user4', $group_members)) {
$groups[] = $group_name;
}
}
The variable $groups will contain all the group names that matched, though in the above example that's only "Group2".