I was wondering if someone could help me with this looping array in PHP as I am still learning the language.
I need to loop into the 3-dimensional (3D) array below in order to:
Check the [room_type_id] values and if its true to a specific value, I would return [check_in_times] array value for this same parent array.
For example:
if [room_type_ids] = 261
I would need to return [check_in_times] = 1
if [room_type_ids] = 36
I would need to return [check_in_times] = 2
My array is:
$myarray = Array (
[0] => Array (
[check_in_times] => Array (
[0] => 1
)
[room_type_ids] => Array (
[0] => 261
[1] => 281
[2] => 283
[3] => 292
[4] => 296
[5] => 365
[6] => 381
[7] => 387
[8] => 389
[9] => 730
)
[season_ids] => Array (
[0] => 0
)
)
[1] => Array (
[check_in_times] => Array (
[0] => 2
)
[room_type_ids] => Array (
[0] => 36
[1] => 38
[2] => 300
[3] => 318
[4] => 336
[5] => 889
[6] => 897
[7] => 728
[8] => 745
[9] => 747
)
[season_ids] => Array (
[0] => 0
)
)
)
Thank you!
Something like:
$room = 261;
$checks = 0;
foreach ($myarray as $subset)
{
if (in_array($room, $subset['room_type_ids']))
{
$checks = $subset['check_in_times'][0];
break;
}
}
Related
Good day. I am new at php and I have a small problem. I have an array.. I want to change stucture of it .. object_type sometimes the same.. I want to have multidimensional array with object_type a key and value all arrays with this object_type.
[0] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 06:24:16
)
[1] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 04:54:54
)
[2] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 04:53:58
)
[3] => Array
(
[initiator_id] => 219
[object_type] => 2
[object_id] => 915
[date] => 2021-11-16 04:53:58
)
Here you can find how to create Multidimensional Arrays. Click here PHP Multidimensional Arrays
And to learn PHP language www.w3schools.com is a very good site. You can find almost every example briefly explained here.
I hope this example may solve your issue.
<?php
$obj = array("initiator_id"=>"35", "object_type"=>"37", "object_id"=>"43");
$obj_2 = array("initiator_id"=>"135", "object_type"=>"237", "object_id"=>"343");
$array_list = array (
array("Test 1",22,18),
array("Test 2",5,2),
array("Test 3",17,15)
);
$array_list[1]["new"] = $obj_2;
$result = array_merge($array_list, $obj);
echo "<Pre>";
print_r($result);
echo "</pre>";
?>
Output:
Array
(
[0] => Array
(
[0] => Test 1
[1] => 22
[2] => 18
)
[1] => Array
(
[0] => Test 2
[1] => 5
[2] => 2
[new] => Array
(
[initiator_id] => 135
[object_type] => 237
[object_id] => 343
)
)
[2] => Array
(
[0] => Test 3
[1] => 17
[2] => 15
)
[initiator_id] => 35
[object_type] => 37
[object_id] => 43
)
I have 2 arrays with me
First array
Array ( [2019-04-29] => Array ( [0] => 366 [1] => 82 [2] => 44 ) [2019-04-30] => Array ( [0] => 330 [1] => 115 [2] => 55 ) )
Second array
Array ( [0] => 492 [1] => 500 )
Need to merge these arrays with output like this.
Array ( [0] => Array ( [0] => 492 [1] => 366 [2] => 82 [3] => 44 ) [1] => Array ( [0] => 500 [1] => 330 [2] => 115 [3] => 55 ) )
Please help.
You can use array_walk and array_merge
$arr1 = Array (
'2019-04-29' => Array (
'0' => 366,
'1' => 82,
'2' => 44
) ,
'2019-04-30' => Array (
'0' => 330,
'1' => 115,
'2' => 55
)
);
$arr2 = Array ( '0' => 492 ,'1' => 500 ) ;
$index = 0;
$res=[];
array_walk($arr1, function($v,$k) use (&$res,$arr2,&$index){
$res[] = array_merge(array($arr2[$index]),$v);
$index++;
});
echo '<pre>';
print_r($res);
Result
Array
(
[0] => Array
(
[0] => 492
[1] => 366
[2] => 82
[3] => 44
)
[1] => Array
(
[0] => 500
[1] => 330
[2] => 115
[3] => 55
)
)
You can do this with a foreach loop and array_merge, after using array_values to reindex $array1 to numeric indexes starting with 0.
$array1 = array_values($array1);
foreach ($array2 as $k => &$v) {
$v = array_merge(array($v), $array1[$k]);
}
print_r($array2);
Output:
Array
(
[0] => Array
(
[0] => 492
[1] => 366
[2] => 82
[3] => 44
)
[1] => Array
(
[0] => 500
[1] => 330
[2] => 115
[3] => 55
)
)
Demo on 3v4l.org
You can use array_walk and array_merge combination with traditional incrementer
array_walk($arr1, function (&$item, $key) use ($arr2,&$i) { // $i should change at memory address
$item = array_merge($item, [$arr2[$i]]);
$i++;
});
Output
Array
(
[2019-04-29] => Array
(
[0] => 366
[1] => 82
[2] => 44
[3] => 492
)
[2019-04-30] => Array
(
[0] => 330
[1] => 115
[2] => 55
[3] => 500
)
)
Demo.
in the following array i want to show from array[0] to array[2] is pack1, array[5] to array[10] pack2, array[10] to array[12] is pack3 pack4 starts from array[14]. (if empty array is above 2 then close the pack and start another pack)
array
(
[0] => array
(
[id] => 1
[num] => 980909
)
[1] => array
(
[id] => 2
[num] => 090909
)
[2] => array
(
[id] => 3
[num] => 909
)
[3] => array
(
)
[4] => array
(
)
[5] => array
(
[id] => 6
[num] => 6565
)
[6] => array
(
[id] => 7
[num] => 6565
)
[7] => array
(
[id] => 8
[num] => 65
)
[8] => array
(
)
[9] => array
(
[id] => 10
[num] => 665
)
[10] => array
(
[id] => 11
[num] => 600
)
[11] => array
(
)
[12] => array
(
)
[13] => array
(
)
[14] => array
(
[id] => 15
[num] => 700
)
$datas = array();
$empcount = 10; $emp = $dIndex = 0;
for($i=0;$i
if( $emp >= $empcount) { $dIndex++; $emp = 0; } $datas[$dIndex][] = $finaldata[$i]; }
What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.
my array is (coming from database)
Array ( [0] => Array ( [date] => 2011-05-12 [user_id] => 48 ) [1] => Array ( [date] => 2011-05-31 [user_id] => 77 ) [2] => Array ( [date] => 2011-05-03 [user_id] => 318 ) [3] => Array ( [date] => 2011-05-20 [user_id] => 619 ) [4] => Array ( [date] => 2011-05-21 [user_id] => 619 ) [5] => Array ( [date] => 2011-05-25 [user_id] => 619 ) [6] => Array ( [date] => 2011-05-28 [user_id] => 619 ) [7] => Array ( [date] => 2011-05-11 [user_id] => 747 ) [8] => Array ( [date] => 2011-05-12 [user_id] => 747 ) [9] => Array ( [date] => 2011-05-29 [user_id] => 747 ) [count] => 10 )
and when i use
foreach($appVenueAmbassador as $kk => $venueUserId){
$unique[] = $appVenueAmbassador[$kk]['user_id'];
}
on that query then i get
Array ( [0] => 48 [1] => 77 [2] => 318 [3] => 619 [4] => 619 [5] => 619 [6] => 619 [7] => 747 [8] => 747 [9] => 747 [10] => )
means last element is coming blank
how can i solve this problem ?
That would be because the last element is [count] => 10 and doesn't have a user_id. You should be seeing a warning regarding this if you'd switch on error reporting. Also, the way you're grabbing that value is quite overcomplicated. Try this:
foreach ($appVenueAmbassador as $venue){
if (isset($venue['user_id'])) {
$unique[] = $venue['user_id'];
}
}
Check to see that the key is a number before using it, or remove the count index from the array before processing.
Here is a way to solve your problem
<?php
for ($i = 0; $i < $appVenueAmbassador['count']; $i++) {
$unique[] = $appVenueAmbassador[$i]['user_id'];
}
?>
It will not use the last key count