I'm trying very simply to use in_array() to check a key is in an array and then echo its value.
$array = Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
And I have value 556729685 which I want to check that this value exists or not? So I am using in_array() function for this.
in_array(556729685, array_keys($array));
in_array(556729685, array_values($array));
in_array(556729685, $array);
All above three i have used but result always showing NULL means blank.
I am really frustrated to find the solution. I don't understand what's happening.
You should use array_column() which will return the values from a single column in the input array as an array.
$product_auto_ids = array_column($array['cart_item'], 'product_auto_id');
In this case, it would return the following:
Array
(
[0] => 556729685
[1] => 951790801
)
Then you can use in_array() like you currently are.
in_array(556729685, $product_auto_ids);
Related
how to get all values array but set all keys the same name
ex :
this is my array
but i need remove this key .. I have used array_values() .. but not working
enter image description here
Seems like you want to grab all the arrays inside the item keys? Using array_column and specifying the item as the key will return the arrays inside each item key in a new array. If that is what you're after? The question was kind of hard to understand IMHO.
array_column($array, 'item');
This will return an array on the following structure.
Array
(
[0] => Array
(
[name] => ttt
[price] => 100
[quantity] => 1
)
[1] => Array
(
[name] => sss
[price] => 100
[quantity] => 1
)
[2] => Array
(
[name] => vvv
[price] => 100
[quantity] => 1
)
)
Here's a demo showing it working.
I have a PHP array as follows:
print_r($myarray);
Array
(
[0] => Array
(
[JAN] => 484603732
[FEB] => 350203732
[MAR] => 133347732
[APR] => 203347732
[MEI] => 79797732
[JUNI] => 112047732
[JULI] => 380597732
[AGS] => 76597732
[SEP] => 86597732
[OKT] => 120397732
[NOV] => 391597732
[DES] => 58597732
)
)
I want to delete element like [JAN], [FEB], [MAR], ...
But I little bit confused about how to handle it with array_column
So far, I've tried:
$array = array_column($myarray, 'JAN','FEB','MAR','APR','MEI','JUNI','JULI','AGS','SEP','OKT','NOV','DES');
But, still facing error
array_column() expects at most 3 parameters, 13 given
My expected output:
Array
(
[0] => 484603732
[1] => 350203732
[2] => 133347732
[3] => 203347732
[4] => 79797732
[5] => 112047732
[6] => 380597732
[7] => 76597732
[8] => 86597732
[9] => 120397732
[10] => 391597732
[11] => 58597732
)
Any well thought to advise will be appreciated.
Thanks.
You don't need array_column() here. According to PHP documentation
array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.
You can simply use array_values() to remove all the keys.
$array = array_values($myarray[0])
I have the following array :
Array
(
[0] => Array
(
[Name] => first_data
[building] => A
[apt] => 16
)
[1] => Array
(
[Name] => first_data
[building] => B
[apt] => 16
)
[2] => Array
(
[Name] => second_data
[building] => A
[apt] => 17
)
[3] => Array
(
[Name] => second_data
[building] => B
[apt] => 18
)
and I need it to be returned as :
Array
(
[0] => Array
(
[Name] => first_data
[A] => 16
[B] => 16
)
[1] => Array
(
[Name] => second_data
[A] => 17
[B] => 18
)
Any ideas?
BTW the first array has hundreds of entries (not only first_data, but second and etc...) plus it has more than A and B.
Thanks in advance.
Not exactly what you want, but if you instead index the new array by the name, you can do this very easily. If the index number is some kind of ID, you can just create a field for it
foreach ( $oldarray as $index => $piece )
{
$newarray[$piece['Name']] = array($piece['building'] => $piece['apt'])
}
This will give you
Array
(
['first_data'] => Array
(
['A'] => 16,
['B'] => 16
)
['second_data'] => Array
(
['A'] => 17,
['B'] => 18
)
)
Since you have two entries with the same new, when you hit the 2nd loop, it will simply add the other building name. If you can work with this layout, then your solution is very easy, it will take more steps to do it exactly as you showed. If you absolutely have to do it the way you showed, you need extra code to loop through the new array, find the building name, add the key in the correct place, but this will be slower if you have a large amount of data.
In my opinion, the way I presented it is a far easier way to look around the array too. If you wanted to know the apt value for A in "second_data" you can just do
$newarray['second_data']['A']
with your array layout, it would require a loop to search the array for "second_data" because you have no idea where it is.
iam trying to sort this array by array[key]['premium']['Monthly'] and if there are two Monthly prices the same, then sort by quarterly, then semi-annual, then annual.
i search and couldnt figure out how to use array_multisort function.
Array (
[0] => Array (
[product_id] => 1
[rate] => 27.07
[premium] => Array (
[Annual] => 436.05
[Semi-Annual] => 226.75
[Quarterly] => 115.6
[Monthly] => 37.11
)
)
[1] => Array (
[product_id] => 2
[rate] => 35.00
[premium] => Array (
[Annual] => 565
[Semi-Annual] => 293.8
[Quarterly] => 149.73
[Monthly] => 50.85
)
)
[2] => Array (
[product_id] => 3
[rate] => 30.52
[premium] => Array (
[Annual] => 497.8
[Monthly] => 47.29
)
)
)
I think you want to use usort function, something like
function compare($a, $b){
$p1 = $a["premium"];
$p2 = $b["premium"];
if($p1["Monthly"] == $p2["Monthly"]){
// compare by quarterly
...
}else{
if($p1["Monthly"] < $p2["Monthly"])then return -1;
else return 1;
}
}
usort($prices, "compare");
where $prices is your array. The comparision function isn't implemented fully, just to show the idea. Also, since it looks like there might be missing items in the price array (ie last one misses Quarterly and Semi-Annual) you have to check first (before comparison) does the items exists and take appropriate action in case one or both are missing.
I want to make it so that my multi dimensional array is in a random order. How would you do it?
// This is how the array looks like
print_r($slides);
Array
(
[0] => Array
(
[id] => 7
[status] => 1
[sortorder] => 0
[title] => Pants
)
[1] => Array
(
[id] => 8
[status] => 1
[sortorder] => 0
[title] => Jewels
)
[2] => Array
(
[id] => 9
[status] => 1
[sortorder] => 0
[title] => Birdhouse
)
[3] => Array
(
[id] => 10
[status] => 1
[sortorder] => 0
[title] => Shirt
)
[4] => Array
(
[id] => 11
[status] => 1
[sortorder] => 0
[title] => Phone
)
)
// This how the result is if I use array_rand()
print_r(array_rand($slides, 5));
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
// This how the result is if I use shuffle()
print_r(shuffle($slides));
1
shuffle() is the way to go here. It prints 1 because shuffle changes the array in-place and returns a boolean, as it is written in the documentation:
Returns TRUE on success or FALSE on failure.
I suggest to also read the documentation of array_rand():
Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.
Always read documentation if you use built-in functions. Don't just assume how the work. I bet it took more time to write the question than looking this up.
Instead of
print_r(shuffle($slides));
do
shuffle($slides);
print_r($slides);
You see shuffle() shuffles the array in-place
i am not sure how you want it to display but you can loop the array and use php rand(0,arraylen) function to parse the array.
It works perfect. print_r(shuffle($slides))) gives the output of TRUE, since the return value of shuffle is a boolean and not an array.
See the working example here: http://codepad.org/B5SlcjGf