It's an interesting situation for getting the array key in multi-dimensional array.
I know how to get the array value by using foreach but how to get the key value and insert into database??
Here is my code:
//Array
$BookingInfo = array(
"115"=>array(
"date"=>array(
"15/12/2014"=>array(//need to get the date but not in here
array(
//need to get the date in here!!
"from"=>2,
"to"=>5,
"user"=>"Ella",
"userid"=>"b2111"
),
array(
"from"=>5,
"to"=>7,
"user"=>"Johnson",
"userid"=>"a2413"
)
),
"16/12/2014"=>array(
array(
"from"=>4,
"to"=>8,
"user"=>"Peter",
"userid"=>"g531"
)
),
"17/12/2014"=>array(
array(
"from"=>1,
"to"=>3,
"user"=>"Chris",
"userid"=>"h024"
),
array(
"from"=>3,
"to"=>6,
"user"=>"Jennifer",
"userid"=>"f314"
)
),
"20/12/2014"=>array(
array(
"from"=>1,
"to"=>5,
"user"=>"Raymond",
"username"=>"r362"
)
),
"21/12/2014"=>array(
array(
"from"=>1,
"to"=>6,
"user"=>"Amy",
"username"=>"a754"
)
),
"23/08/2014"=>array(
array(
"from"=>2,
"to"=>4,
"user"=>"Amy",
"userid"=>"m432"
)
)
)
)
);
The foreach code:
foreach($BookingInfo as $roomNumber => $value){
foreach($value as $id => $val){
foreach($val as $bookDate => $array){
foreach($array as $key => $detail){
foreach($detail as $period =>$info){
//get the $bookDate here
//if I get the "$bookDate" here, it shows the result with repeating 3 times, how can I solve it??
}
}
}
}
}
And I want to get the "15/12/2014" 2 times because of two members' booking, and the "16/12/2014" 1 times, what is the method to do it? Thanks for help.
It's probably easiest to just add the bookDate to the detail array, in the second innermost loop:
foreach($BookingInfo as $roomNumber => $value){
foreach($value as $id => $val){
foreach($val as $bookDate => $array){
foreach($array as $key => $detail){
$detail['bookDate'] = $bookDate;
foreach($detail as $detailkey =>$detailval){
print "$detailkey => $detailval\n";
}
print "***\n";
}
}
}
}
(Just make sure that whatever key you use isn't one that might be in the details array already or you might cause some confusion).
See http://codepad.org/oQT3cmo8 for output
Related
I have a PHP array like this:
$arr = array(
'id' => 'app.settings.value.id',
'title' => 'app.settings.value.title',
'user' => 'app.settings.value.user'
);
I want to remove '.id', '.title' and '.user' in the array values. I want to insert the key of this array at the end of the value. I know, that I can get an array key by passing an array and a value to 'array_search', but I want the key within this one array definition - at the end of it's value. Is this possible?
I don't like this, but I guess it's simple and works:
$arr = array(
'id' => 'app.settings.value',
'title' => 'app.settings.value',
'user' => 'app.settings.value'
);
$result = array();
foreach ($arr AS $key => $value) {
$result[$key] = $value . '.' . $key;
}
You're storing app.settings.value which is the same for all array items, so personally I'd prefer:
$arr = array(
'id',
'title',
'user'
);
function prepend_key($key)
{
return 'app.settings.value.' . $key;
}
$result = array_map('prepend_key', $arr);
With $result containing:
array(
'app.settings.value.id',
'app.settings.value.title',
'app.settings.value.user'
);
At the end of the day, either works :)
$shortcodes['video_section'] = array(
'no_preview' => true,
'params' => 'xxx',
'shortcode' => '[sc1][/sc1]',
'popup_title' => __('Video Section', THEME_NAME),
'shortcode_icon' => __('li_video')
);
$shortcodes['image_section'] = array(
'no_preview' => true,
'params' => 'yyy',
'shortcode' => '[sc2][/sc2]',
'popup_title' => __('Image Section', THEME_NAME),
'shortcode_icon' => __('li_image')
);
$shortcodes[] = $th_shortcodes;
How can I retrieve the name of each array and then access to the key and value:
for example I need to loop throught $shortcode and get the array main name: 'image_section' 'video_section'
Then to retrieve the value of some key. I know how to retrieve key and value but really don't understand how to get the name of the declared array. If i do: var_dump($value); I saw the name of the array but how to access to it?
You can use foreach
foreach($shortcodes as $key => $value) {
echo $key // echoes "vide_section" and "image_section"
foreach($value as $innerKey => $innerValue) {
echo $innerKey // echoes 'no_preview', 'params', 'shortcode', 'popup_title', 'shortcode_icon' twice
}
}
Note that $value in this case refers to arrays, you can foreach again to access the inner values.
Use the array_flip function on the array
$array = array_flip($shortcodes);
Yes you can use foreach, where it goes through each item on array, based on creation order.
foreach($shortcodes as $key => $value) {
// $key represents video_section for 1st iteration and image_section for 2nd.
//Here each are array, so you can again iterate over $value and get each item .
foreach($value as $key2 => $value2) {
//here keys are no_preview, params and so on on subsequent iterations.
}
}
There's array_keys():
$keynames = array_keys($shortcodes);
You could foreach throught it:
foreach($shortcodes as $key=>values){ echo $key; }
or flip the values with the case (though this last one I don't recommend), and than foreach through those. But array flip is best to be left alone in this case (though I nice function to keep in the back of your head).
I have two arrays.
I have a foreach loop which is iterating through one of these arrays ($items). For each value in this array, if a condition is not true, I would like to unset that same key, but from another, similar array ($list). (The two arrays are not identical, but the key will always be the same).
I am unsure how to go about this. The code I used below did not successfully unset the record from the first array.
Both arrays do have keys (IDs), with secondary data.
$list = array(
'id' => '2',
'id' => '3',
'id' => '4',
'id' => '5',
'id' => '6'
);
$items = array(
'id' => '2',
'id' => '3',
'id' => '4',
'id' => '5',
'id' => '6'
);
foreach ($items AS $key => $item)
{
if ($item['id'] != $setting)
{
unset($list[$key]);
}
}
What exactly are the keys in the arrays? Because, you cannot have every key in an array be the same. They overwrite each other. (Not sure if this is just for your example or not).
Anyway, using this code works, with tests to verify...
<?php
$list = array(
2,3,4,5,6
);
$items = array(
2,3,4,5,6
);
$set = 3;
foreach($list as $key => $val){
echo $key . " " . $val . "\n";
}
echo "\n -------------------- \n";
foreach($list as $key => $val){
if ($list[$key] != $set){
unset($items[$key]);
}
}
foreach($items as $key => $val){
echo $key . " " . $val . "\n";
}
?>
So, the actual working code to do what you are attempting to do is this:
$list = array(
2,3,4,5,6
);
$items= array(
2,3,4,5,6
);
$set = 3; //random variable to mock your $setting variable
foreach($list as $key => $val){
if ($list[$key] != $set){
unset($items[$key]);
}
}
This loops through $items and for every value that does not equal $setting, remove from list.
$setting = 3;
$list = array('2','3','4','5','6');
$items = array('2','3','4','5','6');
foreach ($items AS $key => $item)
{
if ($item != $setting)
{
unset($list[$key]);
}
}
print_r($list);
Output:
Array ( [1] => 3 )
So $list now only contains the value 3.
Have an array for a ranking script.
Some times the key will be the same. They are numeric.
When the sort is ran, only non-like values are echoed.
Can't figure out the fix.
$list = array( $value1 => 'text', $value2 => 'text', $value3 => 'text');
krsort($list);
foreach ($list as $key => $frame) {
echo $frame;
}
If you assign two values to the same key in an array, the first value will be overridden by the second. You'll therefore end up with only one value for that key in the array.
To resolve this, I'd suggest to change your array structure like this:
<?php
$list = array( $key1 => array($key1member1, $key2member2),
$key2 => array($key2member1),
$key3 => array($key3member1, $key3member2, $key3member3) );
krsort($list);
foreach ($list as $key => $frames) {
foreach ($frames => $frame) {
echo $frame;
}
}
?>
Going by what you wrote in the comments to this question and my other answer, I'd recommend to switch keys and values.
<?php
$list = array( "frame1" => 4, "frame2" => 2, "frame3" => 99, "frame4" => 42 );
arsort($list);
foreach ($list as $frame => $ranking) {
echo $frame;
}
?>
I was wondering when working with multimedional arrays, if a certain key is the same, is there a way to combine the contents of other keys into its own array if a certain key is the same?
Something like this:
// name is the same in both arrays
array(
array(
'name' => 'Pepsi',
'store' => 'Over here',
'number' => '1234567'
),
array(
'name' => 'Pepsi',
'store' => 'Over here',
'number' => '5556734'
)
)
into something like this
array(
array(
'name' => 'Pepsi',
'store' => array('Over here', 'Over here'),
'number' => array('1234567', '5556734')
)
)
The defining key is checking if the name element is the same for the other arrays.
You can try a function like this.
function mergeByKey($array,$key){
$tmp_array = array();
foreach ( $array as $k => $row ) {
$merged = false;
foreach ($tmp_array as $k2 => $tmp_row){
if ($row[$key] == $tmp_row[$key]){
foreach ( $row as $k3 => $value ) {
if ($k3 == $key) continue;
$tmp_array[$k2][$k3][] = $value;
$merged = true;
}
}
if ($merged) break;
}
if (!$merged) {
$new_row = array();
foreach ( $row as $k4 => $value ) {
if ($k4 == $key) $new_row[$k4] = $value;
else $new_row[$k4] = array($value);
}
$tmp_array[] = $new_row;
}
}
foreach ( $tmp_array as $t => $row ) {
foreach ( $row as $t2 => $value ) {
if ( count($value) == 1 && $t2 != $key ) $tmp_array[$t][$t2] = $value[0];
}
}
return $tmp_array;
}
passing the array as first parameter and the key as second one.
I'm referencing to your array structure
edited: missed a piece
edited2: if resultin array contains elements with one string, it returns a string and not a array with one element
demo
This function uses a given field name as the grouping identifier and turns all other fields into arrays.
Note that single occurrences of your field name will yield arrays with a single element for the other fields. I wasn't sure whether that's a desirable trait, but just making sure you know ;-)
$arr = array(
array(
'name' => 'Pepsi',
'store' => 'Over here',
'number' => '1234567'
),
array(
'name' => 'Pepsi',
'store' => 'Over here',
'number' => '5556734'
)
);
function mergeArray($array, $column)
{
$res = array();
foreach ($array as $item) {
foreach ($item as $key => $value) {
if ($key === $column) {
$res[$column][$key] = $value;
} else {
$res[$column][$key][] = $value;
}
}
}
return array_values($res);
}
print_r(mergeArray($arr, 'name'));
Demo
Thanks to Gianni Lovece for her answer but I was able to develop a much simpler solution based on this problem. Just plug in the $result_arr to browse through and the $key you want to use as basis and it immediately outputs a multidimensional array with non-repeating values for repeating elements (see example below).
function multiarray_merge($result_arr, $key){
foreach($result_arr as $val){
$item = $val[$key];
foreach($val as $k=>$v){
$arr[$item][$k][] = $v;
}
}
// Combine unique entries into a single array
// and non-unique entries into a single element
foreach($arr as $key=>$val){
foreach($val as $k=>$v){
$field = array_unique($v);
if(count($field) == 1){
$field = array_values($field);
$field = $field[0];
$arr[$key][$k] = $field;
} else {
$arr[$key][$k] = $field;
}
}
}
return $arr;
}
For example, in the sample array for this question, running multiarray_merge($mysample, 'name') returns
array(
'Pepsi' => array(
'name' => 'Pepsi',
'store' => 'Over here', // String: Not an array since values are not unique
'number' => array('1234567', '5556734') // Array: Saved as array since values are unique
)
);