I have created a custom method named "getProductbySku" in a model php.
/admin/model/catalog/product.php
public function getProductbySku($sku) {
$query = $this->db->query("select id from " . DB_PREFIX . "product where sku = '".$sku."'");
if($query->num_rows){
return $query->row['product_id'];
}else{
return false;
}
}
/admin/controller/module/syncproduct.php
$this->load->model("catalog/product");
print_r(get_class_methods($this->model_catalog_product));
$product_id = $this->model_catalog_product->getProductbySku($row['sku']);
if($product_id){
...
and i can't call my method.
Fatal error: Call to undefined method ModelCatalogProduct::getProductbySku() in public_html/admin/controller/module/syncproduct.php on line 204
Array
(
[0] => addProduct
[1] => editProduct
[2] => copyProduct
[3] => deleteProduct
[4] => getProduct
[5] => getProducts
[6] => getProductsByCategoryId
[7] => getProductDescriptions
[8] => getProductCategories
[9] => getProductFilters
[10] => getProductAttributes
[11] => getProductOptions
[12] => getProductOptionValue
[13] => getProductImages
[14] => getProductDiscounts
[15] => getProductSpecials
[16] => getProductRewards
[17] => getProductDownloads
[18] => getProductStores
[19] => getProductLayouts
[20] => getProductRelated
[21] => getRecurrings
[22] => getTotalProducts
[23] => getTotalProductsByTaxClassId
[24] => getTotalProductsByStockStatusId
[25] => getTotalProductsByWeightClassId
[26] => getTotalProductsByLengthClassId
[27] => getTotalProductsByDownloadId
[28] => getTotalProductsByManufacturerId
[29] => getTotalProductsByAttributeId
[30] => getTotalProductsByOptionId
[31] => getTotalProductsByProfileId
[32] => getTotalProductsByLayoutId
[33] => __construct
[34] => __get
[35] => __set
)
So, i added "echo $file" to /system/engine/loader.php
public function model($model, $data = array()) {
// $this->event->trigger('pre.model.' . str_replace('/', '.', (string)$model), $data);
$model = str_replace('../', '', (string)$model);
$file = DIR_APPLICATION . 'model/' . $model . '.php';
echo $file;
...
Nothing changed...
Opencart version is 2.1.0.1
this file is overwrite catalog/product model system/modification/admin/model/catalog/product.php
Related
I want all arrays data in one array not all arrays in one array only the data of all arrays in one array without any other array in php. I have tried to do too many things but still don't work for me.
I also tried
json_encode
with
preg_match
But still don't work for me.
Here is my code
function fetchHashtags($getData){
$body = $getData;
$hashtag_set = [];
$array = explode('#', $body);
foreach ($array as $key => $row) {
$hashtag = [];
if (!empty($row)) {
$hashtag = explode(' ', $row);
$hashtag_set[] = '#' . $hashtag[0];
}
}
print_r($hashtag_set);
}
Output
Array
(
[0] => #Residência
[1] => #architecture
[2] => #casanaserra
[3] => #mountainhouse
[4] => #interiores
[5] => #decoration
[6] => #casanaserra
[7] => #casadecampo
[8] => #construção
[9] => #exterior
[10] => #rustico
[11] => #arpuro
[12] => #home
[13] => #riodejaneiro
[14] => #construir
[15] => #casasincriveis
[16] => #outdoor
[17] => #arquiteto
[18] => #casasincriveis
[19] => #montanhas
[20] => #archdaily
[21] => #architecturelovers
[22] => #arqlovers
[23] => #arqlove
[24] => #homedesign
[25] => #arquiteturaedecoração
)
Array
(
[0] => #We
[1] => #ascaspenvswheaton
)
Array
(
[0] => #شجریان_بشنویم...
[1] => #۰
[2] => #شجریان_بشنویم
[3] => #_
[4] => #شجریانیها
[5] => #همایون_شجریان
[6] => #مژگان_شجریان
[7] => #پرویزمشکاتیان
[8] => #موزیک
[9] => #سهراب_پورناظری
[10] => #محمدرضا_شجریان
[11] => #موزیک_ویدیو
[12] => #ایران
[13] => #ترانه_ماندگار
[14] => #تصنیف
[15] => #آهنگ_ایرانی
[16] => #هنر
[17] => #موسیقی_ایرانی
[18] => #شعروشاعری
[19] => #موسیقی_سنتی_ایران
[20] => #آواز_سنتی
[21] => #قدیمیها
[22] => #دلشدگان
[23] => #دلنشین
[24] => #سینما
[25] => #homayoun_shajarian
[26] => #music
[27] => #mohamadrezashajarian
[28] => #home
[29] => #iran
[30] => #shajarian
)
And one more thing i also want to remove some data that don't look like hashtags.
for example:
Array
(
[0] => #Residência
[1] => architecture // This should be removed
[2] => #casanaserra
[3] => mountainhouse // This also should be removed
[4] => #interiores
)
As you can see from my code you can use array_merge for merge arrays then use array_filter for filter value without # or other rules you need:
$array = [['1', '#2', '3', '#4'], ['5', '#6']];
$flatArray = array_merge(...$array);
$filteredArray = array_filter($flatArray, function($a) {
if (str_contains($a, '#')) {
return $a;
}
});
print_r($filteredArray);
Result:
Array (
[1] => #2
[3] => #4
[5] => #6 )
Reference:
array_merge
array_filter
str_contains
After seeing your code and the context of the situation I changed things:
Instead of use explode i used preg_split for split \n and space;
Delete array_map because you don't need it.
$array = ["
My name is Faraz shaikh i am a php developer and this my #instagram profile.Check out my #merge and my #packages with new #hashtags
#chinese #art #colors #home #harmory #peace #handmade #paintings #etsy
", "
My name is Hunain shaikh i am a php developer and this my #Facebook profile.Check out my #Berge and my #Hackages with new #tags
#english #Kite #colours #me #memory #pee #made #paints #etsafsy
"];
function fetchHashtags($getData) { // This Functions takes out the hashtags from the string and put it in to arrays.
$body = $getData;
$hashtag_set = [];
$array = preg_split('/[\s]+/', $body);
$mapArray = array_map(function($a) {
return str_replace(' ', '', $a);
}, $array);
$filteredArray = array_filter($mapArray, function($a) {
if (str_contains($a, '#')) {
return $a;
}
});
return $filteredArray;
}
$recentNumberOfPosts = 1;
$zeroPost = 0; //Get Post from 0 to recentNumberOfPosts | NOTE: default = 4
$finalArr = [];
while ($zeroPost <= $recentNumberOfPosts) {
// fetchHashtags($hashtagRecent['data'][$zeroPost]['caption']);
$finalArr[] = fetchHashtags($array[$zeroPost]);
$zeroPost++;
}
print_r(array_merge(...$finalArr));
Fiddle
I am working in PHP. I want to get my timezone, Asia/Dhaka, from offset 6x60x60 = 21600. I have tried with timezone_name_from_abbr("", 21600, false);
But it's not working. I'm getting false.
What can I do now?
The official documentation mentions a bug in that function : PHP timezone_name_from_abbr
You can write a function to parse the result of timezone_abbreviations_list() yourself :
/* Takes a GMT offset and returns a timezone name */
function tz_offset_to_name($offset, $dst)
{
$results = array();
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr)
{
foreach ($abbr as $city)
{
if ($city['offset'] == $offset and $city['dst'] == $dst)
{
$results[] = $city['timezone_id'];
}
}
}
return $results;
}
It gives the correct result :
$offset = 6*60*60 ;
$tz = timezone_name_from_abbr("", $offset, false);
var_dump($tz); // false
$tz = tz_offset_to_name($offset, false);
print_r($tz);
(
[0] => Asia/Aqtobe
[1] => Asia/Almaty
[2] => Asia/Dacca
[3] => Asia/Dhaka
[4] => Asia/Thimbu
[5] => Asia/Thimphu
[6] => Asia/Dacca
[7] => Asia/Dhaka
[8] => Asia/Dushanbe
[9] => Asia/Bishkek
[10] => Asia/Hovd
[11] => Indian/Chagos
[12] => Asia/Bishkek
[13] => Asia/Qyzylorda
[14] => Asia/Krasnoyarsk
[15] => Asia/Novokuznetsk
[16] => Asia/Colombo
[17] => Antarctica/Mawson
[18] => Asia/Novosibirsk
[19] => Asia/Novokuznetsk
[20] => Asia/Omsk
[21] => Asia/Qyzylorda
[22] => Asia/Aqtau
[23] => Asia/Samarkand
[24] => Asia/Tashkent
[25] => Asia/Oral
[26] => Antarctica/Vostok
[27] => Asia/Kashgar
[28] => Asia/Urumqi
[29] => Asia/Yekaterinburg
[30] =>
)
I have an array like this ( array generated dynamically from DB)
$attr = array('color'=>array('red','pink','yello','white','black','light-yellow','maroon','neal'),"brand"=>array('nike','adidas','dg','puma','neaon'),"size"=>array(8,10,11,12,13,14,15));
And I need result like
red-nike-8
red-nike-9
red-nike-10
red-nike-11
red-nike-12
red-nike-13
red-nike-14
red-nike-15
red-adidas-8
red-adidas-9
red-adidas-10
red-adidas-11
red-adidas-12
red-adidas-13
red-adidas-14
red-adidas-15
red-dg-8
red-dg-9
red-dg-10
red-dg-11
red-dg-12
red-dg-13
red-dg-14
red-dg-15
red-puma-8
red-puma-9
red-puma-10
red-puma-11
red-puma-12
red-puma-13
red-puma-14
red-puma-15
red-neaon-8
red-neaon-9
red-neaon-10
red-neaon-11
red-neaon-12
red-neaon-13
red-neaon-14
red-neaon-15
pink-nike-8
pink-nike-9
pink-nike-10
pink-nike-11
pink-nike-12
pink-nike-13
pink-nike-14
pink-nike-15
pink-adidas-8
pink-adidas-9
pink-adidas-10
pink-adidas-11
pink-adidas-12
pink-adidas-13
pink-adidas-14
pink-adidas-15........
Below is my code, This work for static but I need to developed to create dynamic structure.
$attr_color = array('red','pink','yello','white','black','light-yellow','maroon','neal');
$attr_brand = array('nike','adidas','dg','puma','neaon');
$attr_size = array(8,10,11,12,13,14,15);
$array = array();
foreach ($attr_color as $key => $value_one)
{
foreach ($attr_brand as $key => $value_two)
{
foreach ($attr_size as $key => $value_three)
{
$array[] = array($value_one,$value_two,$value_three);
}
}
}
As you have mentioned dynamic array in initial, considering that it's a array of depth two containing multiple subarrays, like below code. You can add any other subarray in this, Like I have added others array.
$attr = [
[
'color1',
'color2',
'color3',
],
[
'brand1',
'brand2',
'brand3',
],
[
'size1',
'size2',
'size3',
],
[
'other1',
'other2',
'other3',
],
];
You have to loop like this to achieve the desired result.
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return [];
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = combinations($arrays, $i + 1);
$result = [];
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge([$v], $t) :
[$v, $t];
}
}
$finalArray = [];
foreach($result as $k => $val){
$finalArray[$k] = implode("-",$val);
}
return $finalArray;
}
$result = combinations($attr);
print_r($result);
Final result is listed below.
Array
(
[0] => color1-brand1-size1-other1
[1] => color1-brand1-size1-other2
[2] => color1-brand1-size1-other3
[3] => color1-brand1-size2-other1
[4] => color1-brand1-size2-other2
[5] => color1-brand1-size2-other3
[6] => color1-brand1-size3-other1
[7] => color1-brand1-size3-other2
[8] => color1-brand1-size3-other3
[9] => color1-brand2-size1-other1
[10] => color1-brand2-size1-other2
[11] => color1-brand2-size1-other3
[12] => color1-brand2-size2-other1
[13] => color1-brand2-size2-other2
[14] => color1-brand2-size2-other3
[15] => color1-brand2-size3-other1
[16] => color1-brand2-size3-other2
[17] => color1-brand2-size3-other3
[18] => color1-brand3-size1-other1
[19] => color1-brand3-size1-other2
[20] => color1-brand3-size1-other3
[21] => color1-brand3-size2-other1
[22] => color1-brand3-size2-other2
[23] => color1-brand3-size2-other3
[24] => color1-brand3-size3-other1
[25] => color1-brand3-size3-other2
[26] => color1-brand3-size3-other3
[27] => color2-brand1-size1-other1
[28] => color2-brand1-size1-other2
[29] => color2-brand1-size1-other3
[30] => color2-brand1-size2-other1
[31] => color2-brand1-size2-other2
[32] => color2-brand1-size2-other3
[33] => color2-brand1-size3-other1
[34] => color2-brand1-size3-other2
[35] => color2-brand1-size3-other3
[36] => color2-brand2-size1-other1
[37] => color2-brand2-size1-other2
[38] => color2-brand2-size1-other3
[39] => color2-brand2-size2-other1
[40] => color2-brand2-size2-other2
[41] => color2-brand2-size2-other3
[42] => color2-brand2-size3-other1
[43] => color2-brand2-size3-other2
[44] => color2-brand2-size3-other3
[45] => color2-brand3-size1-other1
[46] => color2-brand3-size1-other2
[47] => color2-brand3-size1-other3
[48] => color2-brand3-size2-other1
[49] => color2-brand3-size2-other2
[50] => color2-brand3-size2-other3
[51] => color2-brand3-size3-other1
[52] => color2-brand3-size3-other2
[53] => color2-brand3-size3-other3
[54] => color3-brand1-size1-other1
[55] => color3-brand1-size1-other2
[56] => color3-brand1-size1-other3
[57] => color3-brand1-size2-other1
[58] => color3-brand1-size2-other2
[59] => color3-brand1-size2-other3
[60] => color3-brand1-size3-other1
[61] => color3-brand1-size3-other2
[62] => color3-brand1-size3-other3
[63] => color3-brand2-size1-other1
[64] => color3-brand2-size1-other2
[65] => color3-brand2-size1-other3
[66] => color3-brand2-size2-other1
[67] => color3-brand2-size2-other2
[68] => color3-brand2-size2-other3
[69] => color3-brand2-size3-other1
[70] => color3-brand2-size3-other2
[71] => color3-brand2-size3-other3
[72] => color3-brand3-size1-other1
[73] => color3-brand3-size1-other2
[74] => color3-brand3-size1-other3
[75] => color3-brand3-size2-other1
[76] => color3-brand3-size2-other2
[77] => color3-brand3-size2-other3
[78] => color3-brand3-size3-other1
[79] => color3-brand3-size3-other2
[80] => color3-brand3-size3-other3
)
Thanks
i create a method that will read a file with the application/octet type and here are some of the code.
Raw data :
GTHHS;MEKID Interface;5496;2012-07-20;
NM1;081;IN1;980898989;2001-01-15;Mr;Gaf;Uhkil;Uhkil,Gaf;PRI;Gaf
$contents = file_get_contents($tmp_filename);
$stringContents = explode(";", $contents);
Now it gives me this output :
Array
(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
NM1
[4] => 081
[5] => IN1
[6] => 980898989
[7] => 2001-01-15
[8] => Mr
[9] => Gaf
[10] => Uhkil
[11] => Uhkil,Gaf
[12] => PRI
[13] => Gaf
PR1
[14] => 081
[15] => IN1
[16] => 20730089
[17] => 7 The Schooner
[18] => Auhaas
[19] => Huuula Ave
[20] =>
[21] => Kishma
PR2
[22] => 081
[23] => IN1
[24] => 232323233
[25] => 400006
[26] => HGD
[27] => M
[28] => M
[29] => 2007-10-16
[30] => 1976-03-31
);
How can i make the NM1, PR1 as the head of array like this :
Array (
[NM1] = array(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
)
);
I am planning also to make the inner array [0]-[3] as json.
If you explode the contents by \n you have each line starting with that identifier. If you then just explode by ; in that line and add it as a sub array, you got it like you want.
This actually looks like a plain old CSV file with your ifentifier in line one. If so, try something like this:
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, ";", "\"", "\n")) !== FALSE)
{
$key = array_shift($row);
$data[$key] = $row;
}
fclose($handle);
}
echo json_encode($data);
http://php.net/manual/en/function.str-getcsv.php
I have an array like this:
Array
(
[0] => Accounts.csv
[1] => Book1.xlsx
[2] => Documents/
[3] => Documents/AdbWinApi.dll
[4] => Documents/AdbWinUsbApi.dll
[5] => Documents/adb.exe
[6] => Documents/boot.img
[7] => Documents/fastboot.exe
[8] => Documents/side_banner.jpg
[9] => Documents/source.properties
[10] => File Organization.docx
[11] => How to Manage Risks.docx
[12] => How to Trade Forex.docx
[13] => Ken Doc - Galad Letter Head.pdf
[14] => Ken Header.png
[15] => MX_2004_fwmx_2004_en.exe
[16] => xx.docx
[17] => asmack-master.zip
[18] => ca5rmhx7l4-Human Heart 2.7z
[19] => evasi0n7.exe
[20] => ken header.pdf
[21] => sp42471.exe
)
and I would like it to return an array like below;
Array
(
[0] => Accounts.csv
[1] => Book1.xlsx
[2] => Documents
[3] => File Organization.docx
[4] => How to Manage Risks.docx
[5] => How to Trade Forex.docx
[6] => Ken Doc - Galad Letter Head.pdf
[7] => Ken Header.png
[8] => MX_2004_fwmx_2004_en.exe
[9] => xx.docx
[10] => asmack-master.zip
[11] => ca5rmhx7l4-Human Heart 2.7z
[12] => evasi0n7.exe
[13] => ken header.pdf
[14] => sp42471.exe
)
I am using the code below;
{
private function listFiles($bucket = null , $prefix = null) {
$ls = S3::getBucket($bucket, $prefix);
if(!empty($ls)) {
foreach($ls as $l) {
$fname = str_replace($prefix,"",$l['name']);
if(!empty($fname)) {
$rv[] = $fname;
}
}
}
if(!empty($rv)) {
return $rv;
}
}
}
What changes can I make to my code to get the above results. I am using Donovan Schönknecht Amazon S3 library for codeigniter.
You could use a foreach
$newarr=array();
foreach($entries as $v)
{
if(strpos($v,'/')!==false)
{
$v=explode('/',$v);
if(isset($v[1]) && strlen($v[1])>0){}else{$newarr[]=$v[0];}
}
else { $newarr[]=$v;}
}
print_r($newarr);
Working Demo
You can overwrite the elements with $array[3] = ""; e.g. Then u have empty array-elements and then you could use a function like this:
function empty_array($array){
foreach($array as $ar => $value) {
if($value == '') {
unset($array[$ar]);
}
}
return $array;
}
$new_array = emtpy_array($old_array);