Get array of user IDs from multi-dimensional array [duplicate] - php

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?

Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example

Related

Array key value to string in PHP [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I want to convert an array of this style:
Array ( [0] => Array ( [post_id] => 20752 ) [1] => Array ( [post_id] => 20753 ) )
into the string:
20752, 20753
How can I do this?
You can use PHP array_column function with combination of implode:
implode(', ', array_column($data, 'post_id'));

get value from array with std object (php) [duplicate]

This question already has answers here:
stdClass object array inside of another array
(3 answers)
Closed 5 years ago.
my code in PHP :
$stuff = array($this->complaint_model->get_reply($id)->result());
print_r($stuff);
result :
Array (
[0] => Array (
[0] => stdClass Object (
[id_customer] => 21
[id_complaint] => 2
[id] => 4
[nama_customer] => Muhammad Bima Zehansyah
[from] => Admin
[balasan] => coba update
)
)
)
my question is , how to get value [nama_customer] ? thx before guys
Try this
$stuff = array($this->complaint_model->get_reply($id)->result());
echo $stuffVal = $stuff[0][0]->nama_customer;
Get the value like this
$stuff[0][0]->nama_customer
Here you have multidimensional array object that's why you need to first Travers two array like $stuff[0][0] and then the object like $stuff[0][0]->nama_customer
Actually you do not need to put the result into additional array and it would be wise to check if the result is not empty.
So you just take a first item from your result (which is an object) and call the parameter nama_customer
$stuff = $this->complaint_model->get_reply($id)->result();
if (!empty($stuff[0]))
echo $stuff[0]->nama_customer;

PHP Array Merge and remove duplicate [duplicate]

This question already has answers here:
Merge two arrays containing objects and remove duplicate values
(7 answers)
Closed 6 months ago.
the below are the array am getting out i want to mearge array and remove duplicats of it and pass too for loop not able too do i just want is remove duplicate and pass too for loop as a unique value
i tried with
print_r(array_merge($uniq_arr));
for($i = 0; $i < count($uniq_arr); $i++) {
$tag = $uniq_arr[$i];
}
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => water intake
[1] => hygiene
[2] => diet
)
Out put in tag shold be like each unique value
You could just use array_merge() indiscriminately then use array_unique() to remove any duplicate entries.
Here is how you could implement the array_merge, array_unique solution. I am making an assumption that you have 4 arrays like so:
$tag1Array = array('diet','exercise');
$tag2Array = array('diet','exercise');
$tag3Array = array('diet','exercise');
$tag4Array = array('water intake','diet','exercise');
$commontags = array_unique(array_merge($tag1Array,$tag2Array,$tag3Array,$tag4Array));
This should result in a single array of the unique values present in any of the 4 tag arrays.

php sort array keys in multidimensional array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
below is my array
$myarray = Array(
[1] => Array (['mytime']=>1),
[7] => Array(['mytime']=>2),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4)
);
I want to sort output of this array based on keys...
$myarray = Array(
[1] => Array (['mytime']=>1),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4),
[7] => Array(['mytime']=>2)
);
I have already tried ksort($myarray) it displays 1
anyways to fix this??
ksort() does this:
ksort($myarray);
Note: sorting functions do not return a new sorted array; they simply sort the array passed, and return true or false. Thus, ksort($myarray) will return 1 when successful, and $myarray will be sorted.
It's very clear if you read the docs: http://php.net/manual/en/function.ksort.php

count element in array? [duplicate]

This question already has answers here:
Working with arrays in php
(4 answers)
Closed 3 years ago.
I have an Array like this (when I print_r). How can I sum all elements in this Array?
In this example, the sum is 0+1=1
Array ( [0] => Array ( [0] => 0 )
[1] => Array ( [0] => 1 ) )
Take a look at the 'array_reduce' function: array_reduce: http://nl.php.net/manual/en/function.array-reduce.php
Taken from the documentation:
function rsum($v, $w)
{
$v += $w;
return $v;
}
$sum = array_reduce($a, "rsum");

Categories