This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];
Related
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.
This question already has answers here:
Better way to unset multiple array elements
(6 answers)
Closed 4 years ago.
I have an array of this form :
Array
(
[1] => A
[2] => B
[3] => C
)
I want to delete values where key = 1 and 2...
I have tried this
unset($array['1']);
unset($array['2']);
But I need to call 2 times unset... It's possible without 2 calls ?
Thanks, have a good day!
You can try like
$keyToRemove = array('1', '2');
foreach($keyToRemove as $key) {
unset($arr[$key]);
}
Also you can do it like this way too
$arr = array_diff_key($arr, array_flip($keyToRemove));
Similar answer you can check it here
This question already has answers here:
Get value without knowing key in one-pair-associative-array
(4 answers)
Closed 5 years ago.
i have this scenario of having a mixed response from a server and i need to process its data in PHP
Array
(
[14424174] => Array
(
[0] => Array
(
[id] => 45
[nm] => This is a driver name
[ph] => 5454545
)
)
)
I want to access id, nm, ph values
but i had no luck cause this index number (14424174) is unknown to me, so i need to first store this index and then parse the array
Use a nested foreach():
foreach($arr as $i => $sub_arr)
{
foreach($sub_arr as $sub_i => $sub_sub_arr)
{
$id = $sub_sub_arr['id'];
$nm = $sub_sub_arr['nm'];
$ph = $sub_sub_arr['ph'];
}
}
You can use the following pattern:
foreach($array as $key=>$val) {
//get the id:
var_dump($key)//14424174
$nm = $val[nm];
$ph = $val[ph];
}
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"])));
This question already has answers here:
How to find the foreach index?
(14 answers)
Closed 8 years ago.
So I may just be completely overcomplicating this for myself but I am trying to get the index of an array in a multidimensional array.
Hopefully showing it makes more sense.
Array
(
[1234] => Array
(
[Name] => Test
)
[5435] => Array
(
[Name] => Test
)
)
I have a large array with different numbers as the index and I need to do a foreach through them, but I need that index number. (1234,5435)
Is there any easy way of doing this?
Yes there is.
foreach ($arr as $key=>$val)
{
//do stuff
}
The $key is the key you need