Related
I would need to combine two different fields.
In the first field I generate days of the month. I want to list all days of the month.
I would like to add a second field to them, where there are items for each day. But, for example, there are no items on weekends or on other days. Ie. that field two will always have fewer items.
The second field is tightened from the DB.
I would need to do a JOIN like in MySQL for the first field.
It occurred to me that in MySQL it would be possible to make a temporary table with a given month and link it here, but I don't think it's right.
$arrayDate = [0 => '20210401',1 => '20210402',2 => '20210403',3 => '20210404',4 => '20210405',5 => '20210406',6 => '20210407',7 => '20210408',8 => '20210409',9 => '20210410',10 => '20210411',11 => '20210412',12 => '20210413',13 => '20210414',14 => '20210415',15 => '20210416',16 => '20210417',17 => '20210418',18 => '20210419',19 => '20210420',20 => '20210421',21 => '20210422',22 => '20210423',23 => '20210424',24 => '20210425',25 => '20210426',26 => '20210427',27 => '20210428',28 => '20210429',29 => '20210430'];
$arrayItem[35] = ['id' => 35, 'date' => '20210401', 'item' => 'aaaa'];
$arrayItem[36] = ['id' => 36, 'date' => '20210402', 'item' => 'bbbb'];
$arrayItem[37] = ['id' => 36, 'date' => '20210430', 'item' => 'cccc'];
// i need output
20210401 - aaaa
20210402 - bbbb
20210403 - empty
20210404 - empty
...
20210430 - cccc
EDIT: I use nested loops, but I still can't get the right output
foreach ($arrayDate as $date) {
foreach ($arrayItem as $item) {
if ($date == $item['date']) {
bdump($item['date']);
} else {
bdump($date);
}
}
}
bdump($item['date']) = '20210401', '20210402', '20210430'
bdump($date) = '20210401', '20210401', '20210402', '20210402', '20210403', '20210403', '20210403', '20210404', '20210404', '20210404', '20210405', '20210405', '20210405' ....
With array_column you create a array from $arrayItem with date as key.
$dateItem is an array like
array (
20210401 => "aaaa",
20210402 => "bbbb",
20210430 => "cccc",
)
The output you can do with a simple foreach.
$dateItem = array_column($arrayItem,'item','date');
foreach($arrayDate as $date){
echo $date.' '.($dateItem[$date] ?? 'empty')."<br>\n";
}
Note:
With
array_column($arrayItem,null,'date')
you get a two-dimensional array with a date as a key that can be used.
array (
20210401 =>
array (
'id' => 35,
'date' => "20210401",
'item' => "aaaa",
),
20210402 =>
array (
'id' => 36,
'date' => "20210402",
'item' => "bbbb",
),
20210430 =>
array (
'id' => 36,
'date' => "20210430",
'item' => "cccc",
),
)
I have some like this:
$fabrics = array (
'B' => 'BAWEŁNA',
'P' => 'POLIESTER',
'S' => 'SPANDEX',
'E' => 'ELASTAN',
'PO' => 'POLIAMID',
'EL' => 'ELASTAN',
'A' => 'AKRYL',
'AN' => 'ANGORA',
'NY' => 'NYLON',
'W' => 'WEŁNA',
'WO' => 'WEŁNA OWCZA',
'POL' => 'POLIWINYL',
'PVC' => 'PCV',
'SZJ' => 'SZTUCZNY JEDWAB',
'SK' => 'SKÓRA NATURALNA',
'POP' => 'POLIPROPYLEN'
);
$textiles = array( 'B 100%', 'B 80%, P 20%', 'NY 70%, EL 30%', 'B 75%, S 20%', 'SZJ 80%, E 20%');
// rand textilies
$textile = $textiles[array_rand($textiles)];
echo $textile.'</br>';
echo preg_replace(array_keys($fabrics), $fabrics, $textile);
And i want use preg_replace to change a key in full name. But it's not work corectly.:/ Can you help?
Your array keys aren't regular expressions, they're just ordinary strings. You can use strtr() for this.
echo strtr($textile, $fabrics);
With two arguments, the second argument is an associative array of from => to.
I'm running into an issue with work i'm trying to use the value provided by using
$row['make']
and pulling that data which is plain text out of our data base from an import. Now im running into an issue when I run this code i get this
Notice: Undefined index: FORDX in convert.php on line 203
I'm unsure why this is happening, i'm guessing it has something to do with the way i'm accessing the array.
Here's the array
$makes = array(
"ACURA" => "Acura",//does not exist
"AMRTR" => "American",
"ATLAS" => "Atlas",//does not exist
"BRRTR" => "Barrett",
"BMWXX" => "BMW", //does not exist
"BRAE" => "Brae", //does not exist
"BUDDX" => "Budd",
"BUICK" => "Buick", //does not exist
"BUTLR" => "Butler",
"CDLLC" => "Cadillac", //does not exist
"CNDCR" => "Can. Car",//does not exist
"CPCTY" => "Capacity",
"CPTRB" => "Capital",//does not exist
"CARRR" => "Carrier",//does not exist
"CHVRL" => "Chevrolet",
"CHRYS" => "Chrysler", //does not exist
"CLARK" => "Clark", //does not exist
"CLLMN" => "Collins", //does not exist
"CMMRC" => "Commercial Truck", //does not exist
"CMMTR" => "Commercial Truck", //does not exist
"CMMVN" => "Commercial Vans", //does not exist
"CRANE" => "Crane Carrier", //does not exist
"CRNFR" => "Crane Fruehauf", //does not exist
"DATSN" => "Datsun", //does not exist
"DMNDB" => "Diamond B.", //does not exist
"DMNRE" => "Diamond Reo",
"DODGE" => "Dodge",
"DORSY" => "Dorsey",
"DRBTR" => "Durabody", //does not exist
"EASTX" => "East",
"ESPAR" => "Espar", //does not exist
"FNTCN" => "Fontaine",
"FNTTR" => "Fontaine",
"FORDX" => "Ford",
"FRDLT" => "Ford",
"FRGHT" => "Freightliner",
"FRECS" => "Frght Custom Chassis",
"FRUHF" => "Fruehauf",
"FRYEX" => "Frye", //does not exist
"GMXXX" => "GM", //does not exist
"GMCCL" => "GMC",
"GMCXX" => "GMC",
"GRAY" => "Grays", //does not exist
"GRTDN" => "Great Dane",
"GRMAL" => "Grumman", //does not exist
"HEILX" => "Heil",
"HIAB" => "Hiab", //does not exist
"HGHTR" => "Highway", //does not exist
"HINMT" => "Hino", //does not exist
"HBBMF" => "Hobbs",
"HOBBS" => "Hobbs", //does not exist
"HOLND" => "Holland",
"HLLEQ" => "Holland Equipment", //does not exist
"HLLND" => "Holland Hitch Co", //does not exist
"HTCHN" => "Hutchens", //does not exist
"HTCIN" => "Hutchinson", //does not exist
"HYSTER" => "Hyster",
"INGRN" => "Ingersoll Rand",
"IHT" => "International",
"ISUZU" => "Isuzu",
"KNTCK" => "Kentucky",
"KNWRT" => "Kenworth",
"KINDRON" => "Kindron", //does not exist
"KNGTB" => "King's", //does not exist
"KRHNR" => "Krohnert",//does not exist
"LAMDR" => "Land Rover", //does not exist
"LFKTR" => "Lufkin",
"MACK" => "Mack",
"MCKTR" => "Mack",
"MACKX" => "Mack",
"MANAC" => "Manac",
"MTLTR" => "Matlock", //does not exist
"MXNIN" => "Maxon",//does not exist
"MAZDA" => "Mazda",//does not exist
"MRCBN" => "Mercedes-Benz",
"MERC" => "Mercury",//does not exist
"MCKBD" => "Mickey",//does not exist
"MILLR" => "Miller",
"MISCX" => "Miscellaneous",//does not exist
"MITSUB" => "Mitsubishi",
"FUSOX" => "Mitsubishi",
"MTSBS" => "Mitsubishi",
"MNDIN" => "Mond",//does not exist
"MONON" => "Monon",
"MORGN" => "Morgan",//does not exist
"MNCPW" => "Munchie",//does not exist
"M2" => "M2",//does not exist
"NVSTR" => "Navistar",//does not exist
"NSSDA" => "Nissan",//does not exist
"NSSMT" => "Nissan",
"OTHER" => "Other",
"OTTST" => "Ottawa",
"PALFGR" => "Palfinger", //does not exist
"PTRBL" => "Peterbilt",
"PTRSN" => "Peterson",//does not exist,
"PINES" => "Pines",
"POLAR" => "Polar",
"PONT" => "Pontiac",//does not exist
"PRVST" => "Prevost",//does not exist
"PRINC" => "Princeton",//does not exist
"RELIA" => "Reliable",//does not exist
"RENLT" => "Renault",//does not exist
"RSSIN" => "Roussy",//does not exist
"SATRN" => "Saturn",//does not exist
"STDMN" => "Steadman",//does not exist
"STRLG" => "Sterling",
"STWDR" => "Stewart",//does not exist
"STGHT" => "Stoughton",//does not exist
"STRCK" => "Strick",//does not exist
"SUPRR" => "Superior",//does not exist
"SPRRT" => "Superior Tech",//does not exist
"SUPRM" => "Supreme",//does not exist
"THRKN" => "Thermo King",//does not exist
"TIMPT" => "Timpte",
"TOYOT" => "Toyota",//does not exist
"TRLMT" => "Trailermaster",//does not exist
"TRLMB" => "Trailmobile",
"TRLCN" => "Trailmobile",
"TRNSI" => "Transicold",//does not exist
"TRNST" => "Transit",//does not exist
"TRIM" => "Trim",//does not exist
"UTILIT" => "Utilit",//does not exist
"UTLTY" => "Utility",
"VLVNA" => "Volvo",//does not exist
"VOLVO" => "Volvo",
"WABSH" => "Wabash",
"WALKR" => "Walker",
"WARNI" => "Warner",//does not exist
"WEBST" => "Webasto",//does not exist
"WSTNK" => "Westank",//does not exist
"WSTST" => "Western Star",
"WSTAR" => "Westinghouse Air",//does not exist
"WHITE" => "White"
);
Sample data from the db as i looped through the $row['make']
FORDX VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO VOLVO GRTDN GRTDN
Here's the loop code
$result = $pdo->prepare("SELECT * FROM csvImport limit 20");
$data = array();
if ($result->execute()) {
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
if($row['status']== "P-Sale Pending")
$status = "Pend";
else
$status = "Live";
$unit = $row['unit'];//stocknumber
$make = $makes[$row['make']];
}
}
I know this is not an answer but a debugging tip - but formatting in comments is insufficient.
Try this:
if (isset($makes[$row['make']]) {
$make = $makes[$row['make']];
} else {
echo "unrecognized make for unit {$unit}: ";
var_dump($row['make']);
die();
}
...I bet there's some sort of whitespace character in one of the rows' make column...
I'm trying to grab data from this array, but when I do var_dump($ArrayedLevels['Attack']); it returns NULL, I know for a fact that it was able to grab the data from the SQL database, I think it has something to do with my array. Any help would be greatly appreciated.
include("highscoresconfig.php");
$GrabXP = $database2->prepare("SELECT * FROM `skills` WHERE `playerName` = ?");
$GrabXP->execute(array($playerName));
$MainResult = $GrabXP->fetchAll();
$ArrayedLevels = $array = [
"Attack" => $MainResult['Attacklvl'],
"Defence" => $MainResult['Defencelvl'],
"Strength" => $MainResult['Strengthlvl'],
"Hitpoints" => $MainResult['Hitpointslvl'],
"Ranged" => $MainResult['Rangelvl'],
"Prayer" => $MainResult['Prayerlvl'],
"Magic" => $MainResult['Magiclvl'],
"Cooking" => $MainResult['Cookinglvl'],
"Woodcutting" => $MainResult['Woodcuttinglvl'],
"Fletching" => $MainResult['Fletchinglvl'],
"Fishing" => $MainResult['Fishinglvl'],
"Firemaking" => $MainResult['Firemakinglvl'],
"Crafting" => $MainResult['Craftinglvl'],
"Smithing" => $MainResult['Smithinglvl'],
"Mining" => $MainResult['Mininglvl'],
"Herblore" => $MainResult['Herblorelvl'],
"Agility" => $MainResult['Agilitylvl'],
"Thieving" => $MainResult['Thievinglvl'],
"Slayer" => $MainResult['Slayerlvl'],
"Farming" => $MainResult['Farminglvl'],
"Runecrafting" => $MainResult['Runecraftlvl'],
"Hunter" => $MainResult['Hunterlvl'],
"Construction" => $MainResult['Constructionlvl'],
"Summoning" => $MainResult['Summoninglvl'],
"Dungeoneering" => $MainResult['Dungeoneeringlvl'],
];
var_dump($ArrayedLevels["Attack"]);
Problem might be because the fetchAll() returns all the rows of your query.
if($GrabXP->execute(array($playerName))){
//Success
$MainResult = $GrabXP->fetchAll();
/*
This will give you all the rows. Use a foreach loop to iterate through all the rows.
If you want only the first row, add this-
*/
$MainResult = $MainResult[0];
//The rest of your code.
$ArrayedLevels = $array = Array(
"Attack" => $MainResult['Attacklvl'],
...
);
var_dump($ArrayedLevels["Attack"]);
}
else{
//Failure
}
The brackets don't work in PHP when creating a new array, use array() to create one.
Actually: as of PHP 5.4 they do work, see here: http://nl3.php.net/manual/en/language.types.array.php
Try var_dump($MainResult);
if all good, try create your array like this:
$ArrayedLevels = array(
"Attack" => $MainResult['Attacklvl'],
"Defence" => $MainResult['Defencelvl'],
"Strength" => $MainResult['Strengthlvl'],
"Hitpoints" => $MainResult['Hitpointslvl'],
"Ranged" => $MainResult['Rangelvl'],
"Prayer" => $MainResult['Prayerlvl'],
"Magic" => $MainResult['Magiclvl'],
"Cooking" => $MainResult['Cookinglvl'],
"Woodcutting" => $MainResult['Woodcuttinglvl'],
"Fletching" => $MainResult['Fletchinglvl'],
"Fishing" => $MainResult['Fishinglvl'],
"Firemaking" => $MainResult['Firemakinglvl'],
"Crafting" => $MainResult['Craftinglvl'],
"Smithing" => $MainResult['Smithinglvl'],
"Mining" => $MainResult['Mininglvl'],
"Herblore" => $MainResult['Herblorelvl'],
"Agility" => $MainResult['Agilitylvl'],
"Thieving" => $MainResult['Thievinglvl'],
"Slayer" => $MainResult['Slayerlvl'],
"Farming" => $MainResult['Farminglvl'],
"Runecrafting" => $MainResult['Runecraftlvl'],
"Hunter" => $MainResult['Hunterlvl'],
"Construction" => $MainResult['Constructionlvl'],
"Summoning" => $MainResult['Summoninglvl'],
"Dungeoneering" => $MainResult['Dungeoneeringlvl'],
);
first of all try to print the structure of the variable $MainResult:
var_dump($MainResult);
FetchAll return an array with all restuls like
resutl = [
[0] => ['first row'],
[1] => ['sec. row']
]
you will See The variable $MainResult looks like:
MailResult => [
[0] => ['Attacklvl' => 'foo'],
[1] => ['Defencelvl' => 'bar']
]
I have this array
$arr = array(
'one' => array(
'slidertitle' => 'lorem ipsum',
'sliderlocation' => 'http://localhost/images/1.jpg',
'sliderdescription' => 'this is a good lorem ipsum image',
'sliderposition' => 1
),
'two' => array(
'slidertitle' => 'second slider',
'sliderlocation' => 'http://localhost/images/2.jpg',
'sliderdescription' => 'this space was reserved for a link source code here',
'sliderposition' => 2
),
'six' => array(
'slidertitle' => 'sixth slider',
'sliderlocation' => 'http://localhost/images/6.jpg',
'sliderdescription' => 'this is the sixth slider,like,really!',
'sliderposition' => 6
)
);
which i need to look like this
$arr = array(
'two' => array(
'slidertitle' => 'second slider',
'sliderlocation' => 'http://localhost/images/2.jpg',
'sliderdescription' => 'this space was reserved for a link source code here',
'sliderposition' => 2
),
'six' => array(
'slidertitle' => 'sixth slider',
'sliderlocation' => 'http://localhost/images/6.jpg',
'sliderdescription' => 'this is the sixth slider,like,really!',
'sliderposition' => 6
),
'one' => array(
'slidertitle' => 'lorem ipsum',
'sliderlocation' => 'http://localhost/images/1.jpg',
'sliderdescription' => 'this is a good lorem ipsum image',
'sliderposition' => 1
)
);
I am attempting to do that by defining the expected array structure and introducing a dummy array.I then chunk the array and merge each chunk to the array format and i plan to finally unset the dummy and i am left with the array i want and in the order i want.
$arrayFormat = array(
'dummy' => array(
'slidertitle' => 'xxxx',
'sliderlocation' => 'xxxxxxx',
'sliderdescription' => 'xxxxxx',
'sliderposition' => 0
)
);
$arrayLength = count($arr);
$afterChunk = array_chunk($arr,$arrayLength);
$one = $afterChunk[0][0];
$two = $afterChunk[0][1];
$mergedArray = array_merge($arrayFormat,$one);
$secondMergedArray = array_merge($mergedArray,$two);
echo '<pre>';
print_r($secondMergedArray);
echo '</pre>';
The problem is array_chunk() does not include the key of the array so i am getting
Array (
[dummy] => Array
(
[slidertitle] => xxxx
[sliderlocation] => xxxxxxx
[sliderdescription] => xxxxxx
[sliderposition] => 0
)
[slidertitle] => second slider
[sliderlocation] => http://localhost/images/2.jpg
[sliderdescription] => this space was reserved for a link source code here
[sliderposition] => 2 )
when i print_r($secondMergedArray);.is there something that can be done to array_chunk() to include the array key or is there any other array function that can help me get individual array inclusive of the key?.
It's really hard to tell what you're wanting in terms of how to sort the elements. You've not been very clear in the question. There has to be something in the array that you know what order it needs to be.
In the absence of any clues as to what that is, I'm going to assume you want to specify the order of the array keys manually.
So, the current array is array('one'=>... , 'two'=>... , 'six'=>... ) and you want to sort those keys in an order you want to specify manually.
The solution is to use the uksort() function, along with a separate array specifying your sort order:
$arr = ... //input array as specified in the question
$sortOrder = array('two','one','six');
uksort($arr, function ($a, $b) use ($sortOrder) {
$sortMe = array_flip($sortOrder);
if ($sortMe[$a] == $sortMe[$b]) { return 0; }
return ($sortMe[$a] < $sortMe[$b]) ? -1 : 1;
});
print_r($arr);
Outputs your array in 'two','one','six' order. Change the $sortOrder array as required.
Hope that helps.
Note: the syntax I've provided above only works in PHP 5.3 and above. (if you're using an older version, you need to upgrade)
use uksort() for custom order for multidimensional array
http://php.net/manual/en/function.uksort.php