Get array sub-element when the array name is unique in php - php

I have this array object:
//$array
Array (
[#insert_long_unique_id] =>
Array (
[0] => WP_Post Object ( [ID] => 770
) )
[#insert_long_unique_id] =>
Array (
[0] => WP_Post Object ( [ID] => 530
) )
The #insert_long_unique_id is an auto-generated ID and I dunno which method or plugin generate it but it's always different.
I need to reach and echo the [ID] => 770 (first-element) only in my project.

You can do it through array_column()
$id_array = array_column($array,'ID');
echo $id_array[0]; //print first-id
//In case if you want to print all ID's
foreach($id_array as $id_arr){
echo $id_arr;
}

If your array variable name is $array, then you can access to ID inside of object like this:
foreach ($array as $key => $value) {
if (!empty($value)) {
if(!empty($value[0]) && is_object($value[0])){
$myid = $value[0]->ID;
}
}
}

Related

Replace every string in multidimensional array if conditions matched

Ok so I have an array look like this,
Array
(
[0] => Array
(
[0] => order_date.Year
[1] => =
[2] => 2024
),
[1] => Array
(
[0] => order_date.Quarter
[1] => =
[2] => 1
)
)
What I want to do is, in any element of this multidimensional array I want to replace any string that have a . with removing everything after .
So the new array should look like this,
Array
(
[0] => Array
(
[0] => order_date
[1] => =
[2] => 2024
),
[1] => Array
(
[0] => order_date
[1] => =
[2] => 1
)
)
I have tried doing this,
foreach ($filter as $key => $value) {
if(is_array($value)) {
$variable = substr($value[0], 0, strpos($value[0], "."));
$value[0] = $variable;
}
}
print_r($filter);
I'm getting $value[0] as order_date but can't figure out how to assign it to $filter array without affecting other values in array;
The $value variable is not linked with the original array in the foreach loop.
You can make a reference to the original array by using ampersand "&"
foreach ($filter as $key => &$value) { ... }
Or you can use old school key nesting
$filter[$key][0] = $variable;
Please take a look here https://stackoverflow.com/a/10121508/9429832
this will take off values after . in every element of any multidimensional array.
// $in is the source multidimensional array
array_walk_recursive ($in, function(&$item){
if (!is_array($item)) {
$item = preg_replace("/\..+$/", "", $item);
}
});

Return aggregated array from input multidimensional array

I've got two inputs, an ID and multidimensional array
For example, let's say the ID = 5 and the array looks like this:
Array
(
[0] => stdClass Object
(
[year] => 2017
[value] => a:9:{i:5;b:1;i:38;b:1;i:40;b:1;i:42;b:1;i:44;b:1;i:29;b:1;i:46;b:1;i:27;b:1;i:48;b:1;}
)
[1] => stdClass Object
(
[year] => 2018
[value] => a:9:{i:31;b:1;i:5;b:1;i:25;b:1;i:16;b:1;i:27;b:1;i:29;b:1;i:12;b:1;i:14;b:1;i:34;b:1;}
)
[2] => stdClass Object
(
[year] => 2018
[value] => a:3:{i:12;b:1;i:14;b:1;i:16;b:1;}
)
)
I need to unserialize the value and aggregate the data yearly. For example of ID = 5, the output should look like this:
Array
(
[2017] => 1
[2018] => 1
)
Currently I've got so far as to unserialize the value part:
foreach($results as $object=>$result){
echo $result->year;
echo "<br>";
echo join(',', array_keys(unserialize($result->value)));
echo "<br>";
}
Please advise on how to move on from here.
After you unserialize the value, check if the element whose key is ID is set. If it is, increment a counter for the year.
$id = 5;
$output = array();
foreach ($results as $result) {
$value = unserialize($result->value);
if (!empty($value[$id])) {
if (isset($output[$result->year])) {
$output[$result->year]++;
} else {
$output[$result->year] = 1;
}
}
}
print_r($output);

How To Access Values In Associative Array Using PHP

I have an array which is the result of a select query using Amazon SimpleDb.
Here is sample data when I print_r($result);
Array ( [0] => Array ( [Name] => 5140ede647e74
[Attributes] => Array (
[0] => Array ( [Name] => test_id [Value] => 5140ede647e74 )
[1] => Array ( [Name] => test_name [Value] => test1 )
[2] => Array ( [Name] => last_update [Value] => 1363209702 )
[3] => Array ( [Name] => created [Value] => 1363209702 ) ) ) )
If I want to extract the test_id and the test_name, how can I do it? I am currently doing the following
<?php foreach ($result as $item) {
echo $item['Attributes'][0]['Value'];
echo $item['Attributes'][1]['Value'];
} ?>
But I want to do it by referencing "test_id" and "test_name" because when I delete the domain where the data resides and re-enter the data, the order of each attribute can change so I can't trust that $item['Attributes'][0]['Value'] will always be the test_id
Thanks!
foreach ($result as $item) {
foreach ($item['Attributes'] as $keyvalue) {
if ($keyvalue['Name'] == 'test_id' || $keyvalue['Name'] == 'test_name') {
echo $keyvalue['Value'];
}
}
}
You need to recast the Array.
$newArray = array();
foreach ($result as $key=>$row)
{
foreach ($row['Attributes'] AS $row2)
{
$newArray[$key][$row2['Name']] = $row2['Value'];
}
}
EDIT: It depends on what you need to do - this is my preferred method if I plan on doing a lot of work with a resultset - I only need to iterate through the set once and then it's in a format where the data can be accessed quickly.
The following will run trough the last part of your array by reference. Therefore edits that you make are reflected in the $result array.
foreach ($result[0]['Attributes'] as &$item) {
if ($item['Name'] == 'test_id') // do something
}

PHP: Adding a key-value pair to an array

I have an array that looks like this:
$array = Array ( [0] => Array ( [site_id] => 89193)
[1] => Array ( [site_id] => 89093)
[2] => Array ( [site_id] => 3059 )
)
What I want to do is to add a new pair to the array, how can I do so?
For example, next to site_id, I want to add the key [site_name] and its value.
foreach ($array as &$child) {
$child['site_name'] = $value;
}
or, without references:
foreach (array_keys($array) as $key) {
$array[$key]['site_name'] = $value;
}

Need help to understand result set from PDO

I have the following function executing PDO queries:
// removed error handling for presenting here
function getRows($sql) {
$stmt = $this->db->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
The result is:
Array
(
[0] => Array
(
[id] => 1
[category] => Audi
)
[1] => Array
(
[id] => 2
[category] => BMW
)
[2] => Array
(
[id] => 3
[category] => Chrysler
)
)
The the following foreach code:
foreach($result as $key => $value ) {
echo $value.'<br/>';
}
outputs this:
Array
Array
Array
What can I do so it returns the following?
Audi
BMW
Chrysler
I understand that I could just do $value['category].
But that's not what I want to achieve / understand. I would like the resultset not to be an array of arrays.
try
foreach($result as $key => $value ) {
echo $value['category'].'<br/>';
}
Alternative
foreach($result as $k)
{
echo $k['category'];
}
The foreach loop splits up your array into key, value pairs. The key in your loop is the index of the array, the value is an array containing ID and Category.
To access the category simply do:
foreach($result as $key => $value ) {
echo $value['category'].'<br/>';
}

Categories