简体   繁体   中英

get the value of the key in an array in php without foreach loop and for loop

I have an array and I need to change the value of a column but without looping, because it duplicates the results

 $job_query = $this->db->query('SELECT * from job j , company c where j.company = c.id')->result_array();

 $companyC = $this->db->query('SELECT j.company ,c.title, c.city, c.logo , c.logo , c.contact FROM job j , company c where c.id = j.company')->result_array();

I need the column company in the first query changes to be that

$job_query['company']=$companyC[key];

but I a get the key without loop of array_map

In codeigniter result_array() returns the all records in the form of array.So you have to use foreach loop for accessing columns.But if you want to only the first record then use row_array() as below:

$job_query = $this->db->query('SELECT * from job j , company c where j.company = c.id')->row_array();

  $companyC = $this->db->query('SELECT j.company ,c.title, c.city, c.logo , c.logo , c.contact FROM job j , company c where c.id = j.company')->row_array();

Then

$job_query['company'] = $companyC[key]; 

You can use array_column function for that

array_column($array, $columnname);

$array is the array you have

$columnname is the name of the key

yes i did that and it works the problem is that i had 2 queries but now i fixed it and don't need the first i took the value from the second one any way thanks it helped.

foreach ($job_query as $key => $value) { assigned a anew value here $new = array(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM