array – PHP Blog https://blog.dev-php.site Snippets and guides Tue, 02 Jul 2019 06:43:02 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.4 Convert a PHP object to an associative array https://blog.dev-php.site/convert-a-php-object-to-an-associative-array/ Fri, 12 Apr 2019 01:48:16 +0000 http://blog.dev-php.site/?p=110 getProperties() as $property) { $property->setAccessible(true); … Continue reading Convert a PHP object to an associative array]]> // start class MyClass { public $var1 = 1; public $var2 = 2; public static $var3 = 3; protected $var4 = 4; private $var5 = 5; public function toArray() { return (array) get_object_vars($this); } } function toArray($object) { //$reflectionClass = new ReflectionClass(get_class($object)); $reflectionClass = new ReflectionObject($object); $array = []; foreach ($reflectionClass->getProperties() as $property) { $property->setAccessible(true); $array[$property->getName()] = $property->getValue($object); $property->setAccessible(false); } return $array; } $object = new MyClass(); $array = toArray($object); $array2 = $object->toArray(); print_r($array2); // Inaccessible NO static print_r($array); // Inaccessible and static $obArray = []; foreach($object as $ob){ $obArray[] = $ob; } print_r($obArray); // public only Array ( [var1] => 1 [var2] => 2 [var4] => 4 [var5] => 5 ) Array ( [var1] => 1 [var2] => 2 [var3] => 3 [var4] => 4 [var5] => 5 ) Array ( [0] => 1 [1] => 2 ) ]]> Merging PHP Objects https://blog.dev-php.site/merging-php-objects/ Fri, 12 Apr 2019 01:20:00 +0000 http://blog.dev-php.site/?p=103 Continue reading Merging PHP Objects]]> Given two objects of same class, merge both objects into a single object.


class MyClass {} 
  
$objectA = new MyClass(); 
$objectA->a = 1; 
$objectA->b = 2; 
$objectA->d = 3; 
  
$objectB = new MyClass(); 
$objectB->d = 4; 
$objectB->e = 5; 
$objectB->f = 6; 
 
// Using array_merge
$obj_merged = (object) array_merge((array) $objectA, (array) $objectB); 
print_r($obj_merged);
stdClass Object
(
    [a] => 1
    [b] => 2
    [d] => 4
    [e] => 5
    [f] => 6
)

// Using array union operator
$obj_union = (object)((array) $objectA +(array) $objectB);        
print_r($obj_union);
stdClass Object
(
    [a] => 1
    [b] => 2
    [d] => 3
    [e] => 5
    [f] => 6
)
]]>
PHP array_merge and array union operator https://blog.dev-php.site/php-array_merge-and-array-union-operator/ Sat, 06 Apr 2019 06:34:42 +0000 http://blog.dev-php.site/?p=95 Continue reading PHP array_merge and array union operator]]> In PHP you can combine arrays using the union operator (+) or the array_merge function.

$ar1 = [
   0  => '1-0',
  'a' => '1-a',
  'b' => '1-b'
];


$ar2 = [
   0  => '2-0',
   1  => '2-1',
  'b' => '2-b',
  'c' => '2-c'
];

print_r($ar1+$ar2);

print_r(array_merge($ar1,$ar2));

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

Array
(
    [0] => 1-0
    [a] => 1-a
    [b] => 1-b
    [1] => 2-1
    [c] => 2-c
)

array_merge() If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Array
(
    [0] => 1-0
    [a] => 1-a
    [b] => 2-b
    [1] => 2-0
    [2] => 2-1
    [c] => 2-c
)
]]>
PHP iterate through an array in reverse https://blog.dev-php.site/php-iterate-through-an-array-in-reverse/ Sat, 06 Apr 2019 06:06:58 +0000 http://blog.dev-php.site/?p=90 To reverse the items in an array you have two methods of achieving this
array_reverse()

$array = [1,2,3,4,5,6,7,8,9];

$rev = array_reverse($array);

foreach($rev as $val) {
    print $val;
}

end() & prev() rewind

   $array = [1,2,3,4,5,6,7,8,9];
    print end($array);

    while($val = prev($array)) {
        print $val;
    }

will both print:
987654321

]]>