I want to display the key value from a associative array? I have created an array using csv file? - arrays

[0] => Array
(
[0] => name
[1] => email
[2] => contact
[3] => address
)
[1] => Array
(
[0] => santosh
[1] => santoshm9916#gmail.com
[2] => 9035619733
[3] => hennur
)
i want to access the [1] index(Email ID) from this array. please help......
Thank YOU

If you are doing javascript it could be done like this if you know what key to call for your data.
var arr = ([
["name","e-mail","contact","address"],
["santosh","santoshm9916#gmail.com","9035619733","hennur"],
["santosh2","santoshm991622222#gmail.com","90356197332","hennur2"],
["santosh3","santoshm9916#gmail.com","90356197333","hennur3"]
]);
console.log(arr[1][1]);

Your data :
$data = [[
'name',
'email',
'contact',
'address'
],
[
'santosh',
'santoshm9916#gmail.com',
'9035619733',
'hennur'
]
];
PHP Code:
$title = $data[0];
unset($data[0]);
$table = '<table border="1">';
$table .='<tr>';
foreach($title as $k=>$val){
$table.='<td>'.$val.'</td>';
};
$table.='<tr/>';
foreach($data as $key=>$val){
$table.='<tr>';
foreach($title as $k=>$v){
$table.='<td>'.$val[$k].'</td>';
}
$table.='</tr>';
}
$table.='</table>';
echo $table;
Output :
name email contact address
santosh santoshm9916#gmail.com 9035619733 hennur

Related

Remove Sheet Parent Array Import to Collection or Array on Laravel Excel Package

I want import Excel but only on sheet index[0], but when I use toCollection or toArray method the rows wrapped on sheet index array:
I want remove the wrapped parent array, how to do this?
Here is my code:
Import.php
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class Import implements SkipsEmptyRows,
WithCalculatedFormulas,
WithMultipleSheets
{
use Importable;
/**
* Select sheet by index and how they are
* mapped to specific import object.
*
* #return array
*/
public function sheets(): array
{
return [
0 => new static(),
];
}
}
Controller:
$filePath = 'import-excel.xlsx';
$import = new Import();
$rows = $import->toCollection($filePath);
dd($rows->toArray());
Expected Result:
[0] => Array(
[0] => *Contact Name
[1] => *Contact Email
[2] => *Contact Address
)
[1] => Array (
[0] => Talia Oktaviani S.Psi
[1] => nova03#yahoo.com
[2] => Ds. Abdul No. 618 Tarakan 11408 KalU
)
Actual Result:
Array( <== ***I want remove this.***
[0] => Array( <== ***I want remove this.***
[0] => Array(
[0] => *Contact Name
[1] => *Contact Email
[2] => *Contact Address
)
[1] => Array (
[0] => Talia Oktaviani S.Psi
[1] => nova03#yahoo.com
[2] => Ds. Abdul No. 618 Tarakan 11408 KalU
)
) <== ***I want remove this.***
) <== ***I want remove this.***
You can delete if use collapse function
$filePath = 'import-excel.xlsx';
$import = new Import();
$rows = $import->toCollection($filePath)->collapse();
dd($rows->toArray());

Array to object PHP Codeigniter

I have array looking like this
Array ( [0] => diamond.jpg [1] => default1.png [2] => White.png )
i make API, and my client want me to make the array become like this
data : [
{
"image_list": "diamond.jpg",
"image_list": " default1.png",
"image_list": " White.png",
}
]
please help, sorry for my english
Try this.
<?php
$images = ['diamond.jpg','default1.png', 'White.png'];
print_r($images);
$data = array();
foreach($images as $key=>$value)
{
array_push($data,array("image_list"=>$value));
}
echo json_encode($data);
?>

How to use Hash on the query builder results in CakePHP 3.x?

This is my code
$all = $this->find()
->select(['ProductCircles.id', 'ProductCircles.type', 'ProductCircles.name'])
->order(['ProductCircles.type'])
->all()
->toArray();
Log::write('error', $all);
$all = Hash::combine($all, '{n}.ProductCircles.id', '{n}.ProductCircles.name', '{n}.ProductCircles.type');
The $all is an array of ProductCircle entities.
However, the Hash combine is unable to act on the data as I expected.
Please advise.
$all was expected to be an array of ProductCircle entities:
2015-03-15 08:04:36 Error: Array
(
[0] => App\Model\Entity\ProductCircle Object
(
[_accessible:protected] => Array
(
[name] => 1
[type] => 1
[product_count] => 1
[products_in_circles] => 1
)
[_properties:protected] => Array
(
[id] => 27
[type] => MATERIAL
[name] => Wood
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] => App\Model\Entity\ProductCircle
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_registryAlias:protected] => ProductCircles
)
Which is what I expected.
What I want to do with the Hash::combine is to get an array of arrays like this:
$result:
[
[MATERIAL] => [
[27] => [
Wood
]
]
Don't use Hash, but the collection methods that are built into the query object:
$combined = $this->find()
->select(['ProductCircles.id', 'ProductCircles.type', 'ProductCircles.name'])
->order(['ProductCircles.type'])
->combine('id', 'name', 'type')
->toArray();
To expand on José's answer (and using the extract() because that's the one I needed):
$query = $this->MyTable->find('all', [
'contain' => [
'ProductCircles',
],
// ...
]);
return $query->all()->extract('ProductCircles.name');
More info: book.cakephp.org/3/en/core-libraries/collections.html#Cake\Collection\Collection::extract

grocery crud multiselect field

No problem to use this function 'multiselect field' which show on this website:
http://www.grocerycrud.com/documentation/options_functions/field_type
$crud->field_type('fruits','multiselect',
array( "1" => "banana", "2" => "orange", "3" => "apple"));
Next step, i try to extract data from database to replace the 'array' in the formulae above but failed, pls advise.
$this->db->select('employeeNumber');
$a = $this->db->get('employees')->result();
$crud->field_type('firstName', 'multiselect', $a);
I'm getting result like
Array ( [0] => stdClass Object ( [employeeNumber] => 1002 )
[1] => stdClass Object ( [employeeNumber] => 1056 )
Hmm... how to make it into this format, any advise?:
array( "1" => "banana", "2" => "orange", "3" => "apple")
You need to actually do a foreach here. In our case you need to do something like this:
$this->db->select('employeeNumber');
$results = $this->db->get('employees')->result();
$employees_multiselect = array();
foreach ($results as $result) {
$employees_multiselect[$result->employeeNumber] = $result->employeeNumber;
}
$crud->field_type('firstName', 'multiselect', $employees_multiselect);
or it is even better if you have the name of the employer to do something like this:
$this->db->select('employeeNumber, employeeName');
$results = $this->db->get('employees')->result();
$employees_multiselect = array();
foreach ($results as $result) {
$employees_multiselect[$result->employeeNumber] = $result->employeeName;
}
$crud->field_type('firstName', 'multiselect', $employees_multiselect);

Extracting values from arrays in custom fields

I'm trying to come up with a single array of all values in specific custom fields. The values themselves are also arrays. I've tried all sorts of array functions but haven't come across the right one or the right combination. Here is my code thus far:
$args = array(
'post_type' => 'match_report',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'report_home-scorers'
),
array(
'key' => 'report_away-scorers'
)
)
);
$reportscore = new WP_Query($args);
$scorersResults = array();
if ( $reportscore->have_posts() ) {
while ( $reportscore->have_posts() ) {
$reportscore->the_post();
$homescorers = get_post_meta($post->ID,'report_home-scorers',false);
$awayscorers = get_post_meta($post->ID,'report_away-scorers',false);
foreach ($homescorers as $homescorer){
array_push($scorersResults, $homescorer);
}
foreach ($awayscorers as $awayscorer){
array_push($scorersResults, $awayscorer);
}
?>
<?php } wp_reset_postdata(); //endif
}//endwhile
$scorerResults = remove_empty($scorersResults);
function remove_empty($array) {
return array_filter($array, '_remove_empty_internal');
}
function _remove_empty_internal($value) {
return !empty($value) || $value === 0;
}
Here what I get if I print_r($scorerResults); :
Array
(
[1] => Array
(
[0] => 1
[1] => 63
)
[2] => Array
(
[0] => 263
[1] => 195
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] =>
)
)
I just want the values in the internal arrays in an array.
Assuming you want the $scoreResults array to end up as array(1,63,263,195) you could use the array_reduce function like this:
function gatherScores($lhs, $rhs) {
foreach ($rhs as $key => $value)
if ($value)
$lhs[] = $value;
return $lhs;
}
$scorerResults = array_reduce($scorerResults, "gatherScores", array());
I'm not sure what the blank values are in your third and fourth arrays and how they should be handled, so you may need to change the if ($value) condition to check for something different. As it stands it'll obviously also filter out zero scores.

Resources