Getting a value from an PHP associative array - php

I'm not sure how I would loop through this array to get a value from the 3rd array ([0] => Array). How would I go about getting the value of leaguePoints?
Array
(
[4605] => Array
(
[0] => Array
(
[name] => Hecarim's Duelists
[tier] => DIAMOND
[queue] => RANKED_SOLO_5x5
[entries] => Array
(
[0] => Array
(
[playerOrTeamId] => 4605
[playerOrTeamName] => External
[division] => I
[leaguePoints] => 17
[wins] => 223
[isHotStreak] =>
[isVeteran] => 1
[isFreshBlood] =>
[isInactive] =>
)
)
)
)
)

try for getting all values of array
foreach($arr[4605][0]['entries'][0] as $v) {
echo $v;
}
for getting in single statement try
echo $arr[4605][0]['entries'][0]['leaguePoints'];

This is 5 nested arrays. To get leaguePoints, you will need to use something similar to
echo $arr[4605][0]['entries'][0]['leaguePoints'];

Related

Loop through Multidimensional array and update mysql table using codeignitor 3

I am trying to loop through multidimensional array to update the values in mysql table in codeignitor 3
I have an issue in looping through the values in multidimensional array
Below is my Multidimensional array:
$data = array(
'invoice_id' => $invoice_id,
'date' => $this->input->post('date',true),
'commercial_invoice_number' => $this->input->post('commercial_invoice_number',true),
'payment_type' => $this->input->post('payment_type',true)
);
When printing this array ,$data contains as below
(
[invoice_id] => Array
(
[0] => 4549344712
[1] => 4549344713
[2] => 4549344814
)
[date] => Array
(
[0] => 25-11-2022
[1] => 22-12-2022
[2] => 12-10-2022
)
[commercial_invoice_number] => Array
(
[0] => NS202211-4
[1] => NS202211-5
[2] => NS202211-6
)
[payment_type] => Array
(
[0] => 0
[1] => 1
[2] => 0
)
)
print_r($invoice_id) returns as below
Array ( [0] => 4549344712 [1] => 4549344713 [2] => 4549344814 )
foreach ($invoice_id as $value) {
$this->db->where('invoice_id', $value);
$this->db->update('invoice', $data);
echo $this->db->last_query();
}
The above code giving me Array to String conversion error.

Get the values of an associaive array [PHP]

How can i get the values of the array bellow?
Array
(
[list_items] => Array
(
[0] => Array
(
[productName] => BUGGY INFANTIL PRO 125CC
[productId] => 1
[productColor] => Azul
[productQuantity] => 2
[productIMG] => http://localhost/kasi_src/img/vehicules/buggygasolina/BUGGY-INFANTIL-125CC-AZUL.png
[productURL] => http://localhost/kasi_src/125CCInfantil.html
[productPrice] => 1000
)
[1] => Array
(
[productName] => PATINETE 24V
[productId] => 2
[productColor] => Azul
[productQuantity] => 2
[productIMG] => http://localhost/kasi_src/img/vehicules/patinetes/24.png
[productURL] => http://localhost/kasi_src/patinete-24v.html
[productPrice] => 240
)
)
)
I tried a simple foreach loop
foreach($items as $key => $value){
echo $value . "\n";
}
but i ended up getting this error : Notice: Array to string conversion in .....
Any ideas ?
You cannot echo a array.
foreach($array["list_items"] as $list_item){
echo $list_item["productName"];
}
Use var_export($obj,true) if you want to get a textual representation of the full content of each array associated to a key in an associative array as in the example, or simply access the keys of each subarray to retrieve individual values as productName.
<?php
$list_items = array(
0 => array("productName" => "Marshmallows"),
1 => array("productName" => "Mikado"),
);
foreach($list_items as $key=>$obj){
echo var_export($obj,true)."<br/>";
// Or access each array key, for example: echo $obj['productName'];
}
?>
Output:
array ( 'productName' => 'Marshmallows', )
array ( 'productName' => 'Mikado', )

PHP Array Unique Sort Regular not working as expected

I'm using print_r(array_unique($array, SORT_REGULAR)); on the array below but it does not work.
I'm trying to filter out the redundant data.
Notice that [Order] and its key value pairs are all the same. But [Transaction] and its key value pairs are unique.
I need to get the [Order] element data and combine it with the 3 different [Transaction] elements.
My array
Array
(
[0] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
)
[1] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
)
[2] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
The final array I need will look something like this.
Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[0] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
I can flatten the original array and then use array_unique, but wanted to see if there is a better way to accomplish what I need.
my code:
$myarray = array(
0 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11211", "TransactionPrice" => 91.17)
),
1 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11212", "TransactionPrice" => 180.41)
),
2 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11213", "TransactionPrice" => 209.99)
)
);
print_r(array_unique($myarray, SORT_REGULAR));
If you want to determine how many unique values of the Order element there are in your array, you need to apply array_unique only to the Order elements, which you can do using array_column:
$unique_orders = count(array_unique(array_column($myarray, 'Order'), SORT_REGULAR));
You can process your array using a list of keys which have non-unique values to generate an array, while other keys will have just a single value:
$non_unique_keys = ['Transaction'];
$output = array();
foreach (array_keys($myarray[0]) as $key) {
if (in_array($key, $non_unique_keys)) {
$output[$key] = array_column($myarray, $key);
}
else {
$output[$key] = $myarray[0][$key];
}
}
print_r($output);
Example Output:
Array (
[Order] => Array (
[PO] => TR11214
[OrderID] => 242856952012
)
[Sales Tax] => Array (
[PO] => TR11214
[SalesTaxAmount] => 0
)
[Transaction] => Array (
[0] => Array (
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array (
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array (
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
Demo on 3v4l.org
array_unique() is intended for single dimensional arrays. If you want to use it on a multi-dimentional array, you should consider using usort() instead. Then you'll need to iterate through the array in reverse manually, searching for duplicates and removing them.

How to populate missing array data by flipping 2nd and 3rd level keys?

I have a multidimensional array and I need to ensure that paired/related subarrays are generated in the deepest level of my array.
The idea is that subarrays [65][1] and [155][1] should exist. At the same time, subarrays [65][2] and [155][2] should exist.
In other words, [nn][1] and [nn][2] must exist.
I would like to be able to automatically add subarrays which do not exist. I do not know how to do it with PHP.
So, I am looking for a code which goes through the array and creates subarrays which do not exist.
Here is an example.
Array (
[65] => Array (
[1] => Array (
[2] => Array (
[points] => 0000000600
[competition] => 0000000011
)
)
)
[155] => Array (
[1] => Array (
[2] => Array (
[points] => 0000000900
[competition] => 0000000011
)
)
[2] => Array (
[1] => Array (
[points] => 0000000750
[competition] => 0000000025
)
)
)
}
However, the subarray [65][2] does not exist.
As said, I am looking for a code which goes through the array and creates subarrays which do not exist.
The result should look like this:
Array (
[65] => Array (
[1] => Array (
[2] => Array (
[points] => 0000000600
[competition] => 0000000011
)
)
/* this should be added automatically */
[2] => Array (
[1] => Array (
[points] => 0000000000
[competition] => 0000000000
)
)
)
/* */
[155] => Array (
[1] => Array (
[2] => Array (
[points] => 0000000900
[competition] => 0000000011
)
)
[2] => Array (
[1] => Array (
[points] => 0000000750
[competition] => 0000000025
)
)
)
}
Notice that the default data is assign to a new subarray which is assigned 2nd and 3rd level keys using the 3rd and 2nd level keys respectively.
If I understood you correctly, all the elements in your main array are themselves arrays, and each of them must have both a [1] and a [2] element. You can check for this with the array_key_exists() function. Something like:
foreach($rg as $rgkey) {
if(!array_key_exists(1,$rg[$rgkey])) {
/* code to initialize $rg[$rgkey][1] */ }
if(!array_key_exists(2,$rg[$rgkey])) {
/* code to initialize $rg[$rgkey][2] */ }
}
Traverse the top two levels of your structure and then check for the expected third level key, if it doesn't exist, populate the new subarray with the default data using flipped keys.
Code: (Demo)
foreach ($rg as $id1 => $level2) {
foreach ($level2 as $id2 => $level3) {
$id3 = key($level3);
if (!isset($level2[$id3])) {
$rg[$id1][$id3][$id2] = [
'points' => '0000000000',
'competition' => '0000000000'
];
}
}
}
var_export($rg);
Output:
array (
65 =>
array (
1 =>
array (
2 =>
array (
'points' => '0000000600',
'competition' => '0000000011',
),
),
2 =>
array (
1 =>
array (
'points' => '0000000000',
'competition' => '0000000000',
),
),
),
155 =>
array (
1 =>
array (
2 =>
array (
'points' => '0000000900',
'competition' => '0000000011',
),
),
2 =>
array (
1 =>
array (
'points' => '0000000750',
'competition' => '0000000025',
),
),
),
)

Return array with all data in key

I have this array $allYearData
Now i´d like to split it into 12 parts, seperated by month.
So i use PHP preg_grep() with a foreach loop to search for data.
foreach($allYearData as $key){
$janData[] = preg_grep("/^2015-01-.*$/", $key);
}
How can i fetch all data in each key?
As you can see in $janData i get empty keys where there is no hits. And then only the date if there is a hit.
I´d like $janData to be a new array with key 0 to contain the first hit and all data/values in that key. Like $allYearData but only with the hits from preg_grep.
$allYEarData:
Array (
[0] => Array (
[id] => 7811
[objekt_element] => 23050-121-1_3105
[objekt_nr] => 23050-121-1
[element_nr] => 3105
[vart] => B.Avf
[vem] => Blå
[anteckn] =>
[datum] => 2015-09-29 18:00:19
)
[1] => Array (
[id] => 7812
[objekt_element] => 23050-121-1_3107
[objekt_nr] => 23050-121-1
[element_nr] => 3107
[vart] => B.Avf
[vem] => Blå
[anteckn] =>
[datum] => 2015-09-29 18:00:22
)
[2]...
$janData
Array (
[0] => Array ( )
[1] => Array ( )
[2] => Array ( )
[3] => Array ( )
[4] => Array ( )
[5] => Array (
[datum] => 2015-01-16 04:17:01
)
[6] => Array (
[datum] => 2015-01-16 04:16:57
)
[7]
I´d like $janData to be filled
Array (
[0] => Array (
[id] => 7811
[objekt_element] => 23050-121-1_3105
[objekt_nr] => 23050-121-1
[element_nr] => 3105
[vart] => B.Avf
[vem] => Blå
[anteckn] =>
[datum] => 2015-01-16 18:00:19
)
$key in you foreach is array, so select where is a date you want to hit and use $key to add to result array.
foreach($allYearData as $key){
if (preg_grep("/^2015-01-.*$/", $key['datum']){
$janData[] = $key;
}
}

Categories