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
Related
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I am selecting values from my database, when I dump the $results I get;
array (
0 =>
(object) array(
'FieldName' => 'certification_name',
'FieldValue' => 'White Belt',
),
1 =>
(object) array(
'FieldName' => 'certification_name',
'FieldValue' => 'Yellow Belt',
),
)
I want to display the following on my page;
Certification List: White Belt, Yellow Belt
I have created a loop but when I echo the result I get this;
Certification List: Array Array
My PHP code;
foreach ($results as $result) {
$name = $result->FieldName;
$value = $result->FieldValue;
$items[] = array(
'name' => $name,
'value' => $value
);
}
$items = implode("\n", $items);
echo 'Certification List: ' .$items;
What do I need to change in order to get this working?
You shouldn't push arrays into $items, just push the values.
foreach ($results as $result) {
$items[] = $result->FieldValue;
}
$item_list = implode(", ", $items);
echo "Certification List: $item_list";
You can also replace the loop with:
$items = array_column($results, 'FieldValue');
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:
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;});
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".
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have one index array which contain associative array. Like this
$arr = array(
array("id" => 1,"name" => "student1"),
array("id" => 2,"name" => "student2"),
array("id" => 3,"name" => "student3"),
);
Now I want output like this
1,2,3 // output
I have solution for it but I am searching for better way to do that. Here it is
$ids = "";
for($i=0;$i<count($arr);$i++)
{
$ids .= $arr[$i]['id'].",";
}
$ids = rtrim($ids,",");
echo $ids; // output : 1,2,3
Is there any better way to achieve it?
Thanks in advance
Alternative using array_map() if you don't have PHP 5.5+:
echo implode(',', array_map(function($v) { return $v['id']; }, $arr));
Demo
If you have php version >= 5.5 then try,
echo implode(",",array_column($arr,'id'));
try this code
<?php
$arr = array(
array("id" => 1,"name" => "student1"),
array("id" => 2,"name" => "student2"),
array("id" => 3,"name" => "student3"),
);
$str = "";
foreach ($arr as $value)
{
$str=$str.$value["id"].",";
}
$str = substr($str, 0, -1);
echo $str;
?>
foreach ($arr as $val){
$ids[]=$val['id'];
}
echo implode(($ids),',');