I have a following array,
$versions = array
(
'0.9.md5' => '/var/www/md5_test/0.9.md5',
'1.0.0.md5' => '/var/www/md5_test/1.0.0.md5',
'1.0.1.md5' => '/var/www/md5_test/1.0.1.md5',
'1.0.2.md5' => '/var/www/md5_test/1.0.2.md5',
'1.0.3.md5' => '/var/www/md5_test/1.0.3.md5',
'1.0.9.1.md5' => '/var/www/md5_test/1.0.9.1.md5',
'1.0.9.10.1.md5' => '/var/www/md5_test/1.0.9.10.1.md5',
'1.0.9.10.md5' => '/var/www/md5_test/1.0.9.10.md5',
'1.1.3.md5' => '/var/www/md5_test/1.1.3.md5',
'1.0.9.2.md5' => '/var/www/md5_test/1.0.9.2.md5',
'1.0.9.3.md5' => '/var/www/md5_test/1.0.9.3.md5',
'1.0.9.8.md5' => '/var/www/md5_test/1.0.9.8.md5',
'1.0.9.9.1.md5' => '/var/www/md5_test/1.0.9.9.1.md5',
'1.0.9.9.md5' => '/var/www/md5_test/1.0.9.9.md5',
'1.0.9.md5' => '/var/www/md5_test/1.0.9.md5',
'1.1.0.md5' => '/var/www/md5_test/1.1.0.md5',
'1.1.1.md5' => '/var/www/md5_test/1.1.1.md5',
'1.1.2.md5' => '/var/www/md5_test/1.1.2.md5',
);
In this array i want to sort this by keys. I have searched,
Ex: It should order like: 1.0.9.md5, 1.0.9.1.md5,.. , 1.0.9.10.md5, 1.0.9.10.1.md5
I have tried
ksort($versions);
But i could't get exactly what i want.
If these are version numbers, and you need to sort by the version so that 1.0.9.2.md5 comes before 1.0.9.10.1.md5 then you need a custom sort based on semantic versioning:
uksort($versions, 'version_compare');
Demo
Remove the ".md5" -> ksort() -> add the ".md5" again.
foreach($versions as $key => $value) {
$newKey = str_replace(".md5", "", $key);
$new[$newKey] = $value;
}
ksort($new);
foreach($new as $key => $value) {
$newKey = $key . ".md5";
$result[$newKey]= $value;
}
print_r($result);
Result:
Array
(
[0.9.md5] => /var/www/md5_test/0.9.md5
[1.0.0.md5] => /var/www/md5_test/1.0.0.md5
[1.0.1.md5] => /var/www/md5_test/1.0.1.md5
[1.0.2.md5] => /var/www/md5_test/1.0.2.md5
[1.0.3.md5] => /var/www/md5_test/1.0.3.md5
[1.0.9.md5] => /var/www/md5_test/1.0.9.md5
[1.0.9.1.md5] => /var/www/md5_test/1.0.9.1.md5
[1.0.9.10.md5] => /var/www/md5_test/1.0.9.10.md5
[1.0.9.10.1.md5] => /var/www/md5_test/1.0.9.10.1.md5
[1.0.9.2.md5] => /var/www/md5_test/1.0.9.2.md5
[1.0.9.3.md5] => /var/www/md5_test/1.0.9.3.md5
[1.0.9.8.md5] => /var/www/md5_test/1.0.9.8.md5
[1.0.9.9.md5] => /var/www/md5_test/1.0.9.9.md5
[1.0.9.9.1.md5] => /var/www/md5_test/1.0.9.9.1.md5
[1.1.0.md5] => /var/www/md5_test/1.1.0.md5
[1.1.1.md5] => /var/www/md5_test/1.1.1.md5
[1.1.2.md5] => /var/www/md5_test/1.1.2.md5
[1.1.3.md5] => /var/www/md5_test/1.1.3.md5
)
Related
What I'm trying to accomplish is to write a html string in a controller with array values being looped in it. So for example;
$content = "Your store, at location A, has these items added to them". add array loop here. "Do take note!";
My array would be as such
array (
0 =>
array (
'id' => '5db29b6d31c391731239bbdf',
'name' => 'Diamond bracelet (sample)',
'tags' =>
array (
0 => 'female',
1 => 'jewelry',
),
'category' => 'Accessories',
'sku' => '1029EHW',
'priceType' => 'Fixed',
'unitPrice' => 190,
'cost' => 90,
'trackStockLevel' => true,
'isParentProduct' => false,
),
1 =>
array (
'id' => '5db29b6d31c391731239bbdb',
'name' => 'Long-sleeved shirt(sample)(M)',
'tags' =>
array (
0 => 'tops',
1 => 'cotton',
),
'category' => 'Women\'s Apparel',
'sku' => 'ABC1234-M',
'priceType' => 'Fixed',
'unitPrice' => 47.170000000000002,
'cost' => 20,
'trackStockLevel' => true,
'isParentProduct' => false,
'parentProductId' => '5db29b6d31c391731239bbd4',
'variationValues' =>
array (
0 =>
array (
'variantGroupId' => '5db29b6d31c391731239bbd5',
'value' => 'M',
),
),
),
)
note that the array can have many instances of product_name and sku, or can have none.
How do i populate this in my string to be like;
$content = "Your store, at location A, has these items added to them, 1) asd, 2)def, 3)asf . Do take note!
Try this, I've used \sprintf for ease, check if the output serves your purpose.
<?php
function stringMethod(): string
{
$count = 0;
$arrayString = [];
$array = [['product_name' => 'abc', 'product_sku' => 'def'],['product_name' => 'abc', 'product_sku' => 'asd']];
foreach ($array as $value){
$count++;
$arrayString[] = sprintf('%s)%s', $count, $value['product_sku']);
}
$string = \implode(',', $arrayString);
return \sprintf("Your store, at location A, has these items added to them %s Do take note!", $string);
}
echo stringMethod();
Hope this will help you. try to do it in less number of lines
$content = "Your store, at location A, has these items added to them, ";
$productArray = array (0 => array ('id' => '5db29b6d31c391731239bbdf','name' => 'Diamond bracelet (sample)','tags' => array (0 => 'female',1 => 'jewelry',),'category' => 'Accessories','sku' => '1029EHW','priceType' => 'Fixed','unitPrice' => 190,'cost' => 90,'trackStockLevel' => true,'isParentProduct' => false,),1 => array ('id' => '5db29b6d31c391731239bbdb','name' => 'Long-sleeved shirt(sample)(M)','tags' => array (0 => 'tops',1 => 'cotton',),'category' => 'Women\'s Apparel','sku' => 'ABC1234-M','priceType' => 'Fixed','unitPrice' => 47.170000000000002,'cost' => 20,'trackStockLevel' => true,'isParentProduct' => false,'parentProductId' => '5db29b6d31c391731239bbd4','variationValues' => array (0 => array ('variantGroupId' => '5db29b6d31c391731239bbd5','value' => 'M'))));
foreach ($productArray as $key => $product) {
$content .= ($key+1).') '.$product['name'];
if (count($productArray)-1!=$key) {
$content .= ', ';
}
}
$content .= ". Do take note!";
$content = "Your store, at location A, has these items added to them,". $this->getItemList($collection) .". Do take note!"
# Somewhere else in the controller
protected function getItemList(Collection $collection): string
{
return $collection->pluck('name')
->merge($collection->pluck('sku'))
->map(function($item, $key) {
return ($key + 1) . ') ' . $item;
})
->implode(', ');
}
An easy solution would be
$content = "Your store, at location A, has these items added to them";
$array = array(0=>["product_name" => "abc", "product_sku" => "def"], 1=>['product_name' => 'kdkf', 'product_sku'=> 'ljbkj']);
for($i = 0; $i<count($array); $i++){
$content .= ($i+1).") ".$array[$i]['product_name].' '.$array[$i]['product_sku'].', ';
}
$content .= "Do take note.";
This way you're just constantly concatonating the string value in separate parts instead of trying to inject it in the middle of the parent string.
not tested, may be syntax errors
How can I make the values of array comma separated values?
this is my array:
array(
'product_name' =>
0 => 'pn1',
1 => 'pn2'
'supply_product_name' =>
0 => 'ps1'
'custom_product_code' =>
0 => string 'cpc1'
)
how to achieve to look like these:
array(
'product_name' => 'pn1, pn2' ,
'supply_product_name' => 'ps1' ,
'custom_product_code' => 'cpc1'
)
Try these code:
$arr = array(
'product_name' => ['pn1', 'pn2'],
'supply_product_name' => ['ps1'],
'custom_product_code' => ['cpc1']
);
foreach($arr as $key => $val)
{
$arr[$key] = implode($val, ',');
}
var_dump($arr);
This question already has answers here:
How to swap keys with values in array?
(6 answers)
Closed 6 years ago.
How would you modify the following code using only a foreach loop by replacing the keys as the values? For examples the salad, chicken, and pancakes keys would the values instead.
$meals = array(
'Lunch' => array(
'salad' => 'italian',
'salad2' => 'ranch',
'salad3' => 'sesame',
'salad4' => 'bluecheese'
),
'Dinner' => array(
'chicken' => 'grilled',
'chicken2' => 'baked',
'chicken3' => 'steamed',
'chicken4' => 'fried',
'chicken5' => 'broiled'
),
'Breakfast' => array(
'pancakes' => 'blueberry',
'pancakes2' => 'cherry',
'pancakes3' => 'strawberry',
'pancakes4' => 'lemon'
)
);
$newkey = array();
foreach($meals as $key => $value) {
unset($value);
// foreach ($)...
}
print_r($meals);
Edit
If you're unable to use array_flip(), then you'll need to do two loops: one for each meal, and one for each meal option.
Example:
foreach ($meals as $meal => $mealOptions)
{
$revisedMealOptions = array();
foreach ($mealOptions as $originalKey => $newKey)
{
$revisedMealOptions[$newKey] = $originalKey;
}
$meals[$meal] = $revisedMealOptions;
}
Original Answer
It's not entirely clear what you're after. If all you want is to turn:
'Lunch' => array(
'salad' => 'italian'
);
into
'Lunch' => array(
'italian' => 'salad'
);
use array_flip().
Example:
$meals['Lunch'] = array_flip($meals['Lunch']);
See http://php.net/manual/en/function.array-flip.php.
I'm sorry I am a total beginner. I need to use a nested $foreach loop to address this question. For class I'm not allowed to use the array_flip function. I have tried
$newkey = array();
foreach($meals as $key => $value) {
$newkey[] = $value;
foreach ($value as ){
}
}
So, it would read:
Basically I need it to read:
Array(
[Lunch] => array(
[italian] => salad,
[ranch] => salad2,
[sesame] => salad3,
[bluecheese] => salad4
)
)
The easiest way to do it would be like this:
$meals = array(
'Lunch' => array(
'salad' => 'italian',
'salad2' => 'ranch',
'salad3' => 'sesame',
'salad4' => 'bluecheese'
),
'Dinner' => array(
'chicken' => 'grilled',
'chicken2' => 'baked',
'chicken3' => 'steamed',
'chicken4' => 'fried',
'chicken5' => 'broiled'
),
'Breakfast' => array(
'pancakes' => 'blueberry',
'pancakes2' => 'cherry',
'pancakes3' => 'strawberry',
'pancakes4' => 'lemon'
)
);
$newArray = array();
foreach ($meals as $key => $value) {
$temp = array();
foreach ($value as $innerKey => $innerValue) {
$temp[$innerValue] = $innerKey;
}
$newArray[$key] = $temp;
}
unset($meals);
Basically, you create a new array and build it from the beginning with the use of the array $meals.
I want to count all the values (The name of the cities) in this array who have the character t before the last character in the string.
ARRAY
$cities_array = array(
"city1" => "Paris_t1",
"city2" => "Madrid_t1",
"city3" => "Amsterdam_t1",
"city4" => "London_i1",
"city5" => "Miami_i1",
"city6" => "Berlin_i1",
"city7" => "Brussels_i1",
"city8" => "Toronto_i1",
);
The results should be: 3 (Paris_t1 - Madrid_t1 - Amsterdam_t1)
I believe i have to combine:
array_count_values($cities_array)
and
substr($value, -2, 1) == "t"
I have tried, but I get only errors.
This will give you what you want.
$cities_array = array(
"city1" => "Paris_t1",
"city2" => "Madrid_t1",
"city3" => "Amsterdam_t1",
"city4" => "London_i1",
"city5" => "Miami_i1",
"city6" => "Berlin_i1",
"city7" => "Brussels_i1",
"city8" => "Toronto_i1",
);
$count = 0;
$city_text = '';
foreach($cities_array as $city){
if(substr($city, -2, 1) == "t"){
$count++;
$city_text .= $city . '-';
}
}
echo $count. "(".rtrim($city_text,'-').")";
Try below solution:
$cities_array = array(
"city1" => "Paris_t1",
"city2" => "Madrid_t1",
"city3" => "Amsterdam_t1",
"city4" => "London_i1",
"city5" => "Miami_i1",
"city6" => "Berlin_i1",
"city7" => "Brussels_i1",
"city8" => "Toronto_i1",
);
$filtered_array = array_filter($cities_array, function($val){
return (strpos($val, 't', (strlen($val)-2)) !== false);
});
print_r($filtered_array);
output: - (you can implode array by - to get desired result)
Array
(
[city1] => Paris_t1
[city2] => Madrid_t1
[city3] => Amsterdam_t1
)
In above solution in strpos third parameter strlen($val)-2) i.e. position will be searched from second last character of $val
What is the best way to search element in this array?
$emailsArray= array(
'http://gmail.com' => 'gmail.com',
'http://poczta.onet.pl' => array('onet.pl','vp.pl', 'op.pl', 'spoko.pl', 'poczta.onet.pl', 'onet.eu', 'onet.com.pl', 'opoczta.pl','autograf.pl','vip.pl','vip.onet.pl'),
'http://poczta.wp.pl' => 'wp.pl',
'http://poczta.o2.pl' => 'o2.pl',
'http://mail.tlen.pl' => 'tlen.pl',
'http://poczta.interia.pl' => array('interia.pl','poczta.fm','interia.eu'),
'http://poczta.gazeta.pl' => 'gazeta.pl',
'http://pl.mail.yahoo.com' => array('yahoo.pl','yahoo.com'),
);
Depending on what you are searching for you could do something along these lines:
$emailDomain = 'o2.pl'; //grab this from an email you want to check
$emailsArray= array(
'http://gmail.com' => 'gmail.com',
'http://poczta.onet.pl' => array('onet.pl','vp.pl', 'op.pl', 'spoko.pl', 'poczta.onet.pl', 'onet.eu', 'onet.com.pl', 'opoczta.pl','autograf.pl','vip.pl','vip.onet.pl'),
'http://poczta.wp.pl' => 'wp.pl',
'http://poczta.o2.pl' => 'o2.pl',
'http://mail.tlen.pl' => 'tlen.pl',
'http://poczta.interia.pl' => array('interia.pl','poczta.fm','interia.eu'),
'http://poczta.gazeta.pl' => 'gazeta.pl',
'http://pl.mail.yahoo.com' => array('yahoo.pl','yahoo.com'),
);
foreach ($emailsArray as $host => $domains) {
if (is_string($domains)) {
$domains = array($domains);
}
if (in_array($emailDomain, $domains)) {
echo "The email is hosted at $host\n";
}
}
Do a foreach as $key => $value, within that an if is_array ($value) foreach $value as $key => $val
Then just check for matches within those loops