Array_unique is not working [duplicate] - php

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"])));

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.

PHP Pull data out of an array [duplicate]

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

Collect all values from nested array row of specific key [duplicate]

This question already has answers here:
How to get an array of specific "key" in multidimensional array without looping [duplicate]
(4 answers)
Closed 1 year ago.
Need to create a list that consists of all values stored in the array row of a specific key (product_id). Currently, doing a print_r of my $bestsellers variable produces the following array:
Array
(
[0] => stdClass Object
(
[product_id] => 178
[order_item_qty] => 9
)
[1] => stdClass Object
(
[product_id] => 233
[order_item_qty] => 4
)
[2] => stdClass Object
(
[product_id] => 179
[order_item_qty] => 1
)
)
Other SO answers led me to try:
$ids = array_column($bestsellers, 'product_id');
...but that produced an empty array, I guess because the row I’m trying to grab is nested in there? With that in mind I tried
foreach($bestsellers as $bestseller) {
$ids = array_column($bestsellers, 'product_id');
}
...which produced no result at all.
Hopefully someone can help clue me in as to where I’m going wrong. Thanks!
The nested values are objects, not arrays (can't you see stdClass Object in the output?). array_column is for 2-dimensional arrays. You need to access the properties using object syntax.
$ids = array_map(function($x) { return $x->product_id; }, $bestsellers);
For future reference, array_column will work for this in PHP 7, so you must be using PHP 5.
For PHP 7, your code
$ids = array_column($bestsellers, 'product_id');
will do what you want it to.
See the difference here on 3v4l.org.

Combine two array values to make them key=>value format [duplicate]

This question already has answers here:
How to combine two arrays together?
(3 answers)
Closed 6 years ago.
I am having two arrays like this
Array
(
[0] => username
[1] => avatar
)
Array
(
[0] => name
[1] => 4.jpg
)
Here, I need to get these values in the following format
'username'=>name,'avatar'=>4.jpg
i.e., merge the same key values in the above format..
How should I do this,..Someone help me..
If you think that my title is wrong,Please change it into correct format.
Thanks,
Use array_combine()
$final_array = array_combine($fist_array,$second_array);
Reference:- http://php.net/manual/en/function.array-combine.php
use $c = array_combine($a, $b);

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

Categories