Loop into an array of array with PHP - php

I have this array,
$bookings[] = array(
'booking_id' => '1',
'client_name' => 'John',
'client_firstname' => 'Peter',
'days' => array(
array(
'day_id' => '2016-11-23',
'room_id' => '2'
),
array(
'day_id' => '2016-11-24',
'room_id' => '2'
),
array(
'day_id' => '2016-11-25',
'room_id' => '4'
)
)
)
I'm looking for looping into the days array with PHP.
How is it possible please ?
Thanks for your help.

demo
<?php
$bookings[] = array(
'booking_id' => '1',
'client_name' => 'John',
'client_firstname' => 'Peter',
'days' => array(
array(
'day_id' => '2016-11-23',
'room_id' => '2'
),
array(
'day_id' => '2016-11-24',
'room_id' => '2'
),
array(
'day_id' => '2016-11-25',
'room_id' => '4'
)
)
);
foreach($bookings as $booking)
{
foreach($booking['days'] as $day)
{
echo $day['day_id'];
echo "\n";
}
}

Related

Add values from multidimensional array to another array

Hello good morning wherever you are :D, i have a lil problem, i have this code of arrays
$arrayToView is the info of every single user that i want.
$tagsArray are only tags that use every user but i need to merge all the info something like the last array...
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
)
And i have another one like this
$tagsArray= array(
'IVOFACUNDO' = array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' = array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
so the question is how i can merge both arrays obviously respectively with the same admin something like this
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23,
'tags' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44,
'tags' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
)
The key 'tags' need to be created in the merge of every iteration to add and get one array with all the values, how i can do this?
You can try this snippet.
foreach($arrayToView as $key => $arr){
if(array_key_exists($key, $tagsArray)){
$arrayToView[$key]['tags'] = $tagsArray[$key];
}
}
echo '<pre>';print_r($arrayToView);echo '</pre>';
Use php inbuilt function
$result_Arr = array_merge_recursive($arrayToView,$tagsArray);
<?php
$arrayToView = array(
'IVOFACUNDO' => array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' => array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
);
$tagsArray= array(
'IVOFACUNDO' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
);
foreach($arrayToView as $key => $value){
if(isset($tagsArray[$key])){
$arrayToView[$key]['tags'] = array();
foreach($tagsArray[$key] as $key2 => $value2){
$arrayToView[$key]['tags'][$key2] = $tagsArray[$key][$key2];
}
}
}
echo'<pre>';
print_r($arrayToView);
echo'</pre>';
?>

Extracting data from complicated associative array in php and put into new array

I have an complicated array that looks like this:
$input=array(
(int) 0 => array(
'XXX' => array(
'id' => '7',
'p_id' => '1',
'address' => '9463',
'arrival_time' => '2014-05-01 03:30:00'
),
'YYY' => array(
'id' => '1',
'iden' => '1111',
'name' => 'Tom'
)
),
(int) 1 => array(
'XXX' => array(
'id' => '9',
'p_id' => '2',
'address' => '9469',
'arrival_time' => '2014-05-27 16:43:58'
),
'YYY' => array(
'id' => '2',
'iden' => '2222',
'name' => 'Sam'
)
),
(int) 2 => array(
'XXX' => array(
'id' => '3',
'p_id' => '3',
'address' => '9462',
'arrival_time' => '2014-04-21 14:05:00'
),
'YYY' => array(
'id' => '3',
'iden' => '3333',
'name' => 'James'
)
)
)
I would like to convert it such that it looks like this;
$output=array(
(int) 0 => array(
'name' => 'Tom',
'iden' => '1111',
'address' => '9463'
),
(int) 1 => array(
'name' => 'Sam',
'iden' => '2222',
'address' => '9469'
),
(int) 2 => array(
'name' => 'James',
'iden' => '3333',
'address' => '9462'
)
I wrote some code to solve this problem:
foreach ( $input as $key => $value)
{
$output['name']=$input[$key]['YYY']['name'];
$output['iden']=$input[$key]['YYY']['iden'];
$output['address']=$input[$key]['XXX']['address'];
}
Unfortunately, it retrieves only the last element of the input array.
Can someone more experienced help?
Thank you very much.
You are overwriting the values in each iteration, as you always write to $output['name'] etc.
foreach ( $input as $key => $value)
{
$output[$key] = array(
'name' => $value['YYY']['name'],
'iden' => $value['YYY']['iden'],
'address' => $value['XXX']['address']
);
}
The key here is using $output[$key] instead of $output - this way you will add a new element in each iteration.
Also $input[$key] and $value are equivalent, so I used the shorter variant ;)
Try this in your foreach loop :-
foreach ( $input as $key=>$value)
{
$output[$key]['name']=$value['YYY']['name'];
$output[$key]['iden']=$value['YYY']['iden'];
$output[$key]['address']=$value['XXX']['address'];
}
You have to add an index to the array in the foreach: $output[$key]["name"] = ...;

cakephp manually building arrays

I have a relationship where a quote habtm applicants. I am trying to get a quote to save with multiple applicants at once. I already have an array of the applicants I need but I don't know how to format that array to get it to save when I insert it into the quote array.
The applicant array looks like this:
array(
(int) 0 => array(
'Applicant' => array(
'id' => '436',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'george',
'middle_name' => 'a',
'surname' => 'summerlane',
'email' => 'email#q.com',
'landline_number' => '88465120.',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:43:10',
'modified' => '2013-09-21 01:43:10'
)
),
(int) 1 => array(
'Applicant' => array(
'id' => '435',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'mary',
'middle_name' => 's',
'surname' => 'amnn',
'email' => 'some#this.cin',
'landline_number' => '465132',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:41:48',
'modified' => '2013-09-21 01:41:48'
)
),
(int) 2 => array(
'Applicant' => array(
'id' => '66',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => null,
'title' => null,
'first_name' => 'Tania',
'middle_name' => '',
'surname' => 'Humphreys',
'email' => 'purple67#me.com',
'landline_number' => null,
'mobile_number' => '0438854355',
'applicant_type' => 'Main applicant',
'created' => '2012-10-29 00:00:00',
'modified' => '2012-10-21 00:00:00'
)
)
)
I need one that looks like this:
array(
'Applicants' => array(
'id' => 435,
'id' => 436,
'id' => 66
)
)
How might I go about doing this?
Or is there a better way?
When I save a quote the array looks like this:
array(
'QuoteButton' => 'Submit',
'Quote' => array(
'date' => array(
'day' => '13',
'month' => '10',
'year' => '2013'
),
'description' => '',
'quote_accepted' => '0',
'research_accepted' => '0',
'cc_accepted' => '0',
'pesel_accepted' => '0',
'setfees_accepted' => '0',
'total' => '0'
),
'Applicant' => array(
'id' => '66'
),
How do I insert more than one applicant into the array?
An array can't have the same id, but can crate another array like this:
$datas = array(
(int) 0 => array(
'Applicant' => array(
'id' => '436',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'george',
'middle_name' => 'a',
'surname' => 'summerlane',
'email' => 'email#q.com',
'landline_number' => '88465120.',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:43:10',
'modified' => '2013-09-21 01:43:10'
)
),
(int) 1 => array(
'Applicant' => array(
'id' => '435',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => '2013-09-21 01:41:00',
'title' => '',
'first_name' => 'mary',
'middle_name' => 's',
'surname' => 'amnn',
'email' => 'some#this.cin',
'landline_number' => '465132',
'mobile_number' => '',
'applicant_type' => '',
'created' => '2013-09-21 01:41:48',
'modified' => '2013-09-21 01:41:48'
)
),
(int) 2 => array(
'Applicant' => array(
'id' => '66',
'clientcase_id' => '66',
'archive_id' => '1',
'birthdate' => null,
'title' => null,
'first_name' => 'Tania',
'middle_name' => '',
'surname' => 'Humphreys',
'email' => 'purple67#me.com',
'landline_number' => null,
'mobile_number' => '0438854355',
'applicant_type' => 'Main applicant',
'created' => '2012-10-29 00:00:00',
'modified' => '2012-10-21 00:00:00'
)
)
);
$ids = array();
foreach($datas as $data => $applicants) {
$ids[] = $applicants['Applicant']['id'];
}
print_r($ids);
Output:
Array ( [0] => 436 [1] => 435 [2] => 66 )
How to use the ids? Like this:
foreach($ids as $key => $id) {
// do whatever you want with the applicant id
}
like tttony pointed out, array indexes need to be unique so i would:
$ids = array();
foreach($array as $applicant)
$ids[$applicant['Applicant']['id']] = null;
which will output:
Array
(
[436] =>
[435] =>
[66] =>
)
now id is key to $ids array...you could add something else as value rather than null

Printing all array results in a themable way using PHP?

I want to print all results of the taxonomy_vocabulary_11 array (this is a Drupal 7 site).
If I use <?php print render($content['taxonomy_vocabulary_11'][0]['#title']); ?> I get only one result.
I´ve usuccessfully tried
<?php foreach ($content->taxonomy_vocabulary_11 as $key => $value): $terms = $value['#title']; ?>
<?php print $terms; ?>
<?php endforeach?>
I get this error: Notice: Trying to get property of non-object
Now, this is the output I get using devel module (dpm($node);)
(object) array(
'vid' => '5178',
'uid' => '1',
'title' => 'PROYECTO',
'log' => '',
'status' => '1',
'comment' => '2',
'promote' => '0',
'sticky' => '0',
'nid' => '155',
'type' => 'jornadas',
'language' => 'und',
'created' => '1095048000',
'changed' => '1360589684',
'tnid' => '0',
'translate' => '0',
'revision_timestamp' => '1360589684',
'revision_uid' => '1',
'taxonomy_vocabulary_4' => array(),
'taxonomy_vocabulary_6' => array(
'und' => array(
array(
'tid' => '33',
'taxonomy_term' => (object) array(
'tid' => '33',
'vid' => '6',
'name' => 'Jornadas Gratuitas',
'description' => '',
'format' => NULL,
'weight' => '0',
'vocabulary_machine_name' => 'vocabulary_6',
),
),
),
),
'taxonomy_vocabulary_7' => array(
'und' => array(
array(
'tid' => '40',
'taxonomy_term' => (object) array(
'tid' => '40',
'vid' => '7',
'name' => 'Para PH',
'description' => '',
'format' => NULL,
'weight' => '-10',
'vocabulary_machine_name' => 'vocabulary_7',
),
),
),
),
'taxonomy_vocabulary_11' => array(
'und' => array(
array(
'tid' => '262',
'taxonomy_term' => (object) array(
'tid' => '262',
'vid' => '11',
'name' => 'colegios',
'description' => '',
'format' => NULL,
'weight' => '0',
'vocabulary_machine_name' => 'vocabulary_11',
),
),
array(
'tid' => '543',
'taxonomy_term' => (object) array(
'tid' => '543',
'vid' => '11',
'name' => 'derecho',
'description' => '',
'format' => NULL,
'weight' => '0',
'vocabulary_machine_name' => 'vocabulary_11',
),
),
),
),
'body' => array(
'und' => array(
array(
'value' => "
I´ve also tried <?php print render($content['taxonomy_vocabulary_11']); ?> to treat the taxonomy as any other field, but it won´t print anything.
Note: If I go and do print $content['taxonomy_vocabulary_11']; it just print the word array.
What´s wrong in my approach?
Try:
foreach ($content['taxonomy_vocabulary_11'] as $tv11) {
print render($tv11['#title']);
}
I dont use drupal but this should work if i understood how the $content['taxonomy_vocabulary_11'] array is structured.
Just in case anyone needs it:
<?php
$vid = 11; //vocabulary id
$nid = $node->nid; //it looks for the current loaded node
$query = "SELECT tid, name
FROM (
SELECT td.tid AS tid, name
FROM taxonomy_term_data AS td
JOIN taxonomy_index AS tn
ON td.tid = tn.tid
JOIN node AS n
ON n.nid = tn.nid
WHERE td.vid = ". $vid ."
AND n.status = 1
AND n.nid = ".$nid."
GROUP BY td.tid
) AS t
ORDER BY name ASC";
$result = db_query($query);
foreach($result as $term) {
echo l($term->name, "taxonomy/term/$term->tid") . ', ';
}
?>

How to sort this array by date?

I'm trying to sort a array of this kind :
array (
0 =>
array (
'id_ouverture' => 5,
'debut' => '2011-04-25 08:00:00',
'fin' => '2011-04-25 20:00:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
1 =>
array (
'id_ouverture' => 6,
'debut' => '2011-04-18 08:00:00',
'fin' => '2011-04-18 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
2 =>
array (
'id_ouverture' => 7,
'debut' => '2011-05-02 08:00:00',
'fin' => '2011-05-02 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
3 =>
array (
'id_ouverture' => 8,
'debut' => '2011-05-09 08:00:00',
'fin' => '2011-05-09 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
));
I need to sort this array on this key : array[$i]['debut'] on an ascending order.
The results must be :
array (
1 =>
array (
'id_ouverture' => 6,
'debut' => '2011-04-18 08:00:00',
'fin' => '2011-04-18 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
0 =>
array (
'id_ouverture' => 5,
'debut' => '2011-04-25 08:00:00',
'fin' => '2011-04-25 20:00:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
2 =>
array (
'id_ouverture' => 7,
'debut' => '2011-05-02 08:00:00',
'fin' => '2011-05-02 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
),
3 =>
array (
'id_ouverture' => 8,
'debut' => '2011-05-09 08:00:00',
'fin' => '2011-05-09 10:45:00',
'id_salle' =>
array(
'id' => '7',
'nom' => 'BLABLA',
'id_type_salle' => '3',
'visible' => 1,
),
));
Have you an idea to do it ?
You need to use uasort to write a function that will sort the array for you.
http://php.net/manual/en/function.uasort.php
Basically you write your own comparison function and pass it as a callback to the uasort method

Categories