I have the following array:
array('data' => array('one' => 'first', 'two' => 'second'));
How i can get the value of key 'one' using string:
echo __('data.one');
function __($key) {
$parts = explode('.', $key);
$array = array('data' => array('one' => 'first', 'two' => 'second'));
return ???;
}
Thank u!
Add your own error handling in case the key path isn't in your array, but something like:
$array = array('data' => array('one' => 'first', 'two' => 'second'));
$key = 'data.one';
function find($key, $array) {
$parts = explode('.', $key);
foreach ($parts as $part) {
$array = $array[$part];
}
return $array;
}
$result = find($key, $array);
var_dump($result);
This should work for you:
return $array["data"]["one"];
Also for more information and to learn a little bit see: http://php.net/manual/en/language.types.array.php
And : PHP - Accessing Multidimensional Array Values
EDIT:
This should work for you:
<?php
$str = "data.one";
$keys = explode(".", $str);
$array = array('data' => array('one' => 'first', 'two' => 'second'));
$access = $array;
foreach($keys as $v)
$access = $access[$v];
echo $access;
?>
Related
I am trying to replace an array key (= single letter) with a word. I have a associative array and a $mapKeyArray with the replacement words
$inputArray = ['P' => 72,'T' => 0,'U' => 1,'E' => 0];
function cb_replaceWords($a) {
$mapKeyArray = array('P' => 'Pink', 'T' => 'Top', 'U' => 'Union', 'E' => 'EX');
foreach($a as $key) {
// $a[$key] = $mapKeyArray[$key];
$a[$key];
}
return $a;
}
$outputArray = array_map("cb_replaceWords", $inputArray);
Modify your function as:
function cb_replaceWords($a) {
$result = [];
$mapKeyArray = array('P' => 'Pink', 'T' => 'Top', 'U' => 'Union', 'E' => 'EX');
foreach($a as $key => $value) {
$result[$mapKeyArray[$key]] = $value;
}
return $result;
}
// and there's no need to use array_map
$outputArray = cb_replaceWords($inputArray);
In case your arrays have the same order of keys you can replace all this with array_combine:
$outputArray = array_combine($mapKeyArray, $inputArray);
I have the following array:
array('data' => array('one' => 'first', 'two' => 'second'));
How i can get the value of key 'one' using string:
echo __('data.one');
function __($key) {
$parts = explode('.', $key);
$array = array('data' => array('one' => 'first', 'two' => 'second'));
return ???;
}
Thank u!
Add your own error handling in case the key path isn't in your array, but something like:
$array = array('data' => array('one' => 'first', 'two' => 'second'));
$key = 'data.one';
function find($key, $array) {
$parts = explode('.', $key);
foreach ($parts as $part) {
$array = $array[$part];
}
return $array;
}
$result = find($key, $array);
var_dump($result);
This should work for you:
return $array["data"]["one"];
Also for more information and to learn a little bit see: http://php.net/manual/en/language.types.array.php
And : PHP - Accessing Multidimensional Array Values
EDIT:
This should work for you:
<?php
$str = "data.one";
$keys = explode(".", $str);
$array = array('data' => array('one' => 'first', 'two' => 'second'));
$access = $array;
foreach($keys as $v)
$access = $access[$v];
echo $access;
?>
Do you have an idea, how can a string be converted to a variable, e.g.
there is a string -> $string = 'one|two|three';
there is an array -> $arr = array('one' => array('two' => array('three' => 'WELCOME')));
I want to use all with explode(); exploded values to access the array $arr. I tried this code:
$c = explode('|', $string);
$str = 'arr[' . implode('][', $c) . ']';
echo $$str;
It doesnt work, sadly :( Any ideas?
You're doing it wrong.
You can do what you want with a loop to go through the array level by level
$string = 'one|two|three';
$arr = array('one' => array('two' => array('three' => 'WELCOME')));
$c = explode('|', $string);
$result = $arr;
foreach($c as $key)
$result = $result[$key];
echo $result; // WELCOME
Here's a sort of recursive function:
$ex_keys = array('one', 'two', 'three');
$ex_arr = array('one' => array('two' => array('three' => 'WELCOME')));
function get_recursive_var($keys, $arr) {
if (sizeof($keys) == 1)
return $arr[$keys[0]];
else {
$key = array_shift($keys);
return get_recursive_var($keys, $arr[$key]);
}
}
var_dump(get_recursive_var($ex_keys, $ex_arr));
I'd like two merge two array with a custom order: take one value from array one and then one from array two as following:
$array1 = array('key1' => 'value_1_1', 'key2' => 'value_1_2');
$array2 = array('key1' => 'value_2_1', 'key2' => 'value_2_2');
//merge array with custom order
$array_result = array('key1' => array('value_1_1', 'value_2_1'),
'key2' => array('value_2_1', 'value_2_2')
)
Values are different, keys same on both arrays.
Built-in function
$result = array_merge_recursive($array1, $array2);
$result = array();
foreach(array_keys($array1 + $array2) as $key)
{
if(array_key_exists($key, $array1) && array_key_exists($key, $array2))
$result[$key] = array($array1[$key], $array2[$key]);
else
$result[$key] = array_key_exists($key, $array1) ? $array1[$key] : $array2[$key];
}
Try this :
$array1 = array('key1' => 'value_1_1', 'key2' => 'value_1_2');
$array2 = array('key1' => 'value_2_1', 'key2' => 'value_2_2');
$result = array();
/* Create index for $result */
foreach($array1 as $data => $value) {
$result[$data] = array();
}
/* Craete Value for $result from array 1*/
foreach($array1 as $data => $value) {
array_push($result[$data], $value);
}
/* Craete Value for $result from array 2*/
foreach($array2 as $data => $value) {
array_push($result[$data], $value);
}
How can I search for a key in an array that contains more arrays.
For example I would like to search for "key" in "arr" and return this:
arr["some_inner_array"]["another_array_possible"][key"]
array_key_exists can tell me if it exists, but of course what I really need in the value...
I hope my question is clear...
EDIT:
based on the answer below, I managed to do a recursive function for that:
function look_in_array ( $array, $key ) {
if ( isset($array[$key]) )
return $array[$key];
foreach ($array as $item) {
if (is_array($item)) {
$value = look_in_array ($item,$key);
if ($value)
return $value;
}
}
}
This function should work:
function array_key_exists_recursive($searchKey, $array)
{
$result = false;
foreach($array as $key => $value)
{
if(is_array($array[$key]))
{
$result = array_key_exists_recursive($searchKey, $array[$key]);
}
else if(array_key_exists($searchKey, $array))
{
$result = $array[$searchKey];
}
if($result)
break;
}
return $result;
}
Exmaple:
$array = array( "a" => array("b" => "1", "c" => "2") );
var_dump(array_key_exists_recursive("c", $array)); //Result: 2
You could just ask this:
isset(arr["some_inner_array"]["another_array_possible"]["key"])
Would this work for you? Otherwise maybe explaning a bit better about what you're trying to accomplish would help us help you :)
You need to iterate over all elements in the first arrays and then use array_key_exists():
foreach($arr as $inner1) {
foreach($inner1 as $inner2) {
if array_key_exists($inner2, $key) {
echo $inner2[$key];
break 2; // if you only want the first match
}
}
}
Try this:
/**
* #param $path array containing path
* #param $array search array
* #return element matching path or null
*/
function arr_search($path, &$array){
$tmp = &$array;
for($i = 0; $i < count($path); $i++){
if(!isset($tmp[$path[$i]])) return null;
$tmp = &$tmp[$path[$i]];
}
return $tmp;
}
$arr = array(
'a' => array(
'b' => array(
'c' => 'abc',
),
'd' => array('ad'),
),
10 => array(100, 200, 300),
);
var_dump(arr_search(array('a', 'b', 'c'), $arr));
var_dump(arr_search(array('a', 'd'), $arr));
var_dump(arr_search(array(10, 100), $arr));
var_dump(arr_search(array(10, 1), $arr));
// EDIT previous example was wrong, so here is new one :)
function arr_search($key, $array){
$values = array();
if(array_key_exists($key, $array)) $values[] = $array[$key];
$stack = array_values($array);
while($tmp = array_pop($stack)){
if(is_array($tmp)){
foreach($tmp as &$v){
array_push($stack, $v);
}
if(array_key_exists($key, $tmp)){
$values[] = $tmp[$key];
}
}
}
return $values;
}
$arr = array(
'a' => array(
'b' => array(
'c' => 'abc',
),
'd' => array('ad'),
),
'e' => array(
'a' => array(
'b' => 'abc',
),
'b' => array('xyz'),
),
10 => array(100, 200, 300),
);
var_dump(arr_search('b', $arr));
var_dump(arr_search(0, $arr));