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
Related
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have an array that contains player data. This array changes according to the number of players. The array looks like this:
Array
(
[0] => Array
(
[Id] => 0
[Name] => Playername1
[Frags] => -3
[Time] => 339
[TimeF] => 05:39
)
[1] => Array
(
[Id] => 0
[Name] => Playername2
[Frags] => 0
[Time] => 7
[TimeF] => 00:07
)
)
I want to get from this array from each player only the player name [name]. How do I do this? The output should be a string that looks something like this: Playername1, Playername2
I have not found anything on the Internet or on YouTube. The answer is certainly simple and obvious, but I have not found it.
Im using PHP 8.0.13.
$names = implode(',', array_column($arr, 'name'));
echo $names;
array_column: return the values from a single column in the input array.
implode: Join array elements with a string.
try something like this:
$player_names = '';
foreach($your_array as $key => $value){
$player_names .= $value['Name'].', ';
// this should concatenate all the player names
}
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
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);
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.
This question already has answers here:
Reset PHP Array Index
(4 answers)
Closed 8 years ago.
Array
(
[2] => stdClass Object
(
[name] => test-song-poll-03
[description] => test-song-poll-03
[created_at] => 2014-05-02T23:19:06Z
[count] => stdClass Object
(
[approved] => 26638
[pending] => 0
[rejected] => 36923
[total] => 63561
)
[tpm] => 47
[approved_tpm] => 9
[pct] => 2
)
)
I have a function that uses array_filter and it returns what you see above. It will only return one object within the array. I do not know what the array index will be, but I know there will only be one item in the array. Is there an array function that strips down the array and just returns the content of it, since I don't need an array with just one item in it.
You should use:
$x = array_values($yourArrayName);
echo $x[0];
You can also use:
echo current($yourArrayName);