How do I iterate list of dictionary in PHP? [duplicate] - php

This question already has answers here:
Loop through a two-dimensional array
(6 answers)
Closed 2 years ago.
How do I iterate list of dictionary returned from curl response in PHP?
Here is structure of a returned response:
Array (
[0] => Array (
[status] => open
[serviceSpecification] => https://testdomain.com
[expirationDate] =>
[name] => abcd.com
[service] => servicekey
[domainName] => domainname
[productInstanceUrl] => https://anotherinstanceurl.com
[createDate] => 2019-04-15
[serviceSpecificationTextKey] => test.core.key
[billingCycle] => 1
)
[1] => Array (
[status] => open
[serviceSpecification] => https://test.net
[expirationDate] =>
[name] => testname
[service] => https://service.com
[domainName] => test
[productInstanceUrl] => https://instanceurl.com
[createDate] => 2019-04-15
[serviceSpecificationTextKey] => core.test.key
[billingCycle] => 1
)
)
I tried doing following but not working:
foreach ($aboveVariable as $record) {
echo $record['domainName'];
}
Note: I believe its more of a how to iterate through list of list in PHP?

To just show the domainName for each then the code you show works. But based on this I believe its more of a how to iterate through list of list in PHP? I would say you just want to loop the main array and then the sub-arrays:
foreach($aboveVariable as $record) {
foreach($record as $name => $value) {
echo "$name = $value<br>\n";
}
echo "<br>\n";
}
Will show something like:
status = open
serviceSpecification = https://testdomain.com
etc...
status = open
serviceSpecification = https://test.net
etc...

Related

Pushing values into multidimensional array [duplicate]

This question already has answers here:
PHP add elements to multidimensional array with array_push
(4 answers)
Closed 2 years ago.
I'm already getting below array results as follows. All I wanted is to add more entries in it.
How do I do that?
Array
(
[result] => Array
(
[0] => Array
(
[number] => AAA1222
[short_description] => User unable to print
[state] => Closed
)
[1] => Array
(
[number] => AAA1223
[short_description] => Share drive not accessible
[state] => Closed
)
[2] => Array
(
[number] => AAA1224
[short_description] => Network is down
[state] => Closed
)
)
)
If I wanted to add more entries to the array like
[number] => AAA1225
[short_description] => Cable replacement on workstation 12
[state] => Closed
How do I accomplish that.
Thanks!!
You can try with array_push() function too.
Your existence array:
$myArr['result'] = [........]
New array values
$data['number'] = 'AAA1224';
$data['short_description'] = 'Network is down';
$data['state'] = 'Closed';
Push new array into existance array:
array_push($myArr['result'],$data);
Considering you are storing above array in a variable named as $arr.
$arr = [ ... ]; // contains your existing data.
// To add new data
$arrNewData = [
'number' => 'AAA1225',
'short_description' => 'Cable replacement on workstation 12',
'state' => 'Closed'
];
// Push data to existing array.
$arr['result'][] = $arrNewData;
You can add another array data to your existing array
Keep your existing data into a variable $data.
$data = [ ... ]; // here you have result index and array data
Then add new result item as an array.
$data['result'][] = [
'number' => 'AAA1225',
'short_description' => 'Cable replacement on workstation 12',
'state' => 'Closed'
];

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

How to sort an multidimensional array using Php? [duplicate]

This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 6 years ago.
I have an multidimensional array and want to sort in according to date and also want to get only 5 data from an array.
Array :
Array
(
[0] => Array
(
[ID] => 1
[TITLE] => example1
[DATE] => 2016-05-17
[PST_BY] => 0
[IMG_NM] =>
[SLUG] =>
[NAME] => Web Design & Development
)
[1] => Array
(
[ID] => 2
[TITLE] => example2
[DATE] => 2016-05-20
[PST_BY] => 0
[IMG_NM] =>
[SLUG] =>
[NAME] => Mobile OS
)
)
I am doing this but not working :
$list = array_sort($blog, 'DATE', SORT_ASC);
print_r($list);
Example to sort on a specific key (in this case name):
// Function to compare two items in the array
function CompareName($left, $right) {
return $left['name'] > $right['name'];
}
// Example array/data
$myarray = [
["id"=>1, "name"=>"foo"],
["id"=>2, "name"=>"bar"],
["id"=>3, "name"=>"ah"],
["id"=>4, "name"=>"zoo"]
];
echo 'Unsorted:';
var_dump($myarray);
usort($myarray , 'CompareName');
echo 'Sorted:';
var_dump($myarray);
want to get only 5 data from an array
$top5 = array_slice($myarray, 0, 5);
or:
$top5 = array_splice($myarray, 0, 5);

Sorting array of array by value in php [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I have an array named as $result which have contents something like this:
Array (
[1114_435] => stdClass Object
(
[uid] => 435
[v_first_name] => fHerter
[v_last_name] => Herter
[id] => 1114
[v_title] => Morning Stretch
[fk_resident_id] => 435
[v_location] => Front Lawn
[i_started_date] => 1357054200
)
[1114_444] => stdClass Object
(
[uid] => 444
[v_first_name] => fXYZ
[v_last_name] => XYZ
[id] => 1114
[v_title] => Morning Stretch
[fk_resident_id] => 444
[v_location] => Front Lawn
[i_started_date] => 1357054200
)
[1114_448] => stdClass Object
(
[uid] => 448
[v_first_name] => fDavidson
[v_last_name] => Davidson
[id] => 1114
[v_title] => Dinner
[fk_resident_id] => 448
[v_location] => Front Lawn
[i_started_date] => 1357051000
)
)
I want to sort it on the basis of i_started_date. I tried using ksort, asort etc but no luck, maybe i wasn`t using it properly. Any help would be highly appreciated.
Thanks!
Try something like this:
function sortArray($data)
{
$sortArray = array();
foreach($data as $dt)
{
foreach($dt as $key=>$value)
{
if(!isset($sortArray[$key]))
{
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}
$orderby = "1"; //change this to whatever key you want from the array
array_multisort($sortArray[$orderby],SORT_ASC,$data);
return $data;
}

Categories