How to Call a function inside an array value using PHP - php

Actually I need to call a function inside an array value.
this my function
public function RelationName()
{
return $this->name .'--'. $this->relationship;
}
I need to call this above function in another function which present same class file
this is my another function:
public static function getClaimerNameList($cat_id, $subcat_id)
{
$out = [];
$emp_code = Employee::find()
->where(['importcompany_id' => $cat_id])
->andWhere(['id' => $subcat_id])
->all();
foreach ($emp_code as $dat => $datas) {
$out[] = ['id' => $datas['id'], 'name' => $datas['name'] ];
if($dat == 0){
$aux = $datas['id'];
}
($datas['id'] == $subcat_id) ? $selected = $subcat_id : $selected = $aux;
}
return $output = [
'output' => $out,
'selected' => $selected
];
}
Actually I need to call function here
foreach ($emp_code as $dat => $datas) {
$out[] = ['id' => $datas['id'], 'name' => $datas['name'] ];
}
instead 'name' => $datas['name'] of this, I need to call a function getRelationName()
without using function am getting output like
id name
1 raja
2 ram
but I don't wanna output like that, if I use a function I will get output like below
id name
1 raja-father
2 ram-son
Help me to sort out this problem

You need to call it like this
foreach ($emp_code as $dat => $datas)
{
$out[] = ['id' => $datas['id'], 'name' => $datas->RelationName() ];
//more code...
}
And RelationName() need to be located in the Employee class and it shouldn't be static

Related

php laravel foreach loop giving only 1 result from the array

what I want to do is to print the data with foreach, but whatever I have done, it prints the last element and not the other one,
where am i going wrong?
I want "PartyIdentification" to return up to each element.
I don't understand if I'm making a mistake in get and sets. Is there a short solution? I want to print more than one property.
the result of my output
i want to do
$aa = array(
0 => ['ID' => ['val' => '4000068418', 'attrs' => ['schemeID="VKN"']]],
1 => ['ID' => ['val' => '12345678901', 'attrs' => ['schemeID="TICARETSICILNO"']]],
2 => ['ID' => ['val' => '132546555', 'attrs' => ['schemeID="MERSISNO"']]]
);
$invoice_AccountSupplierParty_Party = new \Pvs\eFaturaUBL\Party();
$invoice_AccountSupplierParty_Party->PartyIdentification = InvoiceBuilder::getasd($aa);
public static function getasd(array $data)
{
$asd = new Olusturma();
$date = array();
foreach ($data as $datum) {
$asd->getID($data);
}
return $asd->getResult();
}
namespace App\Support\Invoice;
class Olusturma
{
public $contactDetails = array();
public function __construct()
{
$this->contactDetails = new \Erkan\eFaturaUBL\PartyIdentification();
}
public function setOlusturma(): Olusturma
{
return $this;
}
public function getID($data)
{
foreach ($data as $row => $innerArray) {
foreach ($innerArray as $innerRow => $value) {
$this->setID($value);
}
}
return $this;
}
public function setID($data): Olusturma
{
$this->contactDetails->ID = $data;
return $this;
}
public function getResult(): \Erkan\eFaturaUBL\PartyIdentification
{
return $this->contactDetails;
}

While passing values from one function to another function the view files are not loading in codeigniter 4?

I need help. In a Controller when I am calling one function from another function the view files are not loading. Only showing the blank page.
Here in the below code, I need to pass the array $calc from calc() function to sem1() function. The array values are passed in the function but the view files in the sem1() function are not loading, showing only a blank page. I am using CodeIgniter 4. Please help.
public function calc()
{
$calc = [
'regulation' => $this->request->getPost("regulation"),
'branch' => $this->request->getPost("branch"),
'calculation' => $this->request->getPost("calculation"),
"semester" => $this->request->getPost("semester")
];
if ($calc['calculation'] == "GPA") {
if ($calc['semester'] == "Semester 1") {
$this->sem1($calc);
}
}
}
public function sem1($calc = '')
{
$this->builder->select('elective, sub_code, sub_name');
$this->builder->where('semester', $calc['semester']);
$this->builder->where('branch', $calc['branch']);
$query = $this->builder->get();
$isElective = [0, 0, 0, 0, 0];
return view('au/auform', ['query' => $query, 'semester' => $semester, 'isElective' => $isElective]);
}
You should add an echo to render view:
public function calc(){
$calc = [
'regulation' => $this->request->getPost("regulation"),
'branch' => $this->request->getPost("branch"),
'calculation' => $this->request->getPost("calculation"),
"semester" => $this->request->getPost("semester")
];
if ($calc['calculation'] == "GPA") {
if ($calc['semester'] == "Semester 1") {
echo $this->sem1($calc);
}
}
}
For more look up the docs - LINK

php - how to convert 'tree-array' to array

I have gotten the tree-array like that:
Now I want to convert it to an array like this:
array(
1 => array('id'=>'1','parentid'=>0),
2 => array('id'=>'2','parentid'=>0),
3 => array('id'=>'3','parentid'=>1),
4 => array('id'=>'4','parentid'=>1),
5 => array('id'=>'5','parentid'=>2),
6 => array('id'=>'6','parentid'=>3),
7 => array('id'=>'7','parentid'=>3)
);
I had already coded something like this:
private function _getOrderData($datas)
{
$_data = [];
static $i = 0;
foreach ($datas as $data) {
$i++;
$rows = ['id' => $data['id'], 'pid' => isset($data['children']) ? $data['id'] : 0, 'menu_order' => $i];
if(isset($data['children'])) {
$this->_getOrderData($data['children']);
}
$_data[] = $rows;
}
return $_data;
}
But it didn't work
Cry~~
How can I fix my code to get the array? Thanks~
BTW, my English is pool.
So much as I don't know you can read my description of the problem or not.
I had already solved this problem, Thx~
private function _getOrderData($datas, $parentid = 0)
{
$array = [];
foreach ($datas as $val) {
$indata = array("id" => $val["id"], "parentid" => $parentid);
$array[] = $indata;
if (isset($val["children"])) {
$children = $this->_getOrderData($val["children"], $val["id"]);
if ($children) {
$array = array_merge($array, $children);
}
}
}
return $array;
}
The array look like:
array

PHP edit an element of an array and return the array with the edited element

I have a function like the below one that will select from the DB some different values.
One of these values needs to be edited before passing the array to other functions.
The problem is that I don't know how to return the array with the edited element. How can I edit a value in the array and return the full array but with the edited element?
function eGetDashboard($eID, $pdo) {
$dashboard = PDOjoin('event', 'event.*, join_category_event.*, join_event_user.*, _dashboard.*, _dashboard_icons.*', array(
array('LEFT JOIN' => 'join_category_event', 'ON' => 'event.id_event = join_category_event.id_event'),
array('LEFT JOIN' => 'join_event_user', 'ON' => 'event.id_event = join_event_user.id_event'),
array('LEFT JOIN' => '_dashboard', 'ON' => 'join_category_event.id_category = _dashboard.id_category'),
array('LEFT JOIN' => '_dashboard_icons', 'ON' => 'join_category_event.id_category = _dashboard_icons.id_category')
), array('event.id_event' => $eID), $pdo);
foreach ($dashboard as $eDashboard)
$label = eSetDashboardLabel($eDashboard['multimedia_descr'], getLang());
return $dashboard;
}
function eSetDashboardLabel($label, $lang) {
$label = explode(";", $label);
foreach ($label as $labels) {
if (substr($labels, 1, 2) == $lang)
return substr($labels, 4);
}
}
By using a Reference (note: &) to the element, any changes will affect the original element:
foreach ($dashboard as &$eDashboard) {
$eDashboard['multimedia_descr'] = eSetDashboardLabel($eDashboard['multimedia_descr'], getLang());
}
Alternatively, you can capture the key in the loop and reference the array:
foreach ($dashboard as $key => $eDashboard) {
$dashboard[$key]['multimedia_descr'] = eSetDashboardLabel($eDashboard['multimedia_descr'], getLang());
}

How to modify output of certain function of File_CSV_DataSource?

The following function is from a PEAR script called File_CSV_DataSource, and is a CSV parser.
CSV file is this:
name,age,skill
john,13,knows magic
tanaka,8,makes sushi
jose,5,dances salsa
The current output of this function is like this:
array (0 => array ('name' => 'john','age' => '13','skill' => 'knows magic',),1 =>array('name' => 'tanaka','age' => '8','skill' => 'makes sushi',),2 =>array ('name' => 'jose','age' => '5','skill' => 'dances salsa',),)
what I'm trying to achieve is to make the function to output like this:
array (array ('john','13','knows magic'), array('tanaka','8','makes sushi'), array ( 'jose','5','dances salsa'),)
so without the 0 => and without the column header => , only the value of the cells
Is there any way for me to modify the function bellow to make it output as in my example above?
public function connect($columns = array())
{
if (!$this->isSymmetric()) {
return array();
}
if (!is_array($columns)) {
return array();
}
if ($columns === array()) {
$columns = $this->headers;
}
$ret_arr = array();
foreach ($this->rows as $record) {
$item_array = array();
foreach ($record as $column => $value) {
$header = $this->headers[$column];
if (in_array($header, $columns)) {
$item_array[$header] = $value;
}
}
// do not append empty results
if ($item_array !== array()) {
array_push($ret_arr, $item_array);
}
}
return $ret_arr;
}
Thanks in advance
Just use the built in filegetcsv. It will do that for you.

Categories