Hi i need to print the names of $aSpelers and $aRugnummers when array $Posities of the $aSpelers is 'verdediger'
So for example:
Janmaat 7 Verdediger
de Vrij 3 Verdediger
So the first value of the array doesnt has to print out because it's not a 'verdediger'
Here are the arrays i have to use
$aSpelers = array('Cilessen', 'Janmaat', 'de Vrij' , 'Vlaar', 'Blind', 'de Jong', 'Sneijder');
$aRugnummers = array(1, 7, 3, 2, 5,8, 10 );
$Posities = array('doel', 'verdediging', 'verdediging', 'verdediging', 'verdediging','middenveld','middenveld');
I have to use a foreach loop this is what i have already
foreach()
{
}
I suggest using a multidimensional array. This makes your goal (get it?) a lot easier.
$aSpelers = array(
array(
'naam' => 'Cilessen',
'rugnummer' => '1',
'positie' => 'doel',
),
array(
'naam' => 'Janmaat',
'rugnummer' => '7',
'positie' => 'verdediging',
),
array(
'naam' => 'de Vrij',
'rugnummer' => '3',
'positie' => 'verdediging',
),
array(
'naam' => 'Vlaar',
'rugnummer' => '2',
'positie' => 'verdediging',
),
array(
'naam' => 'Blind',
'rugnummer' => '5',
'positie' => 'verdediging',
),
array(
'naam' => 'de Jong',
'rugnummer' => '8',
'positie' => 'middenveld',
),
array(
'naam' => 'Sneijder',
'rugnummer' => '10',
'positie' => 'middenveld',
),
);
foreach ($aSpelers as $speler) {
if ($speler['positie'] == 'verdediger') {
echo $speler['naam'].' heeft rugnummer '.$speler['rugnummer'].' en speelt positie '.$speler['positie'].'<br />';
}
}
That should do the job.
Edit
I added an if condition to check if the speler is a verdediger and only echo if that is the case.
All array contain same number of value so you can use for loop
for ($i = 0; $i < count($aSpelers); $i++) {
if ($Posities[$i] == "verdediging") {// check condition to match Verdediger
echo $aSpelers[$i] . " " . $aRugnummers[$i] . " " . $Posities[$i];
}
}
Use like this for foreach. change array as object and use
$object = (object) $Posities;
$i=0;
foreach($object as $cont){
if($cont=="verdediging")
echo $aSpelers[$i]." - ".$aRugnummers[$i]." - ".$aRugnummers[$i]."<br>";
$i++;
}
Related
I'm trying to insert multiple rows into MySQL using the 'InsertBatch' function. However, I am unable to make it work. I'm new to this, and I'm not sure if I'm doing it correctly.
I'm using input and select fields with $is data and a table with $tmp data. I used foreach and for statements to get the $tmp_data on the table. An array push() was also used to merge the data into an array.
public function Add_Fees_Matrix() {
$fm_model = new Mod_Fees_Matrix();
$data = [];
//input and select type
$is_data = [
'fm_code' => $this->request->getPost('fm_code'),
'sy_id' => $this->request->getPost('sy_id'),
'dept_id' => $this->request->getPost('dept'),
'gl_id' => $this->request->getPost('gl_id'),
'ppl_id' => $this->request->getPost('ppl_mode')
];
//table
$tmp_data = [
'fcp_description' => $_POST['fcp_description'],
'fmf_amount' => $_POST['fmf_amount']
];
foreach ($tmp_data as $k => $v) {
for($i = 0; $i < count($v);$i++) {
array_push($data, $is_data);
$data[$i][$k] = $v[$i];
}
}
echo "<pre>";
var_export($data);
$fm_model->insertBatch($data);
}
In my output, it generates four arrays. However, only the first two arrays are considered necessary. I think the problem is caused by the array push() function.
Here's the output of var_export():
array (
0 =>
array (
'fm_code' => 'FM-1741195162687292',
'sy_id' => '2',
'dept_id' => '1',
'gl_id' => '2',
'ppl_id' => '1',
'fcp_description' => 'INSTITUTIONAL DEVELOPMENT FEE',
'fmf_amount' => '123',
),
1 =>
array (
'fm_code' => 'FM-1741195162687292',
'sy_id' => '2',
'dept_id' => '1',
'gl_id' => '2',
'ppl_id' => '1',
'fcp_description' => 'MATRICULATION',
'fmf_amount' => '1230',
),
2 =>
array (
'fm_code' => 'FM-1741195162687292',
'sy_id' => '2',
'dept_id' => '1',
'gl_id' => '2',
'ppl_id' => '1',
),
3 =>
array (
'fm_code' => 'FM-1741195162687292',
'sy_id' => '2',
'dept_id' => '1',
'gl_id' => '2',
'ppl_id' => '1',
),
)
Upon clicking a button, can someone help me to insert the data on my database?
This seems to work in my problem.
public function Add_Fees_Matrix() {
$fm_model = new Mod_Fees_Matrix();
$data = [];
$tmp_data = [
'fcp_description' => $this->request->getPost('fcp_description'),
'fcp_amount' => $this->request->getPost('fmf_amount')
];
foreach ($tmp_data as $k => $v) {
for($i = 0; $i < count($v);$i++) {
$data[$i]['fm_code'] = $this->request->getPost('fm_code');
$data[$i]['sy_id'] = $this->request->getPost('sy_id');
$data[$i]['dept_id'] = $this->request->getPost('dept');
$data[$i]['gl_id'] = $this->request->getPost('gl_id');
$data[$i]['ppl_id'] = $this->request->getPost('ppl_mode');
$data[$i][$k] = $v[$i];
}
}
$fm_model->insertBatch($data);
echo "<pre>";
var_export($data);
}
I have an array in a class and want to obtain the 'Apple' key value ('iPhone').
I assume a for each loop needs to be used.
How would I go about doing this?
UPDATE: I also need to specify the customerType and productType key values.
class Products {
public function products_list() {
$customerType = 'national';
$productType = 'phones';
$phoneType = 'Apple';
$productsArr = array();
$productsArr[] = array(
'customerType' => 'national',
'productType' => array(
'phones' => array(
'Apple' => 'iPhone',
'Sony' => 'Xperia',
'Samsung' => 'Galaxy'
),
'cases' => array(
'leather' => 'black',
'plastic' => 'red',
),
),
'international' => array(
'phones' => array(
'BlackBerry' => 'One',
'Google' => 'Pixel',
'Samsung' => 'Note'
),
'cases' => array(
'leather' => 'blue',
'plastic' => 'green'
),
)
);
}
}
I have created a function that you can more or less give it any product and it will return the key and value from the array
<?php
class Products
{
public function products_list()
{
$customerType = 'national';
$productType = 'phones';
$phoneType = 'Apple';
$productsArr[] = array(
'customerType' => 'national', 'productType' => array(
'phones' => array(
'Apple' => 'iPhone',
'Sony' => 'Xperia',
'Samsung' => 'Galaxy'
),
'cases' => array(
'leather' => 'black',
'plastic' => 'red',
),
),
'international' => array(
'phones' => array(
'BlackBerry' => 'One',
'Google' => 'Pixel',
'Samsung' => 'Note'
),
'cases' => array(
'leather' => 'blue',
'plastic' => 'green'
),
)
);
echo $this->get_value($phoneType, $productsArr) .'<br>';
echo $this->get_value($customerType, $productsArr) .'<br>';
echo $this->get_value($productType, $productsArr) .'<br>';
}
function get_value($product, array $products)
{
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($products), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $key => $value) {
if (is_string($value) && ($key == $product)) {
return 'key ->' . $key .' value ->'.$value;
}
}
return "";
}
}
$phone_products = new Products();
$phone_products->products_list();
To use it within the class just call
$this->get_value($phoneType, $productsArr);
from without the class call
$phone_products = new Products();
echo ($phone_products->get_value($phoneType, $productsArr));
//output: key ->Apple value ->iPhone
NB: $phoneType, $productsArr will either be defined the methods they are being used in or passed from other methods or define global variables within the class.
If you want single entry:
echo $productsArr[0]['productType']['phones']['Apple']."<br />";
If there are multiple data, you can use foreach:
foreach ($productsArr as $prod){
echo $prod['productType']['phones']['Apple']."<br />";
}
just try
foreach($productsArr[0]['productType']['phones'] as $phones){
echo $phones[$phoneType]; }
foreach($productsArr as $key1 => $data1 ){
if(is_array($data1))
foreach($data1 as $key2 => $data2 ){
if(is_array($data2))
foreach($data2 as $key3 => $data3 ){
if(array_key_exists('Apple', $data3)) {
echo $data3['Apple'] ."\n";
}
}
}
}
how to check whether 4 exist or not in array at id key position
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
),
)
The array you provided is not a valid multi-dimensional array. You need to add commas after each array in the array. Below i use array_column and in_array without using foreach
<?php
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),//add comma
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
),
);
$filtered = array_column($arr, 'id');//return the id column in this array
if(in_array(4, $filtered)){//check if 4 exists
echo '4 exists';
} else {
echo '4 does not exist';
}
?>
Quite simple, loop through the array and check if the value exists:
$value = 4;
$exists = false;
foreach($arr as $innerArr){
if($innerArr['id'] == $value){
$exists = true;
break;
}
}
If $exists is now true, the value exists within the array.
Try this one, and let me know if you face any problem.
<?php
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
)
);
foreach ($arr as $key => $value) {
if (in_array("4", $value))
{
$sub_index = $value['id'];
echo "Match found at $sub_index";
break;
}
}
Just gotta loop through your array and check existence through in_array() function.
you need something like this
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
)
);
$exists_flag = false;
foreach($arr as $inside_arr)
{
if($inside_arr['other_data'] == 4) {
$exists_flag = true;
break;
}
}
print($exists_flag);
Hope this helps!
As it stand, your array is wrong, you need to separate multi array with commas, you need to not that in_array() will not work with multi array, however you may create a recursive function that will check a provided key does exists or not in an array try code below, hope it helps ,
<?php
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
)
);
function search_items($search_value, $array)
{
foreach ($array as $item) {
if (($item == $search_value) || (is_array($item) && search_items($search_value, $item))) {
return true;
}
}
return false;
}
echo search_items("4", $arr) ? 'item found' : 'item not found';
?>
$result = array_search(4, array_column($arr, 'id'));
If we split this into steps then it would be the following:
$allColumnsNamedId = array_column($arr, 'id'); // find all columns with key 'id'
$resultBoolean = array_search(4, $allColumnsNamedId); //search the array for value 4
if($resultBoolean) {
echo 'Exists';
} else {
echo 'Does not exist';
}
I'm have time overlap check inside while loop and I would also like to display events title with results which is in another array.
Without double foreach loop I can echo $event->title; and it displays all event titles. If I put array $event which has $event->title, inside foreach which I have for overlap testing it only displays first title.
I'm newbie PHP and I'm banging my head with complete solution for a couple of days now. Any help would be awesome :(
while (list(,$event_row) = each($events)) {
$event->loadEvent($event_row[0]);
foreach ($events as $thisevent) {
$event_array = array($event->id,$event->title);
$conflicts = 0;
foreach ($events as $thatevent) {
if ($thisevent[0] === $thatevent[0]) {
continue;
}
$thisevent_from = $thisevent[1];
$thisevent_ends = $thisevent[2];
$thatevent_from = $thatevent[1];
$thatevent_ends = $thatevent[2];
if ($thatevent_ends > $thisevent_from AND $thisevent_ends > $thatevent_from) {
echo $event->title;
$conflicts++;
echo "Event #" . $thisevent[0] . " overlaps with Event # " . $thatevent[0] . "\n";
}
}
if ($conflicts === 0) {
echo "Event #" . $thisevent[0] . " is OK\n";
}
}
}
Without double foreach var_export($event) displays all event arrays.
array( 'title' => 'Event 1', 'description' => 'Event description 1', 'contact' => 'event contact 1', 'url' => '', 'link' => '', 'email' => 'event#eventemail.com', 'picture' => '', 'color' => '#009933', 'catName' => 'Events', 'catDesc' => 'General events', 'startDate' => 1432918800, 'endDate' => 1432926000, 'startDay' => 29, 'startMonth' => 5, 'startYear' => 2015, 'endDay' => 29, 'endMonth' => 5, 'endYear' => 2015, 'recurStartDate' => NULL, 'recurEndDate' => NULL, 'id' => '11', 'catId' => 1, 'status' => true, 'recType' => '', 'recInterval' => 0, 'recEndDate' => 1432850400, 'recEndType' => 0, 'recEndCount' => 1, ))
And var_export($events) array displays three times all arrays with id, start date and end date:
array ( 0 => array ( 0 => '11', 1 => 1432918800, 2 => 1432926000, ), 1 => array ( 0 => '13', 1 => 1432918800, 2 => 1432926000, ), 2 => array ( 0 => '16', 1 => 1432926000, 2 => 1432929600, ), )
Thank you for looking into it and giving any possible solutions hints, ideas :)
I have an array:
$settings = array(
'name' => array(
0 => 'Large Pouch',
1 => 'XL Pouch'
),
'size' => array(
0 => '9x14',
1 => '12x18'
),
'weight' => array(
0 => '10',
1 => '20'
),
'metro_manila_price' => array(
0 => '59',
1 => '79'
),
'luzvimin_price' => array(
0 => '89',
1 => '139'
)
);
I wanted to put the values from that array to one array. $shipping_options with format of
for example:
$shipping_options = array(
'0' => 'Large Pouch 9x14 - $59',
'1' => 'XL Pouch 12x18 - $79'
);
How to program this?
You could write a loop:
$shipping_options = array();
foreach ($settings['name'] as $key => $value) {
$value = sprintf('%s(%s) - $%s',
$value,
$settings['size'][$key],
$settings['metro_manila_price'][$key]);
$shipping_options[$key] = $value;
}
try this one
echo "<pre>";
$size = count($settings['name']);
$shipping_options = array();
for($i=0; $i<$size; $i++)
{
$shipping_options[$i] = $settings['name'][$i]."(".$settings['size'][$i].") - $".$settings['metro_manila_price'][$i];
}
print_r($shipping_options);
You can try this
foreach ($settings['name'] as $key => $value) {
$shipping_options[$key] = $settings['name'][$key] . " " . $settings['size'][$key] . " - $" . $settings['metro_manila_price'][$key];
}