Array key value to string in PHP [duplicate] - php

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'));

Related

how to query array within an array [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
When I execute a print_r I get this result...
Array (
[0] => Array (
[A_program_id] => a0F36000008PIYF
[XC_program_logo] => louisville.jpg
)
[1] => Array (
[A_program_id] => a0F36000008qjSp
[XC_program_logo] => alabama.jpg
)
[2] => Array (
[A_program_id] => a0F36000008pRxL
[XC_program_logo] => trinity.jpg
)
)
How do I make a while loop or whatever to properly fetch needed to query something like this:
//zero based
echo"".$rows[0][0]."</td><td>"".$rows[1][0].""</td><td>"".$rows[2][0]."";
echo"".$rows[0][1]."</td><td>"".$rows[1][1].""</td><td>"".$rows[2][1]."";
to show
louisville.jpg alabama.jpg trinity.jpg
a0F36000008PIYF a0F36000008qjSp a0F36000008pRxL
please help thx
One possibility is to use array_column to extract each rows data from the source, and then implode to add the </td><td> between each value:
echo implode('</td><td>', array_column($rows, 'XC_program_logo'));
echo implode('</td><td>', array_column($rows, 'A_program_id'));
Demo on 3v4l.org
This will give the same output as the two echo statements in your question.

Loading and summing data from array into a variable [duplicate]

This question already has answers here:
How can I add all of my array values together in PHP?
(4 answers)
Closed 3 years ago.
I have an array and I want to sum up the values of the array such as [0][1][2] and assign the result of the sum to a variable
My array
Array ( [0] => 1 [1] => 2 [2] => 0 )
Can anyone provide me a solution that would be really helpful?
Use php array_sum() function.
$val = array_sum($yourArray);
It will sum up all of your array elements into a variable.

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

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

sort in explode variable [duplicate]

This question already has answers here:
PHP Sorting Array ASC
(3 answers)
Closed 6 years ago.
Hello everyone I have a small problem with the php sort I have basically a variable example
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
rsort($prova);
echo $prova[0];
but that's out 4,v Instead I would like so 1,x
Use sort() simply.
<?php
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova);
echo $prova[0]; // Prints 1,x
?>
See it working live
have a look at http://php.net/manual/en/function.sort.php, here you can use second parameter i.e. sort_flags
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova, SORT_STRING); //SORT_STRING - compare items as strings
print_r($prova);
sort($prova, SORT_NUMERIC); //SORT_NUMERIC - compare items numerically
print_r($prova);
output
Array
(
[0] => 1,x
[1] => 2,f
[2] => 22,a
[3] => 4,v
)
Array
(
[0] => 1,x
[1] => 2,f
[2] => 4,v
[3] => 22,a
)

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