Differentiate between dynamic and manually assigned array keys - php

I was looking for a way to shorten my code, and could not find the answer to this question.
In this sample array:
$addressData = array(
'Full Name',
'Address Line 1',
'Address Line 2',
30 => 'City/Town',
22 => 'Province/State/Region',
27 => 'Postal/ZIP code',
'Country',
);
The first three values will be assigned keys 0, 1, and 2, respectively. The last value (Country) will be assigned key 31.
When I walk through this array, is there any way that I can treat those manually assigned values in different manner than the automatically assigned values?
I can't find anything in the manual, but I thought the community might be able to help.
Edit for further clarification. This is not real code, but it explains what I hope to achieve:
foreach($addressData as $key => $value){
if(is_set_manually($key)){
$someValue = TRUE;
}
}

Not sure if i understand your question correctly, but if you want to fix sequence if it's broken you can do something like this:
<?php
$addressData = array(
'Full Name',
'Address Line 1',
'Address Line 2',
30 => 'City/Town',
22 => 'Province/State/Region',
27 => 'Postal/ZIP code',
'Country',
);
$result= [];
$first = 0;
foreach($addressData as $key=>$data){
if($key != $first && ($key > $first+1)){
$newkey = $first;
$result[$newkey] = $data;
}else{
$result[$key] = $data;
}
$first++;
}
print_r($result);
And new array $result looks like:
Array
(
[0] => Full Name
[1] => Address Line 1
[2] => Address Line 2
[3] => City/Town
[4] => Province/State/Region
[5] => Postal/ZIP code
[6] => Country
)

If you can assign array like following format may be helpful for your answer.
<?php
$addressData = array(
'Full Name',
'Address Line 1',
'Address Line 2',
array(30 => 'City/Town'),
array(22 => 'Province/State/Region'),
array(27 => 'Postal/ZIP code'),
'Country',
);
foreach($addressData as $key => $value){
if(is_array($value)){
echo 'Manually Key<br>';
} else {
echo 'Dynamic Key<br>';
}
}
?>

Related

Loop an array and retain only elements that relate to a specific key with a qualifying value

I have this array :
(
[id] => block_5df755210d30a
[name] => acf/floorplans
[data] => Array
(
[floorplans_0_valid_for_export] => 0
[floorplans_0_title] => title 1
[floorplans_0_house_area] => 40m²
[floorplans_0_bedrooms] => 1
[floorplans_1_valid_for_export] => 1
[floorplans_1_title] => title xx
[floorplans_1_house_area] => 90m²
[floorplans_1_bedrooms] => 2
[floorplans_2_valid_for_export] => 1
[floorplans_2_title] => title 2
[floorplans_2_house_area] => 50m²
[floorplans_2_bedrooms] => 1
[floorplans] => 3
)
)
As we can see in the data, we have fields (floorplans_X_valid_for_export).
What I want to do is to get the data only when this field equal to 1.
So from the given example, I want to keep only these fields:
[floorplans_1_valid_for_export] => 1
[floorplans_1_title] => title xx
[floorplans_1_house_area] => 90m²
[floorplans_1_bedrooms] => 2
[floorplans_2_valid_for_export] => 1
[floorplans_2_title] => title 2
[floorplans_2_house_area] => 50m²
[floorplans_2_bedrooms] => 1
This is an odd schema, but it can be done by iterating through the array and searching for keys where "valid_for_export" equals 1, and then using another array of field "stubs" to get the associated items by a unique identifier of X in floorplans_X_valid_for_export
$array = [
'floorplans_0_valid_for_export' => 0,
'floorplans_0_title' => 'title 1',
'floorplans_0_house_area' => '40m²',
'floorplans_0_bedrooms' => 1,
'floorplans_1_valid_for_export' => 1,
'floorplans_1_title' => 'title xx',
'floorplans_1_house_area' => '90m²',
'floorplans_1_bedrooms' => '2',
'floorplans_2_valid_for_export' => 1,
'floorplans_2_title' => 'title 2',
'floorplans_2_house_area' => '50m²',
'floorplans_2_bedrooms' => 1,
'floorplans' => 3
];
$stubs = [
'floorplans_%s_valid_for_export',
'floorplans_%s_title',
'floorplans_%s_house_area',
'floorplans_%s_bedrooms'
];
$newArr = [];
foreach ($array as $key => $value) {
if (strpos($key, 'valid_for_export') && $array[$key] == 1) {
$intVal = filter_var($key, FILTER_SANITIZE_NUMBER_INT);
foreach ($stubs as $stub) {
$search = sprintf($stub, $intVal);
if (isset($array[$search])) {
$newArr[$search] = $array[$search];
} else {
// key can't be found, generate one with null
$newArr[$search] = null;
}
}
}
}
echo '<pre>';
print_r($newArr);
Working: http://sandbox.onlinephpfunctions.com/code/23a225e3cefa2dc9cc97f53f1cbae0ea291672c0
Use a parent loop to check that the number-specific valid_for_export value is non-empty -- since it is either 0 or non-zero.
If so, then just push all of the associated elements into the result array.
Some reasons that this answer is superior to the #Alex's answer are:
Alex's parent loop makes 13 iterations (and the same number of strpos() calls); mine makes just 3 (and only 3 calls of empty()).
$array[$key] is more simply written as $value.
Sanitizing the $key to extract the index/counter is more overhead than necessary as demonstrated in my answer.
Code (Demo)
$array = [
'floorplans_0_valid_for_export' => 0,
'floorplans_0_title' => 'title 1',
'floorplans_0_house_area' => '40m²',
'floorplans_0_bedrooms' => 1,
'floorplans_1_valid_for_export' => 1,
'floorplans_1_title' => 'title xx',
'floorplans_1_house_area' => '90m²',
'floorplans_1_bedrooms' => '2',
'floorplans_2_valid_for_export' => 1,
'floorplans_2_title' => 'title 2',
'floorplans_2_house_area' => '50m²',
'floorplans_2_bedrooms' => 1,
'floorplans' => 3
];
$labels = ['valid_for_export', 'title', 'house_area', 'bedrooms'];
$result = [];
for ($i = 0; $i < $array['floorplans']; ++$i) {
if (!empty($array['floorplans_' . $i . '_valid_for_export'])) {
foreach ($labels as $label) {
$key = sprintf('floorplans_%s_%s', $i, $label);
$result[$key] = $array[$key];
}
}
}
var_export($result);
Output:
array (
'floorplans_1_valid_for_export' => 1,
'floorplans_1_title' => 'title xx',
'floorplans_1_house_area' => '90m²',
'floorplans_1_bedrooms' => '2',
'floorplans_2_valid_for_export' => 1,
'floorplans_2_title' => 'title 2',
'floorplans_2_house_area' => '50m²',
'floorplans_2_bedrooms' => 1,
)
With that constructed data it might be hard (not impossble tho), hovewer i would suggest to change it to multidimensional arrays so you have something like:
[floorplans][0][valid_for_export] => 0
[floorplans][0][title] => title 1
[floorplans][0][house_area] => 40m²
[floorplans][0][bedrooms] => 1
[floorplans][1][valid_for_export] => 1
[floorplans][1][title] => title xx
[floorplans][1][house_area] => 90m²
Rought sollution
It is not the best approach, but it should work if you dont need anything fancy, and know that structure of data wont change in future
$keys = [];
$for($i=0;$i<$array['floorplans'];++$i) {
if(isset($array['floorplans_'.$i.'_valid_for_export']) && $array['floorplans_'.$i.'_valid_for_export']===1) {
$keys[] = $i;
}
}
print_r($keys);

Inject an array inbetween another array in PHP

I have to merge (inject) an array in between another array.
Injected array is adding an additional level at the top.
Actual code is different, but here I have created a simple example to illustrate the issue I am facing.
Here is the sample code:
$mid_array = [
'heading3' => 'Heading 3',
'heading4' => 'Heading 4'
];
$main_array = [
'heading1' => 'Heading 1',
'heading2' => 'Heading 2',
$mid_array,
'heading5' => 'Heading 5'
];
echo '<pre>'; print_r($main_array); echo '</pre>';
Output I am getting
Array
(
[heading1] => Heading 1
[heading2] => Heading 2
[0] => Array
(
[heading3] => Heading 3
[heading4] => Heading 4
)
[heading5] => Heading 5
)
This is what exactly I need
Array
(
[heading1] => Heading 1
[heading2] => Heading 2
[heading3] => Heading 3
[heading4] => Heading 4
[heading5] => Heading 5
)
Based on the assumption that your arrays might change but will continue to have keys like heading1, heading2 this would be my solution
$newArray = array_merge($main_array,$mid_array);
ksort($newArray);
this will return the array you need.
I think you are looking for array_splice, as such:
array_splice($main_array, 2, 0, $mid_array);
Note: array_splice does not preserve keys, if keys are important to you, use following:
function array_insert(&$input, array $arrayToAdd, int $atPosition) {
$before = array_slice($input, 0, $atPosition, true);
$after = array_slice($input, $atPosition, null, true);
$input = $before + $arrayToAdd + $after;
}
Usage
array_insert($main_array, $mid_array, 2);
I think you are looking for the array_merge functionality.
http://php.net/manual/en/function.array-merge.php
You can use array_reduce:
$after = 'heading2';
$result = array_reduce(
array_keys($main_array),
function ($carry, $key) use ($main_array, $after, $mid_array) {
$carry[$key] = $main_array[$key];
return $key === $after
? array_merge($carry, $mid_array)
: $carry;
},
[]
);
Here is the demo.
If key ordering presents a problem (with something like array_merge) you can flatten the inserted array by walking it recursively. You walk along the tips plucking what you need as you go.
<?php
$insert = [
'foo' => 'And it\'s been the ruin of many a poor boy',
'bar' => 'And god I know I\'m one'
];
$inserted = [
'big' => 'There is a house in New Orleans',
'fat' => 'They call the rising sun',
$insert,
'mama' => 'My mother was a taylor'
];
$flattened = [];
array_walk_recursive(
$inserted,
function($v, $k) use (&$flattened) {
$flattened[$k] = $v;
}
);
var_export($flattened);
Output:
array (
'big' => 'There is a house in New Orleans',
'fat' => 'They call the rising sun',
'foo' => 'And it\'s been the ruin of many a poor boy',
'bar' => 'And god I know I\'m one',
'mama' => 'My mother was a taylor',
)

Get keys from multidimentional array by value

I need to get all "top level" keys from multidimensional array by searching the "bottom level" values. Here is an example of the array:
$list = array (
'person1' => array(
'personal_id' => '1',
'short_information' => 'string',
'books_on_hand' => array(
'Book 1',
'Book 2',
'Book 3',
)
),
'person2' => array(
'personal_id' => '2',
'short_information' => 'string',
'books_on_hand' => array(
'Book 4',
'Book 2',
'Book 5',
)
),
'person3' => array(
'personal_id' => '3',
'short_information' => 'string',
'books_on_hand' => array(
'Book 4',
'Book 2',
'Book 1',
'Book 3',
)
),
//etc...
);
I want to know all persons who have "Book 2" on hand. I can get that information by loop like this:
foreach ($list as $person => $info){
$check = array_search( 'Book 2', array_column($info, 'books_on_hand') );
if ( $check !== false ){
$results .= 'Name: '.$person;
$results .= 'ID: '.$info['personal_id'];
//get other infos other stuff, if necessary
}
}
The problem is, that foreach in this case is very heavy on memory and only grows more when array has a thousand+ entries. It needs to run through all of the persons, even if only 3 persons at the very top of the array have "Book 2".
I have been trying to optimize it by getting persons with "Book 2" using built-in functions like array_search, array_keys, array_column and only then run foreach for persons found, but I had no luck with getting "top level" keys.
Is it possible to optimize or use built-in function to search multidimensional array?
One way would be to filter it first. Now your result is structured like $list but it only contains elements with the needed book:
$find = 'Book 2';
$result = array_filter($list, function($v) use($find) {
return in_array($find, $v['books_on_hand']);
});
If all you're interested in is the person key and personal_id then this:
$find = 'Book 2';
$result = array_map(function($v) use($find) {
if(in_array($find, $v['books_on_hand'])) {
return $v['personal_id'];
}
}, $list);
Will return something like this for persons with the needed book:
Array
(
[person1] => 1
[person2] => 2
[person3] => 3
)

Multidimensional Array - Pulling Data

New to PHP and after spending hours researching on here, nothing seems to be exactly what I need. I have a multi dimensional array that I'm looking to pull data and COUNT from. FOR Instance:
array (
'loyola' => NULL,
'gold_coast' => NULL,
'lincolnpark' =>
array (
0 => 'Building 1',
1 => 'Building 2',
2 => 'Building 3',
3 => 'Building 4'
),
'lakeview' =>
array (
0 => 'Building 1',
1 => 'Building 2',
2 => 'Building 3'
),
)
I'm looking to essentially create a table that lists all the buildings and in the next column the number of times that building appears.
This what I've gotten thus far, but it only displays all buildings.
$buildings = unserialize($row['buildings']);
$lincolnpark = $buildings['lincolnpark'];
$loyola= $buildings['loyola'];
$gold_coast = $buildings['gold_coast'];
$lakeview = $buildings['lakeview'];
foreach ($lakeview as $value)
{
echo $value;
}
}
Try the code below. It will navigate recursively into the array and will print the qtd each build appear.
<?php
$arr = array (
'loyola' => NULL,
'gold_coast' => NULL,
'lincolnpark' =>
array (
0 => 'Building 1',
1 => 'Building 2',
2 => 'Building 3',
3 => 'Building 4'
),
'lakeview' =>
array (
0 => 'Building 1',
1 => 'Building 2',
2 => 'Building 3'
),
);
$ret = array();
countBuildings($arr);
foreach($ret as $key=>$value){
echo "Building: $key ==> qtd : $value <br>";
}
function countBuildings($arr = array()){
global $ret;
foreach($arr as $value){
if(is_array($value)){
countBuildings($value);
}else{
if($value != NULL){
if(isset($ret[$value])){
$ret[$value] += 1;
}else{
$ret[$value] = 1;
}
}
}
}
}
Do it in two passes: one for counting building occurrences in a separate array, and another for output.

PHP How can I pass values from one array to another?

I'm trying to pass some of the values from theOptions array and drop them into a new array called $theDefaults.
$theOptions = array(
'item1' => array('title'=>'Title 1','attribute'=>'Attribute 1','thing'=>'Thing 1'),
'item2' => array('title'=>'Title 2','attribute'=>'Attribute 2','thing'=>'Thing 2'),
'item3' => array('title'=>'Title 3','attribute'=>'Attribute 3','thing'=>'Thing 3')
);
So, $theDefaults array should look like this:
$theDefaults = array(
'Title 1' => 'Attribute 1',
'Title 2' => 'Attribute 2',
'Title 3' => 'Attribute 3'
);
However, I cannot figure out how to do this.
Have tried this but it is clearly not quite working.
$theDefaults = array();
foreach($theOptions as $k=>$v) {
array_push($theDefaults, $v['title'], $v['attribute']);
}
but when I run this...
foreach($theDefaults as $k=>$v) {
echo $k .' :'.$v;
}
It returns this.
0 :Title 11 :Attribute 12 :Title 23 :Attribute 24 :Title 35 :Attribute 3
Looks to be soooo close, but why are the numbers in the array?
It's even simpler than that:
$theDefaults = array();
foreach($theOptions as $v) {
$theDefaults[$v['title']] = $v['attribute'];
}

Categories