This question already has answers here:
Sorting an Array of Objects in PHP In a Specific Order
(4 answers)
Closed 8 years ago.
Supposing you have an object:
$arr = array(
array('name' => 'toto', 'firstname' => 'abc'),
array('name' => 'toto2', 'firstname' => 'aaa')
);
I want to order it by name AND firstname. You cannot use keyword "use" for old version of php like 5.2. If a general function is not possible, then build a function for this specific case.
Thanks.
Use usort with a callback that compares the elements of the array according to the rules you specify in the callback.
Related
This question already has an answer here:
Is there a way in phpunit that I could possibly assert two values for a key in an array?
(1 answer)
Closed 5 years ago.
$array = [a => '1',
b => '2']
For example, I want to check if a was either 1 or 3. I thought using this would work.
$this->assertThat(
$this->assertContains('1',$array),
$this->logicalOr(
$this->assertContains('3',$array)
));
Pass your assertions as arguments into logicalOr
$this->assertThat($array, $this->logicalOr(
$this->assertContains('3',$array),
$this->assertContains('1',$array)
));
This question already has answers here:
How to load return array from a PHP file?
(3 answers)
Closed 8 years ago.
I have a large "config" array that I want to store in a different file (taking this practice from Laravel's config files).
After storing it in app/config/myconfig.php
<?php
return array(
'foo' => 'bar',
);
How would I call this in my code if I wanted to assign it? E.g. $myarray = app/config/myconfig.php
Include the file:
$myarray = include 'app/config/myconfig.php';
This question already has answers here:
PHP problem with "__php_incomplete_class"
(5 answers)
Closed 8 years ago.
I am developing a web application in PHP and MySQL.
I get some data from the database and set it into a PHP Object. When I do a var_dump() of the variable that contains the information, I see this:
object(__PHP_Incomplete_Class)[2]
public '__PHP_Incomplete_Class_Name' => string 'Empresa' (length=7)
public 'IDEmpresa' => string '13' (length=2)
public 'Nombre' => string 'Prueba' (length=6)
... other attributes
Why the object is "__PHP_Incomplete_Class" and what are the implications of this?
Thanks!
It means that you've unserialized (presumably from the session) an object whose class is not defined in current runtime.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
is there any way to get common values between two arrays in php?
I am trying to compare 2 arrays and keep the elements based on the second array.
I have
array 1
array('3' => 'test1', '4' => 'test2', '1' =>'test3')
array 2
array('2' =>'test2', '3' =>'test3')
I want to compare array 1 and array 2 and keep the test 2 and test 3 in array 1 in my case.
So the end result will be
array('4' => 'test2', '1' =>'test3')
I have tried array_diff but it doesn't come out the results I wanted. I also google for a while but coudln't find anything useful either.
Are there anyways to get what I need? Thanks a lot!
You were close, array_intersect() is the function you need.
I suspect the function you really want is array_diff_key():
$diff = array_diff_key($array1, $array2);
(demo on codepad.org)
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
I'm learning about arrays and am not certain what => means or does.
Assignment of a value to a named key.
http://www.php.net/manual/en/language.operators.assignment.php
$example = array('color' => 'blue');
echo $example['color']; //prints blue
I believe it is just syntax for writing array literals that behave like maps / dictionaries.
$mydict = array('hello' => 'world');
The above is an array with one element. But instead if indexing that element with an integer index, you use the key 'hello':
echo $mydict['hello']