I have an array, and would like to get all the values like this [*][place],
without duplicate results.
The output should be looking like this :
Sønderjylland
Nordjylland
Sjælland
Array
(
[0] => Array
(
[place] => Sønderjylland
[active] => Lagerarbejde
[num] => 123
)
[1] => Array
(
[place] => Nordjylland
[active] => Tømrer
[num] => 124
)
[2] => Array
(
[place] => Sønderjylland
[active] => Klejnsmed
[num] => 125
)
[3] => Array
(
[place] => Sjælland
[active] => Elektriker
[num] => 126
)
)
You can use array_column, array_unique, array_filter, implode together.
echo implode(' ', array_filter(array_unique(array_column($yourArray, 'place'))));
array_column is supported for (PHP 5 >= 5.5.0, PHP 7)
If you are using older versions then a loop would help.
Related
I'm trying to get the value of a specific key in an array or string format, but I can't seem to figure out how to make it work.
So, this is an example of an array I'm working with, for example, I'm trying to get all the product_id's in a single array or string format.
Like output productIdArray[] = ['34441', '34442' , '34444'];
Tried achieving this using for each but, Is there a better way to do that?
Any help would be appreciated, Thanks!
(
[0] => ( Array
(
[id] => 1333708
[abc_id] => 429084
[test_id] => 58291
[order_id] => 2222
[product_id] => 34441
)
)
[1] => ( Array
(
[id] => 1333708
[abc_id] => 429084
[test_id] => 58291
[order_id] => 2222
[product_id] => 34442
)
)
[2] => ( Array
(
[id] => 1333708
[abc_id] => 429084
[test_id] => 58291
[order_id] => 2222
[product_id] => 34444
)
)
)
Simply use array_column
$res = array_column($arr,'product_id');
I have an array of arrays that contains a date string.
I'd like to sort these arrays by this date.
What seems to be the monkeywrench in this is that some of the arrays share the same value for the date field as well as similar values for tid and/or thing and/or other_thing.
Array (
[0] => Array (
[tid] => 44
[date] => 1442905200
[thing] => 2J5265B
[other_thing] => Scoop
)
[1] => Array (
[tid] => 47
[date] => 1442905200
[thing] => 2J5265B
[other_thing] => Scoop
)
[2] => Array (
[tid] => 48
[date] => 1430031600
[thing] => 2E5116A
[other_thing] => shower
)
[3] => Array (
[tid] => 46
[date] => 1430031600
[thing] => 2E5116A
[other_thing] => shower
)
[4] => Array (
[tid] => 80
[date] => 1464246000
[thing] => 7J6147A
[other_thing] => shower
)
[5] => Array (
[tid] => 47
[date] => 1442905200
[thing] => 2J5265B
[other_thing] => TTT
)
[6] => Array (
[tid] => 44
[date] => 1442905200
[thing] => 2J5265B
[other_thing] => TTT
)
[7] => Array (
[tid] => 46
[date] => 1504594800
[thing] => 2J7248A
[other_thing] => shower
)
[8] => Array (
[tid] => 45
[date] => 1513238400
[thing] => 2J7348C
[other_thing] => TTT
)
)
That's what I'd like to do.
I'd like to sort this array.
You should consider using usort() (Docs). This function allows you to specify a comparator to have a user-defined sorting algorithm.
The resulting code might look like this:
function cmp($a, $b)
{
return $b['date'] - $a['date'];
}
usort($your_array, "cmp");
One fast forward solution would be to use the date as a key of the array while the array is being build and then you can simply sort keys using PHP ksort().
To avoid keys duplicity check if the key is set to handle such situations.
// building the data array from database or so
$array = array(); // the array to be sorted
$duplicity = array(); // track the duplicity date records
foreach ($data as $key => $value) {
$counter = 0;
#$duplicity[$value['date']]++; // suppressing notices
$array[$value['date'].'_'.$duplicity[$value['date']]] = $value;
}
ksort($array); // sort array by keys
print_r($array); // just check the sorted array
Demo: https://eval.in/978160
More sorting functions Sorting arrays
I have an array like this, i need to get the unique associative index
Array
(
[0] => Array
(
[id] => 200
[name] => james
[place] => ca
)
[1] => Array
(
[id] => 201
[name] => jana
[place] => uk
)
[2] => Array
(
[id] => 203
[name] => jana
[place] => ca
)
)
That means i need to get the unique 'place' from that array like
ca,uk
Make use of array_column() and array_unique()
array_unique(array_column($array, 'place'))
array_column — Return the values from a single column in the input
array (PHP 5 >= 5.5.0, PHP 7)
array_unique — Removes duplicate values from an array (PHP 4 >= 4.0.1, PHP 5, PHP 7)
I have two array as following
Array
(
[0] => 641
[1] => 622
[2] => 100
[3] => 6431
)
Array
(
[0] => 64.1
[1] => 62.2
[2] => 10
[3] => 643.1
)
How can I make it as following
Array
(
[0] => 641
[1] => 64.1
[2] => 622
[3] => 62.2
[4] => 100
[5] => 10
[6] => 6431
[7] => 643.1
)
It's as simple as
$result=array_merge($array1,$array2);
Note: Your values wont be in the order you presented though. If that is important then you need to loop through your arrays to build a new array accordingly.
Ummm ok here is that version as well
if(count($array1)==count($array2))
{
for($i=0;$i<count($array1);$i++)
{
$result[]=$array1[$i];
$result[]=$array2[$i];
}
}
print_r($result);
Fiddle
Manual
you can use array_merge() function merges one or more arrays into one array.
example:
array_merge(array1,array2,array3...)
print_r($array) results
I retrieved some values from database and got the following result while using print_r:
Array
(
[122] => Array
(
[p_name] => Tony Hutson
[p_image] => profile_image/profile_61323166474.jpg
[pid] => 42
[sid] => 122
[stitle] => sfcxdggf
[audio] => audio_file/audio_61324302202.mp3
[s_description] => ?mnlmkl bvnbmnmmn, bnbmn
[s_image] => sermon_image/image_41324302202.jpg
)
[count] => Array
(
[count] => 2
)
)
How can I retrieve the value of count?
echo $array['count']['count'];
Like this,
echo $array['count']['count'];