$main_array= array(
"Key1" => array(1,2,14,15,16,17,18,19,22,45,47),
"Key2" => array(6,7,40,41,42,43,48,51,52),
"Key3" => array(4,5,8,46,49,53),
"Key4" => array(3,12,13,50),
"Key5" => array(0,9,10,11,20,23,44,),
"Key6" => array(21,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,55,56,57),
"Key7" => array(53)
);
Could you point me how can I get the KeyX value ?
Desired command $getKey(53) - 53 is in the Key7
Try something like this:
foreach ($main_array as $key => $value) {
if(is_array($value) && array_search($search, $value) !== false) {
return $key;
}
}
Related
I have a array structure as shown below.
$arr = [ "level1" =>
["level2" =>
["level3" =>
[ "key1" => "value1",
"key2" => "value2",
]
]
],
[ "level2-A" =>
["level3-A" =>
[ "key1" => "value1-A",
"key2" => "value2-A",
]
]
],
"level2-B" => "value",
"level2-c" => "value",
];
Expected output is as below :
[ "level3" => "value2",
"level3-A" => "value2-A",
"level2-B" => "value",
];
I have tried using array_map_reduce, but I could fetch only till level2.
Use nested loops. Check whether each level is an array before trying to loop through it.
$result = [];
foreach ($arr as $key1 => $level1) {
if (is_array($level1)) {
foreach ($level1 as $key2 => $level2) {
if (is_array($level2)) {
foreach ($level2 as $key3 => $level3) {
foreach ($level3 as $value) {
// loop to the end of the array
}
$result[$key3] = $value;
}
}
}
}
else {
$result[$key1] = $level1;
}
}
The result of this is
Array
(
[level3] => value2
[level3-A] => value2-A
[level2-B] => value
[level2-c] => value
)
I don't know the logic that should prevent adding level2-c to the result.
I have such array:
array:8 [
"text" => "rt_field"
"title" => "rt_field"
"type_id" => "rt_attr_uint"
"author_id" => "rt_attr_uint"
"created_at" => "rt_attr_timestamp"
"recommended_at" => "rt_attr_timestamp"
"deleted_at" => "rt_attr_timestamp"
"feeds" => "rt_attr_multi"
]
I need to get this:
array:10 [
"text" => "rt_attr_string"
"text_txt" => "rt_field"
"title" => "rt_attr_string"
"title_txt" => "rt_field"
"type_id" => "rt_attr_uint"
"author_id" => "rt_attr_uint"
"created_at" => "rt_attr_timestamp"
"recommended_at" => "rt_attr_timestamp"
"deleted_at" => "rt_attr_timestamp"
"feeds" => "rt_attr_multi"
]
I try parse array ($key => $value). When $value == rt_field: I try rename $key to $key.'_txt', and add such key as default (without _txt) with $value = rt_attr_string.
My code:
foreach ($array_param as $key => $value) {
if ($value == 'rt_field'){
$array_param_second[$key] = 'rt_attr_string';
$array_param[$key] = $key.'_txt';
}
}
$result = array_merge($array_param, $array_param_second);
return $result;
But $key in first array doesn't edit.
What I do wrong?
You are editing the value in either array. If you want to update a key, you need to create a new key.
You can just add the keys to a new array, this way there is no need for merging after the foreach.
$result = [];
foreach ($array_param as $key => $value) {
if ($value == 'rt_field'){
$result[$key] = 'rt_attr_string';
$result[$key . '_txt'] = $value;
} else {
$result[$key] = $value;
}
}
Here is your solution....
Your Array
$array[] = array(
"text" => "rt_field",
"title" => "rt_field",
"type_id" => "rt_attr_uint",
"author_id" => "rt_attr_uint",
"created_at" => "rt_attr_timestamp",
"recommended_at" => "rt_attr_timestamp",
"deleted_at" => "rt_attr_timestamp",
"feeds" => "rt_attr_multi"
);
Solution
$new = array();
$keys = array_keys($array[0]);
foreach($array as $r){
for($i=0;$i<count($keys);$i++){
$key = $keys[$i];
if($r[$key]=='rt_field'){
$new[$key.'_txt'] = $r[$key];
$new[$key] = 'rt_attr_string';
}
else $new[$i][$key] = $r[$key];
}
}
echo "<pre>";print_r($new);
I have an array with indexes like
$array = array(
"first_name" => "test",
"last_name" => "testsurename"
);
I need to convert it to:
$array = array(
"0" => array("first_name" => "test"),
"1" => array("last_name" => "testsurename")
);
try this,
$array = array("first_name"=>"test","last_name"=>"testsurename");
$newarray = array();
foreach($array as $key=> $val)
{
$newarray[][$key] = $val;
}
print_r($newarray);
OUTPUT :
$array = array(
"0" => array("first_name" => "test"),
"1" => array("last_name" => "testsurename")
);
DEMO
Another way is to use array_walk, but basically it's all the same.
$array = array("first_name"=>"test","last_name"=>"testsurename");
$result = array();
array_walk($array,
function(&$item, $key, $target) { $target[] = array($key => $item); },
&$result);
You can try
<?php
$array = array("first_name"=>"test","last_name"=>"testsurename");
$final_array = [];
foreach ($array as $key => $value) {
array_push($final_array, [$key => $value]);
}
print_r($final_array);
Output
Array ( [0] => Array ( [first_name] => test ) [1] => Array ( [last_name] => testsurename ) )
$inputArray = [
"first_name" => "test",
"last_name" => "testsurename"
];
$outputArray = array_reduce(array_keys($inputArray), function($carry, $key) use ($inputArray) {
$carry[][$key] = $array[$key];
return $carry;
}, []);
/**
result will be for $outputArray
[
["first_name" => "test"],
["last_name" => "testsurename"]
];
**/
I am testing out a recursive PHP script. But the associative array beginning with "items" key is throwing back the error T_DOUBLE_ARROW. Do I need to use brackets for this portion? The "methods" key is a layer within the "items" keys. Can someone guide me to how I can fix this error? The find_in_arr function works fine when calling name, subject and type keys. But when it gets to items I get the error.
<?php
function find_in_arr($key, $arr)
{
foreach ($arr as $k => $v)
{
if ($k == $key)
{
return $v;
}
if (is_array($v))
{
foreach ($v as $_k => $_v)
{
if ($_k == $key)
{
return $_v;
}
}
}
}
return false;
}
$arr =
array(
"name" => "Php Master",
"subject" => "Php",
"type" => "Articles",
"items" => ("one" => "Iteration","two" => "Recursion",
"methods" => ("factorial" => "Recursion","fibonacci" => "Recursion"),)
"parent"? => "Larry Ullman",
);
var_dump
(
find_in_arr('two', $arr),
find_in_arr('parent', $arr),
find_in_arr('fibonacci', $arr)
//find_in_arr('name', $arr),
//find_in_arr('subject', $arr),
//find_in_arr('type', $arr)
);
It should be:
$arr = array(
"name" => "Php Master",
"subject" => "Php",
"type" => "Articles",
"items" => array(
"one" => "Iteration",
"two" => "Recursion",
"methods" => array(
"factorial" => "Recursion",
"fibonacci" => "Recursion"
)
),
"parent" => "Larry Ullman"
);
I have this following array
$question = array(
"ques_15" => array(
"name" => array(
"0" => "aaa"
)
),
"ques_16" => array(
"name" => array(
"0" => "bbb",
"1" => "ccc"
)
)
);
$i=0;
foreach($question as $k=>$v)
{
echo $question[$k]['name'][$i];
$i++;
}
But my output is only
aaaccc
I am missing the value bbb
You need to iterate the inner 'name' arrays - you could use a nested foreach loop:
$question = array(
"ques_15" => array(
"name" => array(
"0" => "aaa"
)
),
"ques_16" => array(
"name" => array(
"0" => "bbb",
"1" => "ccc"
)
)
);
foreach($question as $quest)
{
foreach($quest['name'] as $val)
{
echo $val;
}
}
you should loop though like so
foreach($question as $q)
{
foreach($q['name'] as $v)
{
echo $v;
}
}
in foreach you don't need a counter $i, it's for while() or for()
you array is two dimensional so you need 2 foreach
Check it out in a functional way.
The shorthand array declaration works only on PHP 5.4+ though, but it still works with your longhand array declaration.
$questions = [
'ques_15' => ['name' => ['aaa']],
'ques_16' => ['name' => ['bbb', 'ccc']]
];
array_map(function($a){
foreach ($a['name'] as $v) echo $v;
}, $questions);