Posts Tagged with php array

Displaying 1-10 of 11 results.
Resolved: PHP datetime to russian user friendly format converting. Дата и время на русский на дружественный вид
posted by admin on October 12, 2016
I have datetime from databse MySQL, in format
2016-10-10 12:34:33

By PHP datetime formats I can have string like this
07 October 2016, 04:11, Friday

But I want the3 same output, but in russian
07 Октября 2016, 04:11, ПятницаRead more
Resolved: PHP display an Array under alphabetical letters without key sensitive
posted by admin on April 27, 2016
I have an associative array of tags like

array(11) {
[0]=>
array(4) {
["tag_id"]=>
string(2) "81"
["slug"]=>
string(4) "mag1"
["name"]=>
string(4) "mag1"
["TagsCount"]=>
string(1) "2"
}
[1]=>
array(4) {
["tag_id"]=>
string(2) "85"
["slug"]=>
string(4) "mag2"
["name"]=>
string(4) "mag2"
["TagsCount"]=>
string(1) "1"
}
[2]=>
array(4) {
["tag_id"]=>
string(2) "83"
["slug"]=>
string(4) "mag3"
["name"]=>
string(4) "mag3"
["TagsCount"]=>
string(1) "2"
}
[3]=>
array(4) {
["tag_id"]=>
string(2) "84"
["slug"]=>
string(4) "mag4"
["name"]=>
string(4) "mag4"
["TagsCount"]=>
string(1) "2"
}
. . .
[9]=>
array(4) {
["tag_id"]=>
string(2) "79"
["slug"]=>
string(4) "tag5"
["name"]=>
string(4) "tag5"
["TagsCount"]=>
string(1) "1"
}
[10]=>
array(4) {
["tag_id"]=>
string(2) "80"
["slug"]=>
string(4) "tag6"
["name"]=>
string(4) "tag6"
["TagsCount"]=>
string(1) "1"
}
}

In output I want to see result like

<h3>A</h3>
<ol>
<li>
<em>tag count</em>
<a href="link to tag">Animal</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Aqua</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Arthur</a>
</li>
</ol>
<!-- if B not EXIST not show B -->
<h3>C</h3>
<ol>
<li>
<em>tag count</em>
<a href="link to tag">Camel</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Crazy</a>
</li>
</ol>
<!-- etc -->
Read more
Wiki: PHP 5 Sorting Arrayswiki
posted by admin on April 16, 2016
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.

PHP - Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:
  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key


Sort Array in Ascending Order - sort()
The following example sorts the elements of the $cars array in ascending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>


The following example sorts the elements of the $numbers array in ascending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>


Sort Array in Descending Order - rsort()
The following example sorts the elements of the $cars array in descending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>


The following example sorts the elements of the $numbers array in descending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>


Sort Array (Ascending Order), According to Value - asort()
The following example sorts an associative array in ascending order, according to the value:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>


Sort Array (Ascending Order), According to Key - ksort()
The following example sorts an associative array in ascending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>


Sort Array (Descending Order), According to Value - arsort()
The following example sorts an associative array in descending order, according to
arsort($age);
?>
[/php]

Sort Array (Descending Order), According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
Read more
Wiki: PHP Convert Hex Color to RGB and RGB to Hexwiki
posted by admin on April 13, 2016
Converting certain values back and forth is often necessary when developing functions to help your website work the way you want it to. I needed a function that would convert a hex color to rgb and for some reason it took me a while to figure it out.

function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);

if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
//return implode(",", $rgb); // returns the rgb values separated by commas
return $rgb; // returns an array with the rgb values
}


This function works with both shorthand hex codes such as #f00 and longhand hex codes such as #ff0000. It also accepts the number sign (#) just in case. You can see there are two return lines at the end of the function. The first, which is commented out, will return the rgb values separated by a comma. The second, which is the default, will return an array with the rgb values.

So now with this function in place we can use it like so:

$rgb = hex2rgb("#cc0");
print_r($rgb);


The above function would output:

Array ( [0] => 204 [1] => 204 [2] => 0 )


Since we are going one way with this, might as well go the other. Here is a function to convert rgb to a hex color:

function rgb2hex($rgb) {
$hex = "#";
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

return $hex; // returns the hex value including the number sign (#)
}


This function works like so:

$rgb = array( 255, 255, 255 );
$hex = rgb2hex($rgb);
echo $hex;


The above function would output:

#ffffff
Read more
Resolved: Convert an associative array to a simple array of its values in php
posted by admin on April 7, 2016
I would like to convert the array:

Array (
[category] => category
[post_tag] => post_tag
[nav_menu] => nav_menu
[link_category] => link_category
[post_format] => post_format
)

to

array(category, post_tag, nav_menu, link_category, post_format)
Read more
Resolved: PHP remove same values from two arrays
posted by admin on April 5, 2016
I have two arrays
1.

$array1 = array("a" => "green", "red", "blue", "red");


2.

$array2 = array("b" => "green", "yellow", "red");


I want to compare values, and get new array with unique elements.
Result

Array
(
[1] => blue
)
Read more
Resolved: PHP reverse array
posted by admin on March 31, 2016
How to reverse my arrayRead more
Resolved: PHP trim my strings in array
posted by admin on March 27, 2016
I have array, and I want to trim my array elements
My array

$arr = array(" one", " two", " three ", ..., etc);
Read more
Resolved: PHP remove empty strings from array
posted by admin on March 27, 2016
I have array

$arr = array("one", "two", "", "four");


I want to remove empty string from array with indexRead more
Resolved: PHP Compose array from string, by comma and space
posted by admin on March 27, 2016
I have hashtags, and I want to compose array from my hashtags string. My hashtags format is

one, two, three, four, five, six, 0909090, ... etc
Read more