PHP: Adding a key-value pair to an array - php

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

Related

php - check multidimensional array for values that exists in more than 1 subarray

Following simplified multidimensional array given:
$input = Array
(
[arr1] => Array
(
[0] => JAN2016
[1] => MAI2013
[2] => JUN2014
}
[arr2] => Array
(
[0] => APR2016
[1] => DEC2013
[2] => JUN2014
}
[arr3] => Array
(
[0] => JAN2016
[1] => MAI2020
[2] => JUN2022
}
)
I want to check for elements, that exists in more than 1 subarray. An ideal output would be:
$output = Array
(
[JUN2014] => Array
(
[0] => arr1
[1] => arr2
)
[JAN2016] => Array
(
[0] => arr1
[1] => arr3
)
)
I'm currently stucked in a nested foreach because i need to look all silblings of the outer foreach and don't know how to accomplish that.
foreach($input as $k=>$values)
{
foreach($values as $value)
{
//check if value exists in array k+1....n
//if true, safe to output.
}
}
You are almost all the way there
$new = [];
foreach($input as $k=>$values) {
foreach($values as $value) {
$new[$value][] = $k;
}
}
The $new array should look just as you want it
Extended solution which filters subarrays:
$newArray = [];
foreach($input as $k=>$values)
{
foreach($values as $value)
{
$newArray[$value][] = $k;
}
}
print_r(array_filter(
$newArray,
function($v) { return 1 < count($v); }
));
Sample fiddle here.

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

Flatten an subarray and transform its values in key and also in value of the new array using PHP

I have following multidimensional array:
Array (
[0] => Array
(
[0] => 2007
[1] => 318
)
[1] => Array
(
[0] => 2001
[1] => 307
)
[2] => Array
(
[0] => 1993
[1] => 306
)
[3] => Array
(
[0] => 2011
[1] => 285
)
)
and would like to transform it in:
Array (
[2007] => 318,
[2001] => 307,
[1993] => 306,
[2011] => 285
)
Also the value of key[0] of the subarray will be the key of the new array and value of key[1] of the subarray will be the value of the new array.
I tried that, but only success:
$newArray = array();
foreach ($chart05 as $items) {
$newArray = array_merge($newArray, $items);
}
and this as well:
foreach($array as $k => $v){
foreach($v as $value) {
$newArray[] = $value;
}
but none give me the expect result.
Then I would like to transform the new array in a string just like that:
[2007,318], [2001,307], [1993,306], [2011,285]
Here I'm not sure, but I think implode would work. What do you guys think?
I would appreciate any help!!!
$newArray = array();
foreach ($chart05 as $items) {
$newArray[$items[0]] = $items[1];
}
Wil create the array structure you want.
However for the string output, i believe you dont want to change the original array, just json_encode it:
echo json_encode($chart05);
Live example: http://codepad.viper-7.com/tYxECS

Merge multidimensional arrays in PHP

I would like create a list of values within a new array based on the same keys from the previous array. Basically, I would like to turn this array:
$old_array = Array (
[segment1] => Array (
[subsegment] => Array (
[number1] => 1413
[number2] => 306
)
)
[segment2] => Array (
[subsegment] => Array (
[number1] => 717
[number2] => 291
)
)
)
...into this array:
$new_array = Array (
[segment] => Array (
[subsegment] => Array (
[number1] => Array (
[0] => 1413
[1] => 717
)
[number2] => Array (
[0] => 306
[1] => 291
)
)
)
)
I tried the following:
$new_array = array ();
foreach ($old_array["segment"]["subsegment"] as $value) {
$new_array["segment"]["subsegment"][] = $value;
}
Unfortunately, this doesn't work. What do I need to do? Thanks.
This is very specific to your example $old_array:
$index = 1;
$new_array = array();
do {
if (!isset($old_array["segment" . $index]["subsegment"]))
break;
foreach ($old_array["segment" . $index]["subsegment"] as $key => $value) {
$new_array["segment"]["subsegment"][$key][] = $value;
}
$index++;
} while (true);
I understand you want all number1's in the same key, then all number 2's, and so on. try this:
$numberCount = count($old_array['segment1']['subsegment']);
foreach ($old_array as $segment)
{for ($i=1;$i<=$numberCount;$i++)
{$new_array['segment']['subsegment']['number' . $i][] = $segment['subsegment']['number' . $i];}}
this is assuming all subsegment have the same number of [numberx] keys

PHP - Compare two array and remove both item in a specific index if either of the array have empty value

Refer to 2 array below:
$bar_arr =
Array
(
Array
(
[bar] => bar01.jpg
[position] => 1
)
Array
(
[bar] => bar02.jpg
[position] => 2
)
Array
(
[bar] => bar03.jpg
[position] => 3
)
)
$banner_arr =
Array
(
Array
(
[banner] =>
[position] => 1
)
Array
(
[banner] => banner02.jpg
[position] => 2
)
Array
(
[banner] => banner03.jpg
[position] => 3
)
)
$banner_arr[0][banner] don't have value, so I would like to remove this index. In the meantime$bar_arr[0][bar] would also be removed, I want to end up like this:
$bar_arr =
Array
(
Array
(
[bar] => bar02.jpg
[position] => 2
)
Array
(
[bar] => bar03.jpg
[position] => 3
)
)
$banner_arr =
Array
(
Array
(
[banner] => banner02.jpg
[position] => 2
)
Array
(
[banner] => banner03.jpg
[position] => 3
)
)
My question is how to compare this two array and remove both item in a specific index if either of the array have empty value.
Thanks
If you're just checking the value of banner and you assume that the two arrays are ordered identically, this is fairly simple (You might need to make a copy of banner_arr first ... not sure):
foreach ($banner_arr as $key => $banner) {
if (empty($banner['banner'])) {
unset($banner_arr[$key]);
unset($bar_arr[$key]);
}
}
More likely though, the order of the arrays can't be relied upon. In this case, just use an additional array of positions and track all the positions that need to be removed, and unset those:
$positions = array();
foreach ($banner_arr as $key => $banner) {
if (empty($banner['banner'])) {
$positions[] = $banner['position'];
unset($banner_arr[$key]);
}
}
then search through $bar_arr for corresponding positions:
foreach ($bar_arr as $key => $bar) {
if (in_array($bar['position'], $positions)) {
unset($bar_arr[$key]);
}
}
I'm assuming that both arrays are the same length and that the only possible missing values are in ['bar'] or ['banner'].
Basically I'd just loop through the array and store the valid values in new arrays;
$new_bar_arr = array();
$new_banner_arr = array();
$count = count($banner_arr);
$index = 0;
while($index < $count){
if(!empty($bar_arr[$index]['bar']) && !empty($banner_arr[$index]['banner'])){
$new_bar_arr[] = $bar_arr[$index];
$new_banner_arr[] = $banner_arr[$index];
}
$index++;
}
Assuming your count lines up like you've suggested:
$newArray = array_map( NULL, $banner_arr, $bar_arr );
foreach( $newArray as $key => $array ){
foreach( $array as $arr ){
if( $arr === NULL ){
unset( $newArray[$key] );
}
}
}
Even if it doesn't, I'd just make a new function and use array map still.

Categories