Viewed   90 times

When interpolating PHP's string-indexed array elements (5.3.3, Win32) the following behavior may be expected or not:

$ha = array('key1' => 'Hello to me');

print $ha['key1'];   # correct (usual way)
print $ha[key1];     # Warning, works (use of undefined constant)

print "He said {$ha['key1']}"; # correct (usual way)
print "He said {$ha[key1]}";   # Warning, works (use of undefined constant)

print "He said $ha['key1']";   # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[ key1 ]";   # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[key1]";     # !! correct (How Comes?)

Inerestingly, the last line seems to be correct PHP code. Any explanations? Can this feature be trusted?


Edit: The point of the posting now set in bold face in order to reduce misunderstandings.

 Answers

1

Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.

If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.

Thursday, October 13, 2022
2

i asume your json comes via ajax.... (otherwise the code works with json_decode) so be sure the js json stringifys your object and

you'll need to stripslashes before json_decode ;-) in php

Sunday, November 13, 2022
 
2

The uasort() function allows you to specify a callback function, which will be responsible of doing the comparison between two elements -- so, should do just well, if you implement the proper callback function.

Here, you'd have to implement a callback function that will receive two arrays -- and compmare the age item :

function callback($a, $b) {
  if ($a['age'] > $b['age']) {
    return 1;
  } else if ($a['age'] < $b['age']) {
    return -1;
  }
  return 0;
}


Using that function in the following portion of code :

$arr = array(
    'ted' => array( 'age' => 27 ),
    'bob' => array( 'age' => 18 ),
    'jay' => array( 'age' => 24 )
);

uasort($arr, 'callback');
var_dump($arr);

You would get you this resulting array :

array
  'bob' => 
    array
      'age' => int 18
  'jay' => 
    array
      'age' => int 24
  'ted' => 
    array
      'age' => int 27
Saturday, November 19, 2022
 
3

Try:

Use array_filter() to get all duplicate values from array

Use array_diff() to get all unique values from array

$array = array(1=>0, 2=>1, 3=>1, 4=>2);
$counts = array_count_values($array);
$duplicates = array_filter($array, function ($value) use ($counts) {
    return $counts[$value] > 1;
});
print '<pre>';print_r($duplicates);

$result=array_diff($array,$duplicates);
print '<pre>';print_r($result);

Output:

Array
(
    [2] => 1
    [3] => 1
)

Array
(
    [1] => 0
    [4] => 2
)
Monday, October 10, 2022
1

Because the arrays are multidimensional you have to extract the ids like this:

$ids1 = array();
foreach($array1 as $elem1)
    $ids1[] = $elem1['id'];

$ids2 = array();
foreach($array2 as $elem2)
    $ids2[] = $elem2['id'];

$one_not_two = array_diff($ids1,$ids2);

For your specific question, check out array_diff() with multidimensional arrays

Tuesday, December 20, 2022
 
yariv
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :