how to query array within an array [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.
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.

Related

Print php array values [duplicate]

This question already has answers here:
Query with GROUP_CONCAT not working with PHP
(1 answer)
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 3 years ago.
How do i print array values from this query ?
I need something like 6475,7377,6367. (comma separated).
This is what i get when i do a print_r($myarray):
Array
(
[0] => Array
(
[gift_product] => 6475
)
[1] => Array
(
[gift_product] => 7377
)
[2] => Array
(
[gift_product] => 6367
)
)
Thanks alot!
You could have handled this on the MySQL side using GROUP_CONCAT, something like this:
SELECT GROUP_CONCAT(gift_product) AS products
FROM yourTable
GROUP BY id;
Your current output indicates that you are getting back gift_product values over several records, when in fact you wanted to retrieve a CSV string of products.
You can use implode function after you map your array using array_map:
echo implode(', ', array_map(function ($entry) {
return $entry['gift_product'];
}, $myarray))
Use array_column to fetch column wise data with implode,
echo implode(",", array_column($arr, "gift_product"));
Demo
Output
6475,7377,6367

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;

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
)

Array_unique is not working [duplicate]

This question already has answers here:
How do I use array_unique on an array of arrays?
(7 answers)
Closed 7 years ago.
I am storing some value in multidimensional array, while storing that iam trying to neglate duplicate array in my values for that i used array_unique function in php. Its working in local, but not working in live can any one help me. I have attached my code below
$ex = $_POST;
$_SESSION["cartdetail"][] = $ex;
$_SESSION["cartdetail"] = array_unique($_SESSION["cartdetail"]);
echo "<pre>";
print_r($_SESSION["cartdetail"]);
echo "</pre>";
outPut
Array
(
[0] => Array
(
[cid] => 7
[cname] => studies
[ctype] => Class Room
[cdate] => 12-1
[ctime] => 09:30 am-06:30 pm
[cdays] => 5
[cprice] => 1
[viewDetails] => start
)
)
When i am trying to store different value in array, its not storing. Its storing only first value in array.Thank for your help
You need to serialize the internal arrays and then unserialize back:
$_SESSION["cartdetail"] = array_map("unserialize", array_unique(array_map("serialize", $_SESSION["cartdetail"])));

Categories