array_diff converst array to stdClass Object - php

$array
Array
(
[0] => one
[1] => two
[2] => three
)
$array2
Array
(
[0] => three
)
array_diff($array,$array2)
stdClass Object
(
[0] => one
[1] => two
)
i need final result to be array instead of stdClass Object
Array
(
[0] => one
[1] => two
)

Refer to doc, its already returning an array.
Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.
To cast stdClass to array, can do
$casted_result = (array) $result;

Related

Search multidimensional array for value

I wonder if there is better (faster) way to search for value in multidimensional array than looping through every item.
Lets say i have
$id_to_search = '16819976033';
And array which is pretty big
Array
(
[0] => Array
(
[id] => Array
(
[0] => 16771055710
[1] => 16776555710
[2] => 16819976033
)
[o] => 21566
[p] => 12597.66
)
[1] => Array
(
[id] => Array
(
[0] => 14089762
)
[o] => 12606
[p] => 1747.49
)
etc ...
)
I can find it if i loop through each item and than compare them but its very slow because array is big.
You can use by array_search function in PHP:
$key = array_search($id_to_search, array_column($YourArray, 'id'));

array_unique outputs null while sorting array

I have this array (decoded from JSON, output with print_r):
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[item] => te
[date] => 13.10
)
[1] => stdClass Object
(
[item] => te
[date] => 13.10
)
[2] => stdClass Object
(
[item] => tr
[date] => 13.10
)
)
)
But now I have to remove all the duplicates.
If I try $result = array_unique($array, SORT_REGULAR);
$result is null.
Can someone spot my mistake?
This is a stdClass object, not an array. When you decode by using the function json_decode, you need to pass the parameter "true" to have an array:
$array = json_decode($json, true);
Edit: As people noticed in the comments, the actual array exists in $array['data'], so the array_unique must be applied on $array['data'] instead of $array.

Update a multidimentional array from key of other

I have a result array like this:
$result = Array
(
[1] => Array
(
[0] => some value
[1] => some value
[2] => some value
)
[4] => Array
(
[0] => some value
[1] => some value
[6] => Array
(
[0] => some value
[8] => Array
(
[0] => some value
)
)
)
)
and I have a second array
$test = Array
(
[4] => Array
(
[6] => Array
(
[8] => Array
(
[0] = value to add
)
)
)
)
How can I update the $result array on base of keys from $test array without losing any key index.
The main point is $result can have any structure and is dynamically generated but the $test array will always have keys index(s) which will somewhat match the $result array.
The general PHP array combine merge and other functions don't provide desired results.
[Edit]
The point is my second array $test will decide the position of $result where the data will be added/merged.
Did you try using:
http://php.net/manual/en/function.array-merge-recursive.php
By add do you mean append or addition(sum)??
This should work for appending the data.

Create an array-of-arrays from an array's elements

I'm looking for a function to take a simple array and create an array of arrays, without iterating through the original array (i.e., foreach)
For example, give this array:
['a','b','c','d']
I want the result to be:
[['a'], ['b'], ['c'], ['d']]
Does such a function exist?
You can use array_chunk() Documentation:
http://php.net/manual/en/function.array-chunk.php
It will split an array into a multi-demential array of "chunks." For you, you want every chunk to be 1 variable in size.
$Alphabet = array('a','b','c','d');
$Chunked = array_chunk($Alphabet, 1); // Chunk the Alphabet array in 1 size chunks
print_r($Chunked);
This will produce:
Array
(
[0] => Array
(
[0] => a
)
[1] => Array
(
[0] => b
)
[2] => Array
(
[0] => c
)
[3] => Array
(
[0] => d
)
)

Comparing Associative array and standard array PHP

I have two arrays
Array1:
Array ( [0] => Array ( [0] => 3 [1] => 1 [2] => 4 ) [1] => Array ( [0] => 1 [1] => 6 ) )
Array2:
Array ( [0] => 1 [1] => 3 [2] => 2 )
I used array_diff for comparing and getting the difference values, but the same key is coming ie.,
array_diff(Array1,Array2)
returns Array([0] =>3 [2] => 4)
but is there any other way to get difference and having result like
Array([0] =>3 [1] => 4)..
Assuming you've got array_diff working on the multidimensional array somehow, but from the docs:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.
Use array_values around it.
array_values(array_diff($array1, $array2));

Categories