How to split up the value as an array [duplicate] - php

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 4 years ago.
How to split up the value to an array. I have the whmcs api data it looks like 1,2,3,4,5. I need to split up the value to an array value suggest any solution
My api data
Array
(
[result] => success
[totalresults] => 2
[promotions] => Array
(
[promotion] => Array
(
[0] => Array
(
[id] => 6
[code] => 0UP3NU5J7J
[type] => Percentage
[recurring] => 0
[value] => 15.00
[cycles] => One Time,Monthly,Quarterly,Semi-Annually,Annually,Biennially,Triennially,1Years
[appliesto] => 1,2,3
[requires] => 1,2,3
[requiresexisting] => 0
[startdate] => 2018-10-22
[expirationdate] => 2018-10-25
[maxuses] => 0
[uses] => 0
[lifetimepromo] => 0
[applyonce] => 0
[newsignups] => 0
[existingclient] => 0
[onceperclient] => 0
[recurfor] => 0
[upgrades] => 0
[upgradeconfig] => a:4:{s:5:"value";s:4:"0.00";s:4:"type";s:0:"";s:12:"discounttype";s:10:"Percentage";s:13:"configoptions";s:0:"";}
[notes] =>
)
)
)
)
My code is
#foreach($response['promotions']['promotion'][0] as $key => $value)
#if($key == 'appliesto')
{{$var=$value}}
#endif
#endforeach
I need to split the "appliesto" value as an array.Please suggest any solution

No need of loop:-
$response['promotions']['promotion'][0]['appliesto'] = explode(',',$response['promotions']['promotion'][0]['appliesto']);
But if promotions can have multiple child array then do:-
#foreach($response['promotions']['promotion'] as &$value)
$value['appliesto'] = explode(',',$value['appliesto']);
#endforeach
Reference:- explode()

just use the explode function like :
$apelistoArray = explode(',',$value);
This will return an array of the values that the appliesto field has so you need to parse them as an array.
This new array has to be parsed inside a loop before you use the curly brackets to echo the values in the blade.
So you have to do something like below:
#foreach($apelistoArray as $data)
{{$data}}
#endforeach

Related

PHP associative Array get Array Id [duplicate]

This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 3 years ago.
Hi I cant get my head around this one I have the following array :
Array
(
[questions] => Array
(
[585] => Array
(
[correct] => 1
[mark] => 1
[type] => single_choice
[answered] => 1
)
[596] => Array
(
[correct] =>
[mark] => 0
[type] => true_or_false
[answered] => 1
)
[595] => Array
(
[correct] => 1
[mark] => 1
[type] => single_choice
[answered] => 1
)
)
)
I am trying to get the arrays number in a foreach statement here is my code , it works for everything else apart from the numbers I just need to get either 585,596 ir 595 in the foreach look .
<?php
/// $quiz_res is the array
foreach($quiz_res['questions'] as $result) {
echo key($result); //// DOES NOT WORK
##### I need to get the number here eg 585 ???
echo $result['correct']; /// this works
echo $result['mark']; /// this works
echo $result['type']; /// this works
echo $result['answered']; /// this works
}
?>
Also it shouldnt matter but this is relating to the results of a learnpress quiz if any one is familiar with them.
Any help or pointers would be greatly appreciated
You have to name the index in the foreach call:
foreach($quiz_res['questions'] as $id => $result) {
echo $id; // 585

Looking for duplicated values in an array for a certain key [duplicate]

This question already has answers here:
Get the keys for duplicate values in an array
(11 answers)
Closed 4 years ago.
I have an array:
Array
(
[0] => Array
(
[sku_code_part_id] => 1
[part_label] => blue
[part_value] => BLU
)
[1] => Array
(
[sku_code_part_id] => 2
[part_label] => Orange
[part_value] => ORG
)
[2] => Array
(
[sku_code_part_id] => 3
[part_label] => Orange
[part_value] => ORG
)
[3] => Array
(
[sku_code_part_id] => 4
[part_label] => Orange
[part_value] => ORG
)
[4] => Array
(
[sku_code_part_id] => 5
[part_label] => Green
[part_value] => GRE
)
[5] => Array
(
[sku_code_part_id] => 6
[part_label] => Red
[part_value] => RED
)
)
I'm wanting a simple way of checking the array $this->request->post['custom_parts'] for the any duplicated values assigned to the part_value keys.
So I can flag an error that 'ORG' is duplicated numerous times.
I've tried various methods such as removing duplications and comparing before and after. However I'm running into a number of issues with this.
Any ideas?
You may be able to use "array_key_exists"
http://php.net/manual/en/function.array-key-exists.php
You may want to write a function but here is a solution using foreach.
$part_values = [];
$part_values_duplicates = [];
foreach($this->request->post['custom_parts'] as $customPart){
if(!in_array($customPart['part_value'], $part_values)){
$part_values[] = $customPart['part_value'];
}
else {
$part_values_duplicates[] = $customPart['part_value'];
}
}
var_dump($part_values_duplicates);

merging array in CI 3

I want to merge two arrays in order to get the data as per my requirement.
I am posting my result, please have a look.
First array:
Array
(
[0] => Array
(
[km_range] => 300
[id] => 2
[car_id] => 14782
)
[1] => Array
(
[km_range] => 100
[id] => 3
[car_id] => 14781
)
[2] => Array
(
[km_range] => 300
[id] => 4
[car_id] => 14783
)
)
Second array:
Array
(
[0] => Array
(
[user_id] => 9c2e00508cb28eeb1023ef774b122e86
[car_id] => 14783
[status] => favourite
)
)
I want to merge the second array into the first one, where the value at key car_id matches the equivalent value; otherwise it will return that field as null.
Required output:
<pre>Array
(
[0] => Array
(
[km_range] => 300
[id] => 2
[car_id] => 14782
)
[1] => Array
(
[km_range] => 100
[id] => 3
[car_id] => 14781
)
[2] => Array
(
[km_range] => 300
[id] => 4
[car_id] => 14783
[fav_status] => favourite
)
)
Since the merge is so specific I would try something like this:
foreach ($array1 as $index => $a1):
foreach ($array2 as $a2):
if ($a1['car_id'] == $a2['car_id']):
if ($a2['status'] == "favourite"):
$array1[$index]['fav_status'] = "favourite";
endif;
endif;
endforeach;
endforeach;
You might be able to optimize the code more but this should be very easy to follow...
Another way to achieve this without using the index syntax is to reference the array elements in the foreach by-reference by prepending the ampersand operator:
foreach($firstArray as &$nestedArray1) {
foreach($secondArray as $nestedArray2) {
if ($nestedArray1['car_id'] == $nestedArray2['car_id']) {
$nestedArray1['fav_status'] = $nestedArray2['status'];
}
}
}
You can see it in action in this Playground example.
Technically you asked about merging the arrays. While the keys would be different between the input arrays and the desired output (i.e. "status" vs "fav_status"), array_merge() can be used to merge the arrays.
if ($nestedArray1['car_id'] == $nestedArray2['car_id']) {
$nestedArray1 = array_merge($nestedArray1, $nestedArray2);
}
Playground example.
Additionally the union operators (i.e. +, +=) can be used.
If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator1
if ($nestedArray1['car_id'] == $nestedArray2['car_id']) {
$nestedArray1 += nestedArray1;
}
Playground example.
1http://php.net/manual/en/function.array-merge.php#example-5587

convert array into array to string is possible..? [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 6 years ago.
How to convert array within array to string, that means I am having a one result set having value of country id but the result set will be in array within array.
Something like below code :
Array
(
[0] => Array
(
[country_id] => 7
)
[1] => Array
(
[country_id] => 8
)
[2] => Array
(
[country_id] => 9
)
[3] => Array
(
[country_id] => 10
)
[4] => Array
(
[country_id] => 1
)
[5] => Array
(
[country_id] => 2
)
[6] => Array
(
[country_id] => 3
)
[7] => Array
(
[country_id] => 4
)
[8] => Array
(
[country_id] => 5
)
)
I want that country list into one string like 7,8,9,10,1,2,3,4,5 but without looping..
Anyone have idea please let me know...?
You can use array_column() and implode() function to do this.
Here is how you can do it,
$values=array_column($array,"country_id");
$country_string=implode($values);
array_column() returns the values from a single column of the input,
identified by the column_key(country_id).
implode() returns a string containing a string representation of all the array elements in the same order, with the glue string between
each element.
implode() can have two arguments, first as glue by which you want to join the elements and second as the array of elements. If first argument is not given then default glue is ",".
Use array_column and implode for (PHP 5 >= 5.5.0, PHP 7) as
$data = array_column($records, 'country_id');
echo implode(",",$data);
try this,
$newarray = array();
foreach ($array as $item) {
$newarray[] = $item['country_id'];
}
echo implode(",",$newarray);
i hope it will be helpful.

Array_Unique filtering

I have an multidimensional array:
Array
(
[0] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Other Cereals
[IName] =>
[Date] => 2013-02-05
)
[1] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Porridge
[IName] => Oats,Milk,Sugar
[Date] => 2013-02-06
)
[2] => Array
(
[Id] => 1
[MTime_Id] => 1
[MName] => Breakfast
[DName] => Porridge
[IName] => Oats,Milk,Sugar,Oats,Milk,Sugar
[Date] => 2013-02-05
)
)
And I am trying to use array unique to filter this
[IName] => Oats,Milk,Sugar,Oats,Milk,Sugar
I am having no luck. How can I filter the duplicates?
Cheers.
If you filter input and therefore don't have extra spaces in IName field, you can use something as simple as this for filtering:
$array[2]['IName'] = implode(',', array_unique(explode(',', $array[2]['IName'])));
The problem is that you habe in array two Oats,Milk,Sugar as element of IName, in array three you have Oats,Milk,Sugar,Oats,Milk,Sugar. This is not the same!
"Oats,Milk,Sugar"=="Oats,Milk,Sugar,Oats,Milk,Sugar" (or "Oats,Milk,Sugar".equals("Oats,Milk,Sugar,Oats,Milk,Sugar")) is false.
If you want to have it unique you have to explode the single results and then do a unique on it or you have to store the single values in seperate fields...
BTW: Here is a link how to remove duplicates from a multi dimensional array How to remove duplicate values from a multi-dimensional array in PHP
I am not sure if a function exists for that, here is a simple solution,
you can loop the array, and get the result of each value, then explode result, and insert it into an array.
then use the array_unique function.
try this:
$result = array();
foreach($arrays as $array)
{
$tmp = $array['IName'];
$tmp2 = explode(',',$tmp);
foreach ($tmp2 as $t)
{
$result[]=$t;
}
}
$array_unique = array_unique($result);

Categories